useOTA.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { useSettings } from '~/store/useSettings';
  2. import type { Tota } from '~/types/ota';
  3. export async function useOTA() {
  4. const {
  5. $config: { public: { APP_VERSION } },
  6. $api
  7. } = useNuxtApp()
  8. const { $state: { settings } } = useSettings()
  9. const log = useLogger('OTA')
  10. const latestUpdate = ref<Tota>({} as Tota)
  11. try {
  12. log.info('Получение обновлений...')
  13. latestUpdate.value = await $api.ota.getVersion(settings.dev)
  14. log.info(`Последняя актуальная версия: ${latestUpdate.value.version}`)
  15. } catch (e) {
  16. log.error('Ошибка получения обновлений ', e)
  17. }
  18. function needsUpdate() {
  19. if (latestUpdate.value?.version) {
  20. return [
  21. latestUpdate.value.version !== APP_VERSION,
  22. latestUpdate.value.version,
  23. APP_VERSION,
  24. latestUpdate.value.apkfile,
  25. ]
  26. }
  27. }
  28. function parseUpdateLog(text: string) {
  29. const result = { features: [], bugfixes: [] };
  30. const getSection = (title) =>
  31. (text.match(new RegExp(`###\\s*${title}([\\s\\S]*?)(?=\\n###\\s*|$)`, 'i')) || [])[1] || '';
  32. const normalize = (line) =>
  33. line
  34. .replace(/^\s*[*-]\s*/, '')
  35. .replace(/\*\*/g, '')
  36. .replace(/\s*\([a-f0-9]{7,}\)\s*$/i, '')
  37. .replace(/\[([^\]]+)]\([^)]+\)/g, '$1')
  38. .trim();
  39. ['Bug Fixes', 'Features'].forEach((title) =>
  40. getSection(title)
  41. .split('\n')
  42. .filter((l) => /^\s*[*-]\s+/.test(l))
  43. .map(normalize)
  44. .forEach((item) =>
  45. title === 'Bug Fixes' ? result.bugfixes.push(item) : result.features.push(item)
  46. )
  47. );
  48. return result;
  49. }
  50. function getDescription() {
  51. const data = parseUpdateLog(latestUpdate.value.description)
  52. log.info('Описание обновления:', data)
  53. return data;
  54. }
  55. return {
  56. needsUpdate,
  57. getDescription,
  58. latestUpdate,
  59. }
  60. }