useOTA.ts 2.1 KB

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