[Updater > Module] Smooth install progress

This commit is contained in:
Ducko 2022-03-14 18:20:02 +00:00
parent bbaa567c75
commit 14d9aab003
2 changed files with 21 additions and 70 deletions

View file

@ -10,21 +10,26 @@ exports.open = async (zipPath, _opts, callback) => {
listeners.error(err);
};
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);
});
callback(null, {
on: (event, listener) => {
listeners[event] = listener;
},
readEntry: () => {}, // Stubs as not used
openReadStream: () => {},
close: () => {}
entryCount
});
mkdirp.sync(extractPath);
const proc = execFile('unzip', ['-q', '-o', zipPath, '-d', extractPath]);
proc.stderr.on('data', errorOut);
const proc = execFile('unzip', ['-o', zipPath, '-d', extractPath]);
proc.on('error', (err) => {
if (err.code === 'ENOENT') {
@ -35,5 +40,9 @@ exports.open = async (zipPath, _opts, callback) => {
errorOut(err);
});
proc.stderr.on('data', errorOut);
proc.stdout.on('data', (x) => x.toString().split('\n').forEach((x) => x.includes('inflating') && listeners.entry()));
proc.on('close', () => listeners.end());
};

View file

@ -696,88 +696,30 @@ function processUnzipQueue() {
let succeeded = true;
const extractRoot = _path.default.join(moduleInstallPath, queuedModule.name);
logger.log(`Installing ${queuedModule.name}@${queuedModule.version} from ${queuedModule.zipfile}`);
const processZipfile = (err, zipfile) => {
if (err) {
onError(err, null);
return;
}
if (err) return onError(err);
const totalEntries = zipfile.entryCount;
let processedEntries = 0;
zipfile.on('entry', entry => {
zipfile.on('entry', () => {
processedEntries += 1;
const percent = Math.min(Math.floor(processedEntries / totalEntries * 100), 100);
events.append({
type: INSTALLING_MODULE_PROGRESS,
name: queuedModule.name,
progress: percent
}); // skip directories
if (/\/$/.test(entry.fileName)) {
zipfile.readEntry();
return;
}
zipfile.openReadStream(entry, (err, stream) => {
if (err) {
onError(err, zipfile);
return;
}
stream.on('error', e => onError(e, zipfile));
(0, _mkdirp.default)(_path.default.join(extractRoot, _path.default.dirname(entry.fileName)), err => {
if (err) {
onError(err, zipfile);
return;
} // [adill] createWriteStream via original-fs is broken in Electron 4.0.0-beta.6 with .asar files
// so we unzip to a temporary filename and rename it afterwards
const tempFileName = _path.default.join(extractRoot, entry.fileName + '.tmp');
const finalFileName = _path.default.join(extractRoot, entry.fileName);
const writeStream = originalFs.createWriteStream(tempFileName);
writeStream.on('error', e => {
stream.destroy();
try {
originalFs.unlinkSync(tempFileName);
} catch (err) {}
onError(e, zipfile);
});
writeStream.on('finish', () => {
try {
originalFs.unlinkSync(finalFileName);
} catch (err) {}
try {
originalFs.renameSync(tempFileName, finalFileName);
} catch (err) {
onError(err, zipfile);
return;
}
zipfile.readEntry();
});
stream.pipe(writeStream);
});
});
});
zipfile.on('error', err => {
onError(err, zipfile);
});
zipfile.on('error', onError);
zipfile.on('end', () => {
if (!succeeded) return;
installedModules[queuedModule.name].installedVersion = queuedModule.version;
finishModuleUnzip(queuedModule, succeeded);
});
zipfile.readEntry();
};
try {
@ -786,7 +728,7 @@ function processUnzipQueue() {
autoClose: true
}, processZipfile);
} catch (err) {
onError(err, null);
onError(err);
}
}