dependency.service.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { BrowserWindow, dialog } from 'electron'
  2. import * as fs from 'fs'
  3. import * as path from 'path'
  4. import type { WallpaperMeta } from '@shared/types'
  5. import { t } from '../i18n'
  6. type DependencyInstaller = (dependencyId: string) => Promise<void>
  7. let installer: DependencyInstaller | null = null
  8. const alreadyAsked = new Set<string>()
  9. export function setDependencyInstaller(fn: DependencyInstaller): void {
  10. installer = fn
  11. }
  12. function getMissingDependency(wallpaper: WallpaperMeta): string | null {
  13. if (!wallpaper.localPath) return null
  14. try {
  15. const project = JSON.parse(fs.readFileSync(path.join(wallpaper.localPath, 'project.json'), 'utf8')) as { dependency?: unknown } | null
  16. const dependency = project?.dependency
  17. const dependencyId = typeof dependency === 'string' || typeof dependency === 'number' ? String(dependency) : ''
  18. if (!/^\d+$/.test(dependencyId)) return null
  19. const baseDir = path.join(path.dirname(path.resolve(wallpaper.localPath)), dependencyId)
  20. return fs.existsSync(path.join(baseDir, 'project.json')) ? null : dependencyId
  21. } catch {
  22. return null
  23. }
  24. }
  25. // throws unless the wallpaper can go ahead, since after "Install now" the download has only just started
  26. export async function ensureDependencyInstalled(
  27. wallpaper: WallpaperMeta,
  28. mode: 'always' | 'once' | 'never' = 'always'
  29. ): Promise<void> {
  30. const dependencyId = getMissingDependency(wallpaper)
  31. if (!dependencyId) return
  32. const title = wallpaper.title || wallpaper.id
  33. // startup restore must not open a dialog, least of all over a minimized tray start
  34. if (mode === 'never') throw new Error(t('dependency.skipped', { title, id: dependencyId }))
  35. if (mode === 'once' && alreadyAsked.has(dependencyId)) {
  36. throw new Error(t('dependency.skipped', { title, id: dependencyId }))
  37. }
  38. alreadyAsked.add(dependencyId)
  39. const options = {
  40. type: 'warning' as const,
  41. title: t('dependency.title'),
  42. message: t('dependency.message', { title }),
  43. detail: `${t('dependency.detail', { id: dependencyId })}\n\n${t('dependency.question')}`,
  44. buttons: [t('dependency.install'), t('dependency.applyAnyway'), t('common.cancel')],
  45. defaultId: 0,
  46. cancelId: 2
  47. }
  48. const parent = BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0]
  49. const { response } = parent ? await dialog.showMessageBox(parent, options) : await dialog.showMessageBox(options)
  50. if (response === 1) return
  51. if (response === 0) {
  52. if (!installer) throw new Error(t('dependency.notReady', { id: dependencyId }))
  53. try {
  54. await installer(dependencyId)
  55. } catch (err) {
  56. throw new Error(t('dependency.subscribeFailed', { id: dependencyId, error: (err as Error).message }), { cause: err })
  57. }
  58. throw new Error(t('dependency.downloading', { id: dependencyId, title }))
  59. }
  60. throw new Error(t('dependency.notApplied', { title, id: dependencyId }))
  61. }