2022-01-15 11:29:22 +00:00
|
|
|
// Jank replacement for yauzl by just using unzip where it expects and skipping (speed++, size--, jank++)
|
2022-01-25 22:21:47 +00:00
|
|
|
const { execFile } = require('child_process');
|
2022-03-11 13:44:16 +00:00
|
|
|
const mkdirp = require('mkdirp');
|
2022-01-14 14:02:26 +00:00
|
|
|
|
2022-03-11 13:44:16 +00:00
|
|
|
exports.open = async (zipPath, _opts, callback) => {
|
2022-03-11 13:49:53 +00:00
|
|
|
const extractPath = `${global.moduleDataPath}/${zipPath.split('/').pop().split('.')[0].split('-')[0]}`;
|
2022-01-14 14:02:26 +00:00
|
|
|
const listeners = [];
|
|
|
|
|
|
|
|
const errorOut = (err) => {
|
|
|
|
listeners.error(err);
|
|
|
|
};
|
|
|
|
|
2022-03-14 18:20:02 +00:00
|
|
|
const entryCount = await new Promise((res) => {
|
|
|
|
execFile('unzip', ['-l', zipPath]).stdout.on('data', (x) => {
|
|
|
|
const m = x.toString().match(/([0-9]+) files/);
|
|
|
|
if (m) res(parseInt(m[1]));
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(res, 500);
|
|
|
|
});
|
|
|
|
|
2022-01-14 14:02:26 +00:00
|
|
|
callback(null, {
|
|
|
|
on: (event, listener) => {
|
|
|
|
listeners[event] = listener;
|
|
|
|
},
|
|
|
|
|
2022-03-14 18:20:02 +00:00
|
|
|
entryCount
|
2022-01-14 14:02:26 +00:00
|
|
|
});
|
|
|
|
|
2022-03-11 13:44:16 +00:00
|
|
|
mkdirp.sync(extractPath);
|
2022-01-14 14:02:26 +00:00
|
|
|
|
2022-03-14 18:20:02 +00:00
|
|
|
const proc = execFile('unzip', ['-o', zipPath, '-d', extractPath]);
|
2022-01-14 14:02:26 +00:00
|
|
|
|
|
|
|
proc.on('error', (err) => {
|
2022-01-25 22:38:42 +00:00
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
require('electron').dialog.showErrorBox('Failed Dependency', 'Please install "unzip", exiting');
|
|
|
|
process.exit(1); // Close now
|
|
|
|
}
|
|
|
|
|
2022-01-14 14:02:26 +00:00
|
|
|
errorOut(err);
|
|
|
|
});
|
|
|
|
|
2022-03-14 18:20:02 +00:00
|
|
|
proc.stderr.on('data', errorOut);
|
|
|
|
|
|
|
|
proc.stdout.on('data', (x) => x.toString().split('\n').forEach((x) => x.includes('inflating') && listeners.entry()));
|
|
|
|
|
2022-03-10 21:37:12 +00:00
|
|
|
proc.on('close', () => listeners.end());
|
2022-01-14 14:02:26 +00:00
|
|
|
};
|