chore: tsify auto-updater (#24328)
This commit is contained in:
parent
4c449fbc75
commit
354ea00f17
6 changed files with 49 additions and 53 deletions
|
@ -186,10 +186,10 @@ auto_filenames = {
|
||||||
|
|
||||||
browser_bundle_deps = [
|
browser_bundle_deps = [
|
||||||
"lib/browser/api/app.ts",
|
"lib/browser/api/app.ts",
|
||||||
"lib/browser/api/auto-updater.js",
|
"lib/browser/api/auto-updater.ts",
|
||||||
"lib/browser/api/auto-updater/auto-updater-native.js",
|
"lib/browser/api/auto-updater/auto-updater-native.ts",
|
||||||
"lib/browser/api/auto-updater/auto-updater-win.js",
|
"lib/browser/api/auto-updater/auto-updater-win.ts",
|
||||||
"lib/browser/api/auto-updater/squirrel-update-win.js",
|
"lib/browser/api/auto-updater/squirrel-update-win.ts",
|
||||||
"lib/browser/api/base-window.ts",
|
"lib/browser/api/base-window.ts",
|
||||||
"lib/browser/api/browser-view.ts",
|
"lib/browser/api/browser-view.ts",
|
||||||
"lib/browser/api/browser-window.ts",
|
"lib/browser/api/browser-window.ts",
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
if (process.platform === 'win32') {
|
|
||||||
module.exports = require('@electron/internal/browser/api/auto-updater/auto-updater-win');
|
|
||||||
} else {
|
|
||||||
module.exports = require('@electron/internal/browser/api/auto-updater/auto-updater-native');
|
|
||||||
}
|
|
5
lib/browser/api/auto-updater.ts
Normal file
5
lib/browser/api/auto-updater.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
if (process.platform === 'win32') {
|
||||||
|
module.exports = require('./auto-updater/auto-updater-win');
|
||||||
|
} else {
|
||||||
|
module.exports = require('./auto-updater/auto-updater-native');
|
||||||
|
}
|
|
@ -1,10 +1,8 @@
|
||||||
'use strict';
|
import { EventEmitter } from 'events';
|
||||||
|
|
||||||
const EventEmitter = require('events').EventEmitter;
|
|
||||||
const { autoUpdater, AutoUpdater } = process._linkedBinding('electron_browser_auto_updater');
|
const { autoUpdater, AutoUpdater } = process._linkedBinding('electron_browser_auto_updater');
|
||||||
|
|
||||||
// AutoUpdater is an EventEmitter.
|
// AutoUpdater is an EventEmitter.
|
||||||
Object.setPrototypeOf(AutoUpdater.prototype, EventEmitter.prototype);
|
Object.setPrototypeOf(AutoUpdater.prototype, EventEmitter.prototype);
|
||||||
EventEmitter.call(autoUpdater);
|
EventEmitter.call(autoUpdater);
|
||||||
|
|
||||||
module.exports = autoUpdater;
|
export default autoUpdater;
|
|
@ -1,13 +1,14 @@
|
||||||
'use strict';
|
import { app } from 'electron';
|
||||||
|
import { EventEmitter } from 'events';
|
||||||
const { app } = require('electron');
|
import * as squirrelUpdate from './squirrel-update-win';
|
||||||
const { EventEmitter } = require('events');
|
|
||||||
const squirrelUpdate = require('@electron/internal/browser/api/auto-updater/squirrel-update-win');
|
|
||||||
|
|
||||||
class AutoUpdater extends EventEmitter {
|
class AutoUpdater extends EventEmitter {
|
||||||
|
updateAvailable: boolean = false;
|
||||||
|
updateURL: string | null = null;
|
||||||
|
|
||||||
quitAndInstall () {
|
quitAndInstall () {
|
||||||
if (!this.updateAvailable) {
|
if (!this.updateAvailable) {
|
||||||
return this.emitError('No update available, can\'t quit and install');
|
return this.emitError(new Error('No update available, can\'t quit and install'));
|
||||||
}
|
}
|
||||||
squirrelUpdate.processStart();
|
squirrelUpdate.processStart();
|
||||||
app.quit();
|
app.quit();
|
||||||
|
@ -17,8 +18,8 @@ class AutoUpdater extends EventEmitter {
|
||||||
return this.updateURL;
|
return this.updateURL;
|
||||||
}
|
}
|
||||||
|
|
||||||
setFeedURL (options) {
|
setFeedURL (options: { url: string } | string) {
|
||||||
let updateURL;
|
let updateURL: string;
|
||||||
if (typeof options === 'object') {
|
if (typeof options === 'object') {
|
||||||
if (typeof options.url === 'string') {
|
if (typeof options.url === 'string') {
|
||||||
updateURL = options.url;
|
updateURL = options.url;
|
||||||
|
@ -34,14 +35,15 @@ class AutoUpdater extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
checkForUpdates () {
|
checkForUpdates () {
|
||||||
if (!this.updateURL) {
|
const url = this.updateURL;
|
||||||
return this.emitError('Update URL is not set');
|
if (!url) {
|
||||||
|
return this.emitError(new Error('Update URL is not set'));
|
||||||
}
|
}
|
||||||
if (!squirrelUpdate.supported()) {
|
if (!squirrelUpdate.supported()) {
|
||||||
return this.emitError('Can not find Squirrel');
|
return this.emitError(new Error('Can not find Squirrel'));
|
||||||
}
|
}
|
||||||
this.emit('checking-for-update');
|
this.emit('checking-for-update');
|
||||||
squirrelUpdate.checkForUpdate(this.updateURL, (error, update) => {
|
squirrelUpdate.checkForUpdate(url, (error, update) => {
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
return this.emitError(error);
|
return this.emitError(error);
|
||||||
}
|
}
|
||||||
|
@ -50,7 +52,7 @@ class AutoUpdater extends EventEmitter {
|
||||||
}
|
}
|
||||||
this.updateAvailable = true;
|
this.updateAvailable = true;
|
||||||
this.emit('update-available');
|
this.emit('update-available');
|
||||||
squirrelUpdate.update(this.updateURL, (error) => {
|
squirrelUpdate.update(url, (error) => {
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
return this.emitError(error);
|
return this.emitError(error);
|
||||||
}
|
}
|
||||||
|
@ -66,9 +68,9 @@ class AutoUpdater extends EventEmitter {
|
||||||
|
|
||||||
// Private: Emit both error object and message, this is to keep compatibility
|
// Private: Emit both error object and message, this is to keep compatibility
|
||||||
// with Old APIs.
|
// with Old APIs.
|
||||||
emitError (message) {
|
emitError (error: Error) {
|
||||||
this.emit('error', new Error(message), message);
|
this.emit('error', error, error.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = new AutoUpdater();
|
export default new AutoUpdater();
|
|
@ -1,8 +1,6 @@
|
||||||
'use strict';
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
const fs = require('fs');
|
import { spawn, ChildProcessWithoutNullStreams } from 'child_process';
|
||||||
const path = require('path');
|
|
||||||
const spawn = require('child_process').spawn;
|
|
||||||
|
|
||||||
// i.e. my-app/app-0.1.13/
|
// i.e. my-app/app-0.1.13/
|
||||||
const appFolder = path.dirname(process.execPath);
|
const appFolder = path.dirname(process.execPath);
|
||||||
|
@ -10,15 +8,15 @@ const appFolder = path.dirname(process.execPath);
|
||||||
// i.e. my-app/Update.exe
|
// i.e. my-app/Update.exe
|
||||||
const updateExe = path.resolve(appFolder, '..', 'Update.exe');
|
const updateExe = path.resolve(appFolder, '..', 'Update.exe');
|
||||||
const exeName = path.basename(process.execPath);
|
const exeName = path.basename(process.execPath);
|
||||||
let spawnedArgs = [];
|
let spawnedArgs: string[] = [];
|
||||||
let spawnedProcess;
|
let spawnedProcess: ChildProcessWithoutNullStreams | undefined;
|
||||||
|
|
||||||
const isSameArgs = (args) => args.length === spawnedArgs.length && args.every((e, i) => e === spawnedArgs[i]);
|
const isSameArgs = (args: string[]) => args.length === spawnedArgs.length && args.every((e, i) => e === spawnedArgs[i]);
|
||||||
|
|
||||||
// Spawn a command and invoke the callback when it completes with an error
|
// Spawn a command and invoke the callback when it completes with an error
|
||||||
// and the output from standard out.
|
// and the output from standard out.
|
||||||
const spawnUpdate = function (args, detached, callback) {
|
const spawnUpdate = function (args: string[], detached: boolean, callback: Function) {
|
||||||
let error, errorEmitted, stderr, stdout;
|
let error: Error, errorEmitted: boolean, stderr: string, stdout: string;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Ensure we don't spawn multiple squirrel processes
|
// Ensure we don't spawn multiple squirrel processes
|
||||||
|
@ -79,13 +77,13 @@ const spawnUpdate = function (args, detached, callback) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Start an instance of the installed app.
|
// Start an instance of the installed app.
|
||||||
exports.processStart = function () {
|
export function processStart () {
|
||||||
return spawnUpdate(['--processStartAndWait', exeName], true, function () {});
|
return spawnUpdate(['--processStartAndWait', exeName], true, function () {});
|
||||||
};
|
}
|
||||||
|
|
||||||
// Download the releases specified by the URL and write new results to stdout.
|
// Download the releases specified by the URL and write new results to stdout.
|
||||||
exports.checkForUpdate = function (updateURL, callback) {
|
export function checkForUpdate (updateURL: string, callback: (error: Error | null, update?: any) => void) {
|
||||||
return spawnUpdate(['--checkForUpdate', updateURL], false, function (error, stdout) {
|
return spawnUpdate(['--checkForUpdate', updateURL], false, function (error: Error, stdout: string) {
|
||||||
let ref, ref1, update;
|
let ref, ref1, update;
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
return callback(error);
|
return callback(error);
|
||||||
|
@ -93,27 +91,27 @@ exports.checkForUpdate = function (updateURL, callback) {
|
||||||
try {
|
try {
|
||||||
// Last line of output is the JSON details about the releases
|
// Last line of output is the JSON details about the releases
|
||||||
const json = stdout.trim().split('\n').pop();
|
const json = stdout.trim().split('\n').pop();
|
||||||
update = (ref = JSON.parse(json)) != null ? (ref1 = ref.releasesToApply) != null ? typeof ref1.pop === 'function' ? ref1.pop() : undefined : undefined : undefined;
|
update = (ref = JSON.parse(json!)) != null ? (ref1 = ref.releasesToApply) != null ? typeof ref1.pop === 'function' ? ref1.pop() : undefined : undefined : undefined;
|
||||||
} catch {
|
} catch {
|
||||||
// Disabled for backwards compatibility:
|
// Disabled for backwards compatibility:
|
||||||
// eslint-disable-next-line standard/no-callback-literal
|
// eslint-disable-next-line standard/no-callback-literal
|
||||||
return callback(`Invalid result:\n${stdout}`);
|
return callback(new Error(`Invalid result:\n${stdout}`));
|
||||||
}
|
}
|
||||||
return callback(null, update);
|
return callback(null, update);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
// Update the application to the latest remote version specified by URL.
|
// Update the application to the latest remote version specified by URL.
|
||||||
exports.update = function (updateURL, callback) {
|
export function update (updateURL: string, callback: (error: Error) => void) {
|
||||||
return spawnUpdate(['--update', updateURL], false, callback);
|
return spawnUpdate(['--update', updateURL], false, callback);
|
||||||
};
|
}
|
||||||
|
|
||||||
// Is the Update.exe installed with the current application?
|
// Is the Update.exe installed with the current application?
|
||||||
exports.supported = function () {
|
export function supported () {
|
||||||
try {
|
try {
|
||||||
fs.accessSync(updateExe, fs.R_OK);
|
fs.accessSync(updateExe, fs.constants.R_OK);
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
}
|
Loading…
Reference in a new issue