[Updater > v2] Partial rewriting

This commit is contained in:
Ducko 2022-03-16 10:51:06 +00:00
parent b8a04bc1a3
commit 0ef70bff3e

View file

@ -25,7 +25,7 @@ class Updater extends EventEmitter {
try { try {
nativeUpdaterModule = options.nativeUpdaterModule ?? require(paths.getExeDir() + '/updater'); nativeUpdaterModule = options.nativeUpdaterModule ?? require(paths.getExeDir() + '/updater');
} catch (e) { } catch (e) {
log('Updater', 'Failed to require nativeUpdater', e); log('Updater', 'Require fail', e);
if (e.code === 'MODULE_NOT_FOUND') return; if (e.code === 'MODULE_NOT_FOUND') return;
throw e; throw e;
@ -41,6 +41,7 @@ class Updater extends EventEmitter {
this.currentlyDownloading = {}; this.currentlyDownloading = {};
this.currentlyInstalling = {}; this.currentlyInstalling = {};
this.hasEmittedUnhandledException = false; this.hasEmittedUnhandledException = false;
this.nativeUpdater = new nativeUpdaterModule.Updater({ this.nativeUpdater = new nativeUpdaterModule.Updater({
response_handler: this._handleResponse.bind(this), response_handler: this._handleResponse.bind(this),
...options ...options
@ -79,10 +80,7 @@ class Updater extends EventEmitter {
const [id, detail] = JSON.parse(response); const [id, detail] = JSON.parse(response);
const request = this.requests.get(id); const request = this.requests.get(id);
if (request == null) { if (request == null) return log('Updater', 'Unknown resp', id, detail);
console.error('Received response ', detail, ' for a request (', id, ') not in the updater request map.');
return;
}
if (detail['Error'] != null) { if (detail['Error'] != null) {
const { const {
@ -131,10 +129,10 @@ class Updater extends EventEmitter {
this.emit('host-updated'); this.emit('host-updated');
} }
} else { } else {
console.warn('Unknown updater response', detail); log('Updater', 'Unknown resp', id, detail);
} }
} catch (e) { } catch (e) {
console.error('Unhandled exception in updater response handler:', e); log('Updater', 'Handler excepetion', e);
if (!this.hasEmittedUnhandledException) { if (!this.hasEmittedUnhandledException) {
this.hasEmittedUnhandledException = true; this.hasEmittedUnhandledException = true;
@ -154,19 +152,18 @@ class Updater extends EventEmitter {
return detail['VersionInfo']; return detail['VersionInfo'];
} }
console.warn('Unknown updater response', detail); log('Updater', 'Unknown resp', detail);
} }
_getHostPath() { _getHostPath() {
const [major, minor, revision] = this.committedHostVersion; const [major, minor, revision] = this.committedHostVersion;
const hostVersionStr = `${major}.${minor}.${revision}`; const hostVersionStr = `${major}.${minor}.${revision}`;
return join(this.rootPath, `app-${hostVersionStr}`);
return join(this.rootPath, `app-${this.committedHostVersion.join('.')}`);
} }
_startCurrentVersionInner(options, versions) { _startCurrentVersionInner(options, versions) {
if (this.committedHostVersion == null) { if (this.committedHostVersion == null) this.committedHostVersion = versions.current_host;
this.committedHostVersion = versions.current_host;
}
const hostPath = this._getHostPath(); const hostPath = this._getHostPath();
@ -179,9 +176,9 @@ class Updater extends EventEmitter {
stdio: 'inherit' stdio: 'inherit'
}); });
}); });
console.log(`Restarting from ${resolve(process.execPath)} to ${resolve(hostExePath)}`);
app.quit(); log('Updater', 'Restarting', resolve(process.execPath), '->', resolve(hostExePath));
return; return app.quit();
} }
this._commitModulesInner(versions); this._commitModulesInner(versions);
@ -252,15 +249,10 @@ class Updater extends EventEmitter {
} }
_recordTaskProgress(progress) { _recordTaskProgress(progress) {
if (progress.task.HostDownload != null) { if (progress.task.HostDownload != null) this._recordDownloadProgress('host', progress);
this._recordDownloadProgress('host', progress); else if (progress.task.HostInstall != null) this._recordInstallProgress('host', progress, null, progress.task.HostInstall.from_version != null);
} else if (progress.task.HostInstall != null) { else if (progress.task.ModuleDownload != null) this._recordDownloadProgress(progress.task.ModuleDownload.version.module.name, progress);
this._recordInstallProgress('host', progress, null, progress.task.HostInstall.from_version != null); else if (progress.task.ModuleInstall != null) this._recordInstallProgress(progress.task.ModuleInstall.version.module.name, progress, progress.task.ModuleInstall.version.version, progress.task.ModuleInstall.from_version != null);
} else if (progress.task.ModuleDownload != null) {
this._recordDownloadProgress(progress.task.ModuleDownload.version.module.name, progress);
} else if (progress.task.ModuleInstall != null) {
this._recordInstallProgress(progress.task.ModuleInstall.version.module.name, progress, progress.task.ModuleInstall.version.version, progress.task.ModuleInstall.from_version != null);
}
} }
queryCurrentVersions() { queryCurrentVersions() {
@ -339,9 +331,7 @@ class Updater extends EventEmitter {
} }
async commitModules(versions) { async commitModules(versions) {
if (this.committedHostVersion == null) { if (this.committedHostVersion == null) throw new Error('Cannot commit modules before host version.');
throw new Error('Cannot commit modules before host version.');
}
if (versions == null) { if (versions == null) {
versions = await this.queryCurrentVersions(); versions = await this.queryCurrentVersions();
@ -361,65 +351,52 @@ class Updater extends EventEmitter {
} }
getKnownFolder(name) { getKnownFolder(name) {
if (!this.valid) { if (!this.valid) throw new Error(INVALID_UPDATER_ERROR);
throw new Error(INVALID_UPDATER_ERROR);
}
return this.nativeUpdater.known_folder(name); return this.nativeUpdater.known_folder(name);
} }
createShortcut(options) { createShortcut(options) {
if (!this.valid) { if (!this.valid) throw new Error(INVALID_UPDATER_ERROR);
throw new Error(INVALID_UPDATER_ERROR);
}
return this.nativeUpdater.create_shortcut(options); return this.nativeUpdater.create_shortcut(options);
} }
} }
function getUpdaterPlatformName(platform) {
switch (platform) {
case 'darwin':
return 'osx';
case 'win32':
return 'win';
default:
return platform;
}
}
function tryInitUpdater(buildInfo, repositoryUrl) {
const rootPath = paths.getInstallPath();
if (rootPath == null) {
return false;
}
instance = new Updater({
release_channel: buildInfo.releaseChannel,
platform: getUpdaterPlatformName(process.platform),
repository_url: repositoryUrl,
root_path: rootPath
});
return instance.valid;
}
function getUpdater() {
if (instance != null && instance.valid) {
return instance;
}
}
module.exports = { module.exports = {
Updater, Updater,
tryInitUpdater,
getUpdater,
TASK_STATE_COMPLETE, TASK_STATE_COMPLETE,
TASK_STATE_FAILED, TASK_STATE_FAILED,
TASK_STATE_WAITING, TASK_STATE_WAITING,
TASK_STATE_WORKING, TASK_STATE_WORKING,
INCONSISTENT_INSTALLER_STATE_ERROR INCONSISTENT_INSTALLER_STATE_ERROR,
tryInitUpdater: (buildInfo, repository_url) => {
const root_path = paths.getInstallPath();
if (root_path == null) return false;
let platform = process.platform;
switch (platform) {
case 'darwin':
platform = 'osx';
break;
case 'win32':
platform = 'win';
break;
}
instance = new Updater({
release_channel: buildInfo.releaseChannel,
platform,
repository_url,
root_path
});
return instance.valid;
},
getUpdater: () => (instance != null && instance.valid && instance) || null
}; };