From c3c09db50019bd51146a2341ef8223fd8c96fd87 Mon Sep 17 00:00:00 2001 From: Izuco Date: Fri, 5 Nov 2021 21:02:55 +0100 Subject: [PATCH] Add updater module --- .gitignore | 2 + modules/module.updater.ts | 88 +++++++++++++++++++++++++++++++++++++++ package.json | 2 +- tsc.ts | 3 ++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 modules/module.updater.ts diff --git a/.gitignore b/.gitignore index 0a3122a..3a3cb18 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ token.yml *.resume *.user.yml lib +test.* +updates.json \ No newline at end of file diff --git a/modules/module.updater.ts b/modules/module.updater.ts new file mode 100644 index 0000000..48d0d9b --- /dev/null +++ b/modules/module.updater.ts @@ -0,0 +1,88 @@ +import got from "got"; +import fs from "fs"; +import { GithubTag, TagCompare } from "../@types/github"; +import path from "path"; +import { UpdateFile } from "../@types/updateFile"; +import packageJson from '../package.json'; +const updateFilePlace = path.join(__dirname, '..', 'config', 'updates.json'); + +const updateIgnore = [ + '*.json', + '*.d.ts', + '.git', + 'lib', + 'node_modules', + '@types', + path.join('bin', 'mkvtoolnix'), + path.join('config', 'token.yml'), + '.eslint', + 'tsconfig.json', + 'updates.json' +]; + +(async (force = false) => { + let updateFile: UpdateFile|undefined; + if (fs.existsSync(updateFilePlace)) { + updateFile = JSON.parse(fs.readFileSync(updateFilePlace).toString()) as UpdateFile; + if (new Date() < new Date(updateFile.nextCheck) && !force) { + return; + } + } + console.log('Checking for updates...') + const tagRequest = await got('https://api.github.com/repos/anidl/multi-downloader-nx/tags') + const tags = JSON.parse(tagRequest.body) as GithubTag[]; + + if (tags.length > 0) { + const newer = tags.filter(a => { + return a.name > packageJson.version; + }) + console.log(`Found ${tags.length} release tags and ${newer.length} that are new.`) + + if (newer.length < 1) { + console.log('[INFO] No new tags found') + return done(); + } + const newest = newer.sort((a, b) => a.name < b.name ? 1 : a.name > b.name ? -1 : 0)[0]; + //TODO REMOVE + newest.name = 'auto-updates' + const compareRequest = await got(`https://api.github.com/repos/anidl/multi-downloader-nx/compare/${packageJson.version}...${newest.name}`) + + const compareJSON = JSON.parse(compareRequest.body) as TagCompare; + + console.log(`You are behind by ${compareJSON.behind_by} releases!`) + const changedFiles = compareJSON.files.filter(a => { + return !updateIgnore.some(filter => { + if (filter.startsWith('*')) { + return a.filename.endsWith(filter.slice(1)) + } else if (fs.statSync(filter).isDirectory()) { + return a.filename.startsWith(filter); + } else { + return a.filename.split('/').pop() === filter; + } + }) + }) + if (changedFiles.length < 1) { + console.log('[INFO] No file changes found... updating package.json. If you thing this is an error please get the newst version yourself.') + return done(newest.name); + } + console.log(`Found file changes: \n${changedFiles.map(a => ` [ + ${a.status === 'modified' ? '*' : a.status === 'added' ? '+' : '-'} + ] ${a.filename}`).join('\n')}`) + + } +})(true) + +function done(newVersion?: string) { + const next = new Date(Date.now() + 1000 * 60 * 60 * 24); + fs.writeFileSync(updateFilePlace, JSON.stringify({ + lastCheck: Date.now(), + nextCheck: next.getTime() + } as UpdateFile, null, 2)) + if (newVersion) { + fs.writeFileSync('../package.json', JSON.stringify({ + ...packageJson, + version: newVersion + }, null, 4)) + } + console.log('[INFO] Searching for update finished. Next time running on the ' + next.toLocaleDateString() + ' at ' + next.toLocaleTimeString() + '.') +} \ No newline at end of file diff --git a/package.json b/package.json index 040033f..f6bfbb6 100644 --- a/package.json +++ b/package.json @@ -63,4 +63,4 @@ "pretest": "npm run tsc", "test": "cd lib && node modules/build win64 && node modules/build linux64 && node modules/build macos64" } -} +} \ No newline at end of file diff --git a/tsc.ts b/tsc.ts index d344b05..51640ab 100644 --- a/tsc.ts +++ b/tsc.ts @@ -10,10 +10,13 @@ const ignore = [ '@types', path.join('bin', 'mkvtoolnix'), path.join('config', 'token.yml'), + path.join('config', 'updates.json'), '.eslint', 'tsconfig.json' ].map(a => path.join(__dirname, a)); +export { ignore } + (async () => { removeSync('lib'); const tsc = exec('npx tsc');