Skip to content

Commit

Permalink
feat: Switch from unzipper to adm-zip
Browse files Browse the repository at this point in the history
  • Loading branch information
cyperdark authored and KotRikD committed Feb 28, 2024
1 parent 358f0e6 commit 10f7d3e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 40 deletions.
2 changes: 2 additions & 0 deletions packages/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 2 additions & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"build": "tsc"
},
"dependencies": {
"adm-zip": "^0.5.10",
"dotenv": "^16.0.3",
"unzipper": "^0.10.14",
"winston": "^3.8.2"
}
}
54 changes: 54 additions & 0 deletions packages/common/utils/unzip.ts
Original file line number Diff line number Diff line change
@@ -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<string> =>
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<string> =>
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);
}
});
54 changes: 15 additions & 39 deletions packages/updater/index.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -26,30 +31,6 @@ const deleteNotLocked = async (filePath: string) => {
}
};

const unzipAsset = (zipPath: string, extractPath: string): Promise<string> =>
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');
Expand Down Expand Up @@ -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

Expand All @@ -122,7 +103,6 @@ export const autoUpdater = () =>

oldProcess.unref();

let started = false;
exec(
`start "" "${newExecutablePath}"`,
async (error, stdout, stderr) => {
Expand All @@ -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();
}
);
});
1 change: 0 additions & 1 deletion packages/updater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"build": "tsc"
},
"dependencies": {
"unzipper": "^0.10.14",
"@tosu/common": "workspace:*"
}
}

0 comments on commit 10f7d3e

Please sign in to comment.