From 10f7d3eac7e499e767ab835f921c93c89c91c2c3 Mon Sep 17 00:00:00 2001 From: ck <21735205+cyperdark@users.noreply.github.com> Date: Wed, 21 Feb 2024 13:17:33 +0300 Subject: [PATCH] feat: Switch from `unzipper` to `adm-zip` --- packages/common/index.ts | 2 ++ packages/common/package.json | 2 ++ packages/common/utils/unzip.ts | 54 ++++++++++++++++++++++++++++++++++ packages/updater/index.ts | 54 ++++++++++------------------------ packages/updater/package.json | 1 - 5 files changed, 73 insertions(+), 40 deletions(-) create mode 100644 packages/common/utils/unzip.ts diff --git a/packages/common/index.ts b/packages/common/index.ts index 52d7762c..969890de 100644 --- a/packages/common/index.ts +++ b/packages/common/index.ts @@ -4,3 +4,5 @@ export * from './utils/platforms'; export * from './utils/agruments'; export * from './utils/sleep'; export * from './utils/config'; +export * from './utils/unzip'; +export * from './utils/directories'; diff --git a/packages/common/package.json b/packages/common/package.json index c43d8666..495e529b 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -10,7 +10,9 @@ "build": "tsc" }, "dependencies": { + "adm-zip": "^0.5.10", "dotenv": "^16.0.3", + "unzipper": "^0.10.14", "winston": "^3.8.2" } } \ No newline at end of file diff --git a/packages/common/utils/unzip.ts b/packages/common/utils/unzip.ts new file mode 100644 index 00000000..de68c4cb --- /dev/null +++ b/packages/common/utils/unzip.ts @@ -0,0 +1,54 @@ +import AdmZip from 'adm-zip'; +import fs from 'fs'; +import path from 'path'; + +import { wLogger } from './logger'; + +export const unzipTosu = ( + zipPath: string, + extractPath: string +): Promise => + new Promise((resolve, reject) => { + const zip = new AdmZip(zipPath); + + zip.getEntries().some((entry) => { + if (entry.entryName === 'tosu' || entry.entryName === 'tosu.exe') { + const fileName = path.basename(entry.entryName); + const { name, ext } = path.parse(fileName); + const modifyName = path.join(extractPath, `${name}_new${ext}`); + + return fs.writeFile( + modifyName, + entry.getData(), + { flag: 'w' }, + (err) => { + if (err) { + return reject(err); + } + + return resolve(modifyName); + } + ); + } + }); + + reject('No matching entry found in the zip file.'); + }); + +export const unzip = (zipPath: string, extractPath: string): Promise => + new Promise((resolve, reject) => { + if (!fs.existsSync(extractPath)) { + fs.mkdirSync(extractPath); + } + + const zip = new AdmZip(zipPath); + + try { + // Extract all contents of the zip file to the specified destination + zip.extractAllTo(extractPath, /*overwrite*/ true); + resolve(extractPath); + } catch (error) { + wLogger.error((error as any).message); + reject(error); + } + }); diff --git a/packages/updater/index.ts b/packages/updater/index.ts index 7c08d8c5..24fe19eb 100644 --- a/packages/updater/index.ts +++ b/packages/updater/index.ts @@ -1,8 +1,13 @@ -import { downloadFile, platformResolver, sleep, wLogger } from '@tosu/common'; +import { + downloadFile, + platformResolver, + sleep, + unzipTosu, + wLogger +} from '@tosu/common'; import { exec, spawn } from 'child_process'; import fs from 'fs'; import path from 'path'; -import unzipper from 'unzipper'; // NOTE: _version.js packs with pkg support in tosu build const currentVersion = require(process.cwd() + '/_version.js'); @@ -26,30 +31,6 @@ const deleteNotLocked = async (filePath: string) => { } }; -const unzipAsset = (zipPath: string, extractPath: string): Promise => - new Promise((resolve, reject) => { - const zip = fs.createReadStream(zipPath).pipe(unzipper.Parse()); - - zip.on('entry', async (entry) => { - const fileName = entry.path; - if (fileName === 'tosu' || fileName === 'tosu.exe') { - const { name, ext } = path.parse(fileName); - const modifyName = path.join(extractPath, `${name}_new${ext}`); - - entry - .pipe(fs.createWriteStream(modifyName)) - .on('finish', () => { - resolve(modifyName); - }); - } else { - entry.autodrain(); - } - }); - - zip.on('error', reject); - // zip.on('finish', resolve); - }); - export const autoUpdater = () => new Promise(async (resolve) => { wLogger.info('Checking updates'); @@ -105,7 +86,7 @@ export const autoUpdater = () => fileDestination ); - const unzipExecutable = await unzipAsset(downloadAsset, process.cwd()); + const unzipExecutable = await unzipTosu(downloadAsset, process.cwd()); const currentExecutablePath = process.argv[0]; // Path to the current executable @@ -122,7 +103,6 @@ export const autoUpdater = () => oldProcess.unref(); - let started = false; exec( `start "" "${newExecutablePath}"`, async (error, stdout, stderr) => { @@ -131,18 +111,14 @@ export const autoUpdater = () => return; } - await sleep(1000); - started = true; - } - ); + await sleep(2500); - while (started == false) { - await sleep(1000); - } + wLogger.info('Closing program'); - wLogger.info('Closing program'); + await sleep(1000); - await sleep(1000); - oldProcess.kill(); - process.exit(); + oldProcess.kill(); + process.exit(); + } + ); }); diff --git a/packages/updater/package.json b/packages/updater/package.json index 554e32ed..50a4201b 100644 --- a/packages/updater/package.json +++ b/packages/updater/package.json @@ -10,7 +10,6 @@ "build": "tsc" }, "dependencies": { - "unzipper": "^0.10.14", "@tosu/common": "workspace:*" } } \ No newline at end of file