|
| 1 | +import { spawn, spawnSync } from 'node:child_process'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import chalk from 'chalk'; |
| 6 | +import ky from 'ky'; |
| 7 | +import ora from 'ora'; |
| 8 | +import prompts from 'prompts'; |
| 9 | +import { rimraf } from 'rimraf'; |
| 10 | +import semver from 'semver'; |
| 11 | +import type * as GitHubApiRel from '../types/GitHubApiRel.js'; |
| 12 | +import apiUtils from '../utils/api.js'; |
| 13 | +import argvUtils from '../utils/argv.js'; |
| 14 | +import configEmbed from '../utils/configEmbed.js'; |
| 15 | +import exitUtils from '../utils/exit.js'; |
| 16 | +import logger from '../utils/logger.js'; |
| 17 | + |
| 18 | +function isInstalledViaInstaller(): boolean { |
| 19 | + if (process.platform !== 'win32') return false; |
| 20 | + |
| 21 | + const appId = '{B0B8B114-AE98-4165-BFC7-E029C1DB80D4}_is1'; |
| 22 | + const regKeys = [ |
| 23 | + `HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\${appId}`, |
| 24 | + `HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\${appId}`, |
| 25 | + `HKLM\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\${appId}`, |
| 26 | + ]; |
| 27 | + |
| 28 | + for (const key of regKeys) { |
| 29 | + const result = spawnSync('reg', ['query', key], { stdio: 'ignore' }); |
| 30 | + if (result.status === 0) return true; |
| 31 | + } |
| 32 | + |
| 33 | + return false; |
| 34 | +} |
| 35 | + |
| 36 | +async function checkAppUpdate(): Promise<void> { |
| 37 | + // const testMode: boolean = false; |
| 38 | + // if (testMode) logger.warn('Update checker test mode is true!'); |
| 39 | + if (isInstalledViaInstaller() === false) return; |
| 40 | + await cleanupInstaller(); |
| 41 | + const githubApiUrl: string = 'https://api.github.com/repos/daydreamer-json/asmr-dl-ng/releases/latest'; |
| 42 | + const githubApiRsp: GitHubApiRel.Release | null = await (async () => { |
| 43 | + try { |
| 44 | + return await ky.get(githubApiUrl, apiUtils.defaultKySettings).json(); |
| 45 | + } catch (error) { |
| 46 | + logger.error('Failed to check for updates'); |
| 47 | + return null; |
| 48 | + } |
| 49 | + })(); |
| 50 | + if (githubApiRsp === null) return; |
| 51 | + const latestVersion = semver.clean(githubApiRsp.tag_name); |
| 52 | + const currentVersion = configEmbed.VERSION_NUMBER; |
| 53 | + if (latestVersion === null) throw new Error('Failed to get latest update'); |
| 54 | + if (currentVersion === null) throw new Error('Embed app version number is null'); |
| 55 | + if (semver.gt(latestVersion, currentVersion) === false) { |
| 56 | + logger.info(`App is up to date (local: ${chalk.green(currentVersion)}, remote: ${chalk.green(latestVersion)})`); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + logger.info(`Update is available (local: ${chalk.red(currentVersion)}, remote: ${chalk.green(latestVersion)})`); |
| 61 | + const userSelectRsp: boolean = ( |
| 62 | + await prompts( |
| 63 | + { |
| 64 | + name: 'value', |
| 65 | + type: 'toggle', |
| 66 | + message: 'Download and install updates automatically?', |
| 67 | + initial: true, |
| 68 | + active: 'yes', |
| 69 | + inactive: 'no', |
| 70 | + }, |
| 71 | + { |
| 72 | + onCancel: async () => { |
| 73 | + logger.error('Aborted'); |
| 74 | + exitUtils.exit(1, null, false); |
| 75 | + }, |
| 76 | + }, |
| 77 | + ) |
| 78 | + ).value; |
| 79 | + if (userSelectRsp === false) return; |
| 80 | + |
| 81 | + await downloadAndApplyUpdate(githubApiRsp); |
| 82 | +} |
| 83 | + |
| 84 | +async function downloadAndApplyUpdate(latestRelInfo: GitHubApiRel.Release): Promise<void> { |
| 85 | + if (!(process.platform === 'win32' && process.arch === 'x64')) { |
| 86 | + throw new Error(`This environment is not supported: ${process.platform}, ${process.arch}`); |
| 87 | + } |
| 88 | + |
| 89 | + const assetNamePattern = /asmr-dl-ng_win_x64_.+?_setup\.exe/g; |
| 90 | + const githubAsset = latestRelInfo.assets.find((e) => assetNamePattern.test(e.name)); |
| 91 | + if (!githubAsset) throw new Error('No update asset found'); |
| 92 | + |
| 93 | + const spinner = !argvUtils.getArgv()['no-show-progress'] |
| 94 | + ? ora({ text: 'Downloading installer ...', color: 'cyan', spinner: 'dotsCircle' }).start() |
| 95 | + : logger.info('Downloading installer ...'); |
| 96 | + const assetBuffer = await ky.get(githubAsset.browser_download_url).bytes(); |
| 97 | + const installerPath = path.join(os.tmpdir(), 'asmr-dl-ng_win_x64_setup.exe'); |
| 98 | + await fs.writeFile(installerPath, assetBuffer); |
| 99 | + spinner?.stop(); |
| 100 | + |
| 101 | + logger.info('Starting installer and exiting application ...'); |
| 102 | + |
| 103 | + const child = spawn(installerPath, ['/silent', '/norestart'], { |
| 104 | + detached: true, |
| 105 | + stdio: 'ignore', |
| 106 | + shell: true, |
| 107 | + }); |
| 108 | + |
| 109 | + child.unref(); |
| 110 | + |
| 111 | + logger.info('Please restart the app after installation is complete'); |
| 112 | + |
| 113 | + await exitUtils.exit(0, null, false); |
| 114 | +} |
| 115 | + |
| 116 | +async function cleanupInstaller(): Promise<void> { |
| 117 | + const installerPath = path.join(os.tmpdir(), 'asmr-dl-ng_win_x64_setup.exe'); |
| 118 | + await rimraf(installerPath); |
| 119 | +} |
| 120 | + |
| 121 | +export default { |
| 122 | + checkAppUpdate, |
| 123 | +}; |
0 commit comments