2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2019-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
/* eslint-disable no-console */
|
2022-03-03 22:34:51 +00:00
|
|
|
import { createWriteStream } from 'fs';
|
2022-02-24 21:01:41 +00:00
|
|
|
import { pathExists } from 'fs-extra';
|
2021-11-29 12:05:51 +00:00
|
|
|
import { readdir, stat, writeFile, mkdir } from 'fs/promises';
|
2021-12-03 22:49:15 +00:00
|
|
|
import { promisify } from 'util';
|
|
|
|
import { execFile } from 'child_process';
|
2022-02-24 21:01:41 +00:00
|
|
|
import { join, normalize, extname } from 'path';
|
2019-03-28 17:09:26 +00:00
|
|
|
import { tmpdir } from 'os';
|
2021-08-19 22:56:29 +00:00
|
|
|
import { throttle } from 'lodash';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ParserConfiguration } from 'dashdash';
|
|
|
|
import { createParser } from 'dashdash';
|
2019-03-28 17:09:26 +00:00
|
|
|
import { FAILSAFE_SCHEMA, safeLoad } from 'js-yaml';
|
|
|
|
import { gt } from 'semver';
|
2021-08-27 20:21:42 +00:00
|
|
|
import config from 'config';
|
2021-10-26 19:15:33 +00:00
|
|
|
import got from 'got';
|
2019-03-28 17:09:26 +00:00
|
|
|
import { v4 as getGuid } from 'uuid';
|
|
|
|
import pify from 'pify';
|
|
|
|
import rimraf from 'rimraf';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { BrowserWindow } from 'electron';
|
|
|
|
import { app, ipcMain } from 'electron';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
import * as durations from '../util/durations';
|
2022-10-24 20:46:36 +00:00
|
|
|
import { getTempPath, getUpdateCachePath } from '../../app/attachments';
|
2021-08-19 22:56:29 +00:00
|
|
|
import { DialogType } from '../types/Dialogs';
|
2021-11-10 00:56:56 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2022-06-14 22:08:38 +00:00
|
|
|
import { isAlpha, isBeta, isStaging } from '../util/version';
|
2022-02-24 21:01:41 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2019-05-24 01:27:42 +00:00
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
import * as packageJson from '../../package.json';
|
2021-11-10 00:56:56 +00:00
|
|
|
import {
|
|
|
|
hexToBinary,
|
|
|
|
verifySignature,
|
|
|
|
getSignatureFileName,
|
|
|
|
} from './signature';
|
2020-08-27 18:08:37 +00:00
|
|
|
import { isPathInside } from '../util/isPathInside';
|
2021-11-10 00:56:56 +00:00
|
|
|
import type { SettingsChannel } from '../main/settingsChannel';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LoggerType } from '../types/Logging';
|
2022-02-24 21:01:41 +00:00
|
|
|
import { getGotOptions } from './got';
|
2022-03-28 19:05:44 +00:00
|
|
|
import { checkIntegrity, gracefulRename } from './util';
|
2022-02-24 21:01:41 +00:00
|
|
|
import type { PrepareDownloadResultType as DifferentialDownloadDataType } from './differential';
|
|
|
|
import {
|
|
|
|
prepareDownload as prepareDifferentialDownload,
|
|
|
|
download as downloadDifferentialData,
|
|
|
|
getBlockMapFileName,
|
2022-02-25 18:44:03 +00:00
|
|
|
isValidPreparedData as isValidDifferentialData,
|
2022-02-24 21:01:41 +00:00
|
|
|
} from './differential';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
const rimrafPromise = pify(rimraf);
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
const INTERVAL = 30 * durations.MINUTE;
|
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
type JSONUpdateSchema = {
|
|
|
|
version: string;
|
|
|
|
files: Array<{
|
|
|
|
url: string;
|
|
|
|
sha512: string;
|
|
|
|
size: string;
|
|
|
|
blockMapSize?: string;
|
|
|
|
}>;
|
|
|
|
path: string;
|
|
|
|
sha512: string;
|
|
|
|
releaseDate: string;
|
2022-03-29 22:46:18 +00:00
|
|
|
requireManualUpdate?: boolean;
|
2021-08-19 22:56:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type UpdateInformationType = {
|
|
|
|
fileName: string;
|
|
|
|
size: number;
|
|
|
|
version: string;
|
2022-02-24 21:01:41 +00:00
|
|
|
sha512: string;
|
|
|
|
differentialData: DifferentialDownloadDataType | undefined;
|
2021-08-19 22:56:29 +00:00
|
|
|
};
|
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
enum DownloadMode {
|
|
|
|
DifferentialOnly = 'DifferentialOnly',
|
|
|
|
FullOnly = 'FullOnly',
|
|
|
|
Automatic = 'Automatic',
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
type DownloadUpdateResultType = Readonly<{
|
|
|
|
updateFilePath: string;
|
|
|
|
signature: Buffer;
|
|
|
|
}>;
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
export abstract class Updater {
|
|
|
|
protected fileName: string | undefined;
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
protected version: string | undefined;
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
protected cachedDifferentialData: DifferentialDownloadDataType | undefined;
|
|
|
|
|
|
|
|
private throttledSendDownloadingUpdate: (downloadedSize: number) => void;
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
private activeDownload: Promise<boolean> | undefined;
|
|
|
|
|
2022-03-28 19:05:44 +00:00
|
|
|
private markedCannotUpdate = false;
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
constructor(
|
|
|
|
protected readonly logger: LoggerType,
|
|
|
|
private readonly settingsChannel: SettingsChannel,
|
|
|
|
protected readonly getMainWindow: () => BrowserWindow | undefined
|
2022-02-25 18:44:03 +00:00
|
|
|
) {
|
|
|
|
this.throttledSendDownloadingUpdate = throttle((downloadedSize: number) => {
|
|
|
|
const mainWindow = this.getMainWindow();
|
|
|
|
mainWindow?.webContents.send(
|
|
|
|
'show-update-dialog',
|
|
|
|
DialogType.Downloading,
|
|
|
|
{ downloadedSize }
|
|
|
|
);
|
|
|
|
}, 500);
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
//
|
|
|
|
// Public APIs
|
|
|
|
//
|
2021-08-19 22:56:29 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
public async force(): Promise<void> {
|
|
|
|
return this.checkForUpdatesMaybeInstall(true);
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
public async start(): Promise<void> {
|
|
|
|
this.logger.info('updater/start: starting checks...');
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
setInterval(async () => {
|
|
|
|
try {
|
|
|
|
await this.checkForUpdatesMaybeInstall();
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.error(`updater/start: ${Errors.toLogFormat(error)}`);
|
|
|
|
}
|
|
|
|
}, INTERVAL);
|
2019-08-02 21:11:10 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
await this.deletePreviousInstallers();
|
|
|
|
await this.checkForUpdatesMaybeInstall();
|
2019-08-02 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
//
|
|
|
|
// Abstract methods
|
|
|
|
//
|
|
|
|
|
|
|
|
protected abstract deletePreviousInstallers(): Promise<void>;
|
|
|
|
|
|
|
|
protected abstract installUpdate(updateFilePath: string): Promise<void>;
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
//
|
|
|
|
// Protected methods
|
|
|
|
//
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
protected setUpdateListener(
|
|
|
|
performUpdateCallback: () => Promise<void>
|
|
|
|
): void {
|
|
|
|
ipcMain.removeHandler('start-update');
|
|
|
|
ipcMain.handleOnce('start-update', performUpdateCallback);
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2022-03-28 19:05:44 +00:00
|
|
|
protected markCannotUpdate(
|
|
|
|
error: Error,
|
|
|
|
dialogType = DialogType.Cannot_Update
|
|
|
|
): void {
|
|
|
|
if (this.markedCannotUpdate) {
|
|
|
|
this.logger.warn(
|
|
|
|
'updater/markCannotUpdate: already marked',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.markedCannotUpdate = true;
|
|
|
|
|
|
|
|
this.logger.error(
|
|
|
|
'updater/markCannotUpdate: marking due to error: ' +
|
|
|
|
`${Errors.toLogFormat(error)}, ` +
|
|
|
|
`dialogType: ${dialogType}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const mainWindow = this.getMainWindow();
|
|
|
|
mainWindow?.webContents.send('show-update-dialog', dialogType);
|
2022-04-27 23:02:41 +00:00
|
|
|
|
|
|
|
this.setUpdateListener(async () => {
|
|
|
|
this.logger.info('updater/markCannotUpdate: retrying after user action');
|
|
|
|
|
|
|
|
this.markedCannotUpdate = false;
|
|
|
|
await this.checkForUpdatesMaybeInstall();
|
|
|
|
});
|
2022-03-28 19:05:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
//
|
|
|
|
// Private methods
|
|
|
|
//
|
|
|
|
|
|
|
|
private async downloadAndInstall(
|
2022-02-24 21:01:41 +00:00
|
|
|
updateInfo: UpdateInformationType,
|
2022-02-25 18:44:03 +00:00
|
|
|
mode: DownloadMode
|
2022-03-03 22:34:51 +00:00
|
|
|
): Promise<boolean> {
|
|
|
|
if (this.activeDownload) {
|
|
|
|
return this.activeDownload;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.activeDownload = this.doDownloadAndInstall(updateInfo, mode);
|
|
|
|
|
|
|
|
return await this.activeDownload;
|
|
|
|
} finally {
|
|
|
|
this.activeDownload = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async doDownloadAndInstall(
|
|
|
|
updateInfo: UpdateInformationType,
|
|
|
|
mode: DownloadMode
|
2022-02-25 18:44:03 +00:00
|
|
|
): Promise<boolean> {
|
2021-11-10 00:56:56 +00:00
|
|
|
const { logger } = this;
|
2022-02-24 21:01:41 +00:00
|
|
|
|
|
|
|
const { fileName: newFileName, version: newVersion } = updateInfo;
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
try {
|
|
|
|
const oldVersion = this.version;
|
|
|
|
this.version = newVersion;
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
let downloadResult: DownloadUpdateResultType | undefined;
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
try {
|
2022-03-03 22:34:51 +00:00
|
|
|
downloadResult = await this.downloadUpdate(updateInfo, mode);
|
2021-11-10 00:56:56 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Restore state in case of download error
|
|
|
|
this.version = oldVersion;
|
2022-03-03 22:34:51 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
throw error;
|
|
|
|
}
|
2019-08-02 21:11:10 +00:00
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
if (!downloadResult) {
|
2022-02-25 18:44:03 +00:00
|
|
|
logger.warn('downloadAndInstall: no update was downloaded');
|
2022-02-25 19:02:09 +00:00
|
|
|
strictAssert(
|
|
|
|
mode !== DownloadMode.Automatic && mode !== DownloadMode.FullOnly,
|
|
|
|
'Automatic and full mode downloads are guaranteed to happen or error'
|
|
|
|
);
|
2022-02-25 18:44:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
const { updateFilePath, signature } = downloadResult;
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
const publicKey = hexToBinary(config.get('updatesPublicKey'));
|
|
|
|
const verified = await verifySignature(
|
2022-02-24 21:01:41 +00:00
|
|
|
updateFilePath,
|
2021-11-10 00:56:56 +00:00
|
|
|
this.version,
|
2022-03-03 22:34:51 +00:00
|
|
|
signature,
|
2021-11-10 00:56:56 +00:00
|
|
|
publicKey
|
|
|
|
);
|
|
|
|
if (!verified) {
|
|
|
|
// Note: We don't delete the cache here, because we don't want to continually
|
|
|
|
// re-download the broken release. We will download it only once per launch.
|
|
|
|
throw new Error(
|
|
|
|
'Downloaded update did not pass signature verification ' +
|
2022-02-24 21:01:41 +00:00
|
|
|
`(version: '${this.version}'; fileName: '${newFileName}')`
|
2021-11-10 00:56:56 +00:00
|
|
|
);
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
await this.installUpdate(updateFilePath);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
const mainWindow = this.getMainWindow();
|
2021-08-19 22:56:29 +00:00
|
|
|
if (mainWindow) {
|
2021-11-10 00:56:56 +00:00
|
|
|
mainWindow.webContents.send('show-update-dialog', DialogType.Update, {
|
|
|
|
version: this.version,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
logger.warn(
|
|
|
|
'downloadAndInstall: no mainWindow, cannot show update dialog'
|
|
|
|
);
|
|
|
|
}
|
2022-02-25 18:44:03 +00:00
|
|
|
|
|
|
|
return true;
|
2021-11-10 00:56:56 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(`downloadAndInstall: ${Errors.toLogFormat(error)}`);
|
2022-03-28 19:05:44 +00:00
|
|
|
this.markCannotUpdate(error);
|
2022-02-25 18:44:03 +00:00
|
|
|
throw error;
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async checkForUpdatesMaybeInstall(force = false): Promise<void> {
|
|
|
|
const { logger } = this;
|
2021-08-19 22:56:29 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
logger.info('checkForUpdatesMaybeInstall: checking for update...');
|
2022-02-25 18:44:03 +00:00
|
|
|
const updateInfo = await this.checkForUpdates(force);
|
|
|
|
if (!updateInfo) {
|
2021-11-10 00:56:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
const { version: newVersion } = updateInfo;
|
2021-11-10 00:56:56 +00:00
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
if (!force && this.version && !gt(newVersion, this.version)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const autoDownloadUpdates = await this.getAutoDownloadUpdateSetting();
|
|
|
|
if (autoDownloadUpdates) {
|
|
|
|
await this.downloadAndInstall(updateInfo, DownloadMode.Automatic);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mode = DownloadMode.FullOnly;
|
|
|
|
if (updateInfo.differentialData) {
|
|
|
|
mode = DownloadMode.DifferentialOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.offerUpdate(updateInfo, mode, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async offerUpdate(
|
|
|
|
updateInfo: UpdateInformationType,
|
|
|
|
mode: DownloadMode,
|
|
|
|
attempt: number
|
|
|
|
): Promise<void> {
|
|
|
|
const { logger } = this;
|
|
|
|
|
|
|
|
this.setUpdateListener(async () => {
|
|
|
|
logger.info('offerUpdate: have not downloaded update, going to download');
|
|
|
|
|
|
|
|
const didDownload = await this.downloadAndInstall(updateInfo, mode);
|
|
|
|
if (!didDownload && mode === DownloadMode.DifferentialOnly) {
|
|
|
|
this.logger.warn(
|
|
|
|
'offerUpdate: Failed to download differential update, offering full'
|
|
|
|
);
|
|
|
|
|
|
|
|
return this.offerUpdate(updateInfo, DownloadMode.FullOnly, attempt + 1);
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
2022-02-25 18:44:03 +00:00
|
|
|
|
|
|
|
strictAssert(didDownload, 'FullOnly must always download update');
|
|
|
|
});
|
|
|
|
|
|
|
|
const mainWindow = this.getMainWindow();
|
|
|
|
if (!mainWindow) {
|
|
|
|
logger.warn('offerUpdate: no mainWindow, cannot show update dialog');
|
|
|
|
return;
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
2022-02-25 18:44:03 +00:00
|
|
|
|
|
|
|
let downloadSize: number;
|
|
|
|
if (mode === DownloadMode.DifferentialOnly) {
|
|
|
|
strictAssert(
|
|
|
|
updateInfo.differentialData,
|
|
|
|
'Must have differential data in DifferentialOnly mode'
|
|
|
|
);
|
|
|
|
downloadSize = updateInfo.differentialData.downloadSize;
|
|
|
|
} else {
|
|
|
|
downloadSize = updateInfo.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(`offerUpdate: offering ${mode} update`);
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
'show-update-dialog',
|
|
|
|
attempt === 0 ? DialogType.DownloadReady : DialogType.FullDownloadReady,
|
|
|
|
{
|
|
|
|
downloadSize,
|
|
|
|
downloadMode: mode,
|
|
|
|
version: updateInfo.version,
|
|
|
|
}
|
|
|
|
);
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async checkForUpdates(
|
|
|
|
forceUpdate = false
|
2022-02-24 21:01:41 +00:00
|
|
|
): Promise<UpdateInformationType | undefined> {
|
2021-11-10 00:56:56 +00:00
|
|
|
const yaml = await getUpdateYaml();
|
|
|
|
const parsedYaml = parseYaml(yaml);
|
2022-03-29 22:46:18 +00:00
|
|
|
|
|
|
|
if (parsedYaml.requireManualUpdate) {
|
|
|
|
this.logger.warn('checkForUpdates: manual update required');
|
|
|
|
this.markCannotUpdate(
|
2022-04-06 18:27:17 +00:00
|
|
|
new Error('yaml file has requireManualUpdate flag'),
|
|
|
|
DialogType.Cannot_Update_Require_Manual
|
2022-03-29 22:46:18 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
const version = getVersion(parsedYaml);
|
2021-08-19 22:56:29 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
if (!version) {
|
|
|
|
this.logger.warn(
|
|
|
|
'checkForUpdates: no version extracted from downloaded yaml'
|
|
|
|
);
|
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
return;
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
if (!forceUpdate && !isVersionNewer(version)) {
|
2021-11-10 00:56:56 +00:00
|
|
|
this.logger.info(
|
2022-02-24 21:01:41 +00:00
|
|
|
`checkForUpdates: ${version} is not newer than ${packageJson.version}; ` +
|
|
|
|
'no new update available'
|
2021-12-03 22:49:15 +00:00
|
|
|
);
|
2021-11-10 00:56:56 +00:00
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
return;
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.info(
|
2022-02-24 21:01:41 +00:00
|
|
|
`checkForUpdates: found newer version ${version} ` +
|
|
|
|
`forceUpdate=${forceUpdate}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const fileName = getUpdateFileName(
|
|
|
|
parsedYaml,
|
|
|
|
process.platform,
|
|
|
|
await this.getArch()
|
|
|
|
);
|
|
|
|
|
|
|
|
const sha512 = getSHA512(parsedYaml, fileName);
|
|
|
|
strictAssert(sha512 !== undefined, 'Missing required hash');
|
|
|
|
|
|
|
|
const latestInstaller = await this.getLatestCachedInstaller(
|
|
|
|
extname(fileName)
|
2021-11-10 00:56:56 +00:00
|
|
|
);
|
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
let differentialData: DifferentialDownloadDataType | undefined;
|
|
|
|
if (latestInstaller) {
|
|
|
|
this.logger.info(
|
|
|
|
`checkForUpdates: Found local installer ${latestInstaller}`
|
|
|
|
);
|
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
const diffOptions = {
|
|
|
|
oldFile: latestInstaller,
|
|
|
|
newUrl: `${getUpdatesBase()}/${fileName}`,
|
|
|
|
sha512,
|
|
|
|
};
|
2022-02-24 21:01:41 +00:00
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
if (
|
|
|
|
this.cachedDifferentialData &&
|
|
|
|
isValidDifferentialData(this.cachedDifferentialData, diffOptions)
|
|
|
|
) {
|
|
|
|
this.logger.info('checkForUpdates: using cached differential data');
|
|
|
|
|
|
|
|
differentialData = this.cachedDifferentialData;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
differentialData = await prepareDifferentialDownload(diffOptions);
|
|
|
|
|
|
|
|
this.cachedDifferentialData = differentialData;
|
|
|
|
|
|
|
|
this.logger.info(
|
|
|
|
'checkForUpdates: differential download size',
|
|
|
|
differentialData.downloadSize
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.error(
|
|
|
|
'checkForUpdates: Failed to prepare differential update',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
this.cachedDifferentialData = undefined;
|
|
|
|
}
|
2022-02-24 21:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
fileName,
|
|
|
|
size: getSize(parsedYaml, fileName),
|
|
|
|
version,
|
|
|
|
sha512,
|
|
|
|
differentialData,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getLatestCachedInstaller(
|
|
|
|
extension: string
|
|
|
|
): Promise<string | undefined> {
|
|
|
|
const cacheDir = await createUpdateCacheDirIfNeeded();
|
|
|
|
const oldFiles = (await readdir(cacheDir)).map(fileName => {
|
|
|
|
return join(cacheDir, fileName);
|
|
|
|
});
|
|
|
|
|
|
|
|
return oldFiles.find(fileName => extname(fileName) === extension);
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async downloadUpdate(
|
2022-02-24 21:01:41 +00:00
|
|
|
{ fileName, sha512, differentialData }: UpdateInformationType,
|
2022-02-25 18:44:03 +00:00
|
|
|
mode: DownloadMode
|
2022-03-03 22:34:51 +00:00
|
|
|
): Promise<DownloadUpdateResultType | undefined> {
|
2021-11-10 00:56:56 +00:00
|
|
|
const baseUrl = getUpdatesBase();
|
|
|
|
const updateFileUrl = `${baseUrl}/${fileName}`;
|
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
const updateOnProgress = mode !== DownloadMode.Automatic;
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
const signatureFileName = getSignatureFileName(fileName);
|
2022-02-24 21:01:41 +00:00
|
|
|
const blockMapFileName = getBlockMapFileName(fileName);
|
2021-11-10 00:56:56 +00:00
|
|
|
const signatureUrl = `${baseUrl}/${signatureFileName}`;
|
2022-02-24 21:01:41 +00:00
|
|
|
const blockMapUrl = `${baseUrl}/${blockMapFileName}`;
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
let cacheDir = await createUpdateCacheDirIfNeeded();
|
2022-02-24 21:01:41 +00:00
|
|
|
const targetUpdatePath = join(cacheDir, fileName);
|
2022-03-03 22:34:51 +00:00
|
|
|
|
|
|
|
const tempDir = await createTempDir();
|
|
|
|
|
|
|
|
const tempUpdatePath = join(tempDir, fileName);
|
|
|
|
const tempBlockMapPath = join(tempDir, blockMapFileName);
|
2021-11-10 00:56:56 +00:00
|
|
|
|
2022-03-28 19:05:44 +00:00
|
|
|
// If true - we will attempt to install from a temporary directory.
|
|
|
|
let tempPathFailover = false;
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
try {
|
2022-02-24 21:01:41 +00:00
|
|
|
validatePath(cacheDir, targetUpdatePath);
|
2022-03-03 22:34:51 +00:00
|
|
|
|
|
|
|
validatePath(tempDir, tempUpdatePath);
|
|
|
|
validatePath(tempDir, tempBlockMapPath);
|
2021-11-10 00:56:56 +00:00
|
|
|
|
|
|
|
this.logger.info(`downloadUpdate: Downloading signature ${signatureUrl}`);
|
2022-03-03 22:34:51 +00:00
|
|
|
const signature = Buffer.from(
|
|
|
|
await got(signatureUrl, getGotOptions()).text(),
|
|
|
|
'hex'
|
|
|
|
);
|
2021-11-10 00:56:56 +00:00
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
if (differentialData) {
|
|
|
|
this.logger.info(`downloadUpdate: Saving blockmap ${blockMapUrl}`);
|
2022-03-03 22:34:51 +00:00
|
|
|
await writeFile(tempBlockMapPath, differentialData.newBlockMap);
|
2022-02-25 18:44:03 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
this.logger.info(
|
|
|
|
`downloadUpdate: Downloading blockmap ${blockMapUrl}`
|
|
|
|
);
|
|
|
|
const blockMap = await got(blockMapUrl, getGotOptions()).buffer();
|
2022-03-03 22:34:51 +00:00
|
|
|
await writeFile(tempBlockMapPath, blockMap);
|
2022-02-25 18:44:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(
|
|
|
|
'downloadUpdate: Failed to download blockmap, continuing',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
2022-02-24 21:01:41 +00:00
|
|
|
}
|
2021-11-10 00:56:56 +00:00
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
let gotUpdate = false;
|
|
|
|
if (!gotUpdate && (await pathExists(targetUpdatePath))) {
|
|
|
|
const checkResult = await checkIntegrity(targetUpdatePath, sha512);
|
|
|
|
if (checkResult.ok) {
|
|
|
|
this.logger.info(
|
|
|
|
`downloadUpdate: Not downloading update ${updateFileUrl}, ` +
|
|
|
|
'local file has the same hash'
|
|
|
|
);
|
2022-03-03 22:34:51 +00:00
|
|
|
|
|
|
|
// Move file into downloads directory
|
|
|
|
try {
|
2022-03-28 19:05:44 +00:00
|
|
|
await gracefulRename(this.logger, targetUpdatePath, tempUpdatePath);
|
2022-03-03 22:34:51 +00:00
|
|
|
gotUpdate = true;
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.error(
|
|
|
|
'downloadUpdate: failed to move already downloaded file',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
2022-02-24 21:01:41 +00:00
|
|
|
} else {
|
|
|
|
this.logger.error(
|
|
|
|
'downloadUpdate: integrity check failure',
|
|
|
|
checkResult.error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
const isDifferentialEnabled =
|
|
|
|
differentialData && mode !== DownloadMode.FullOnly;
|
|
|
|
if (!gotUpdate && isDifferentialEnabled) {
|
2022-02-24 21:01:41 +00:00
|
|
|
this.logger.info(
|
|
|
|
`downloadUpdate: Downloading differential update ${updateFileUrl}`
|
|
|
|
);
|
2021-11-10 00:56:56 +00:00
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
try {
|
2022-03-03 22:34:51 +00:00
|
|
|
await downloadDifferentialData(tempUpdatePath, differentialData, {
|
2022-03-02 19:48:07 +00:00
|
|
|
statusCallback: updateOnProgress
|
|
|
|
? this.throttledSendDownloadingUpdate
|
|
|
|
: undefined,
|
|
|
|
logger: this.logger,
|
|
|
|
});
|
2021-11-10 00:56:56 +00:00
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
gotUpdate = true;
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.error(
|
|
|
|
'downloadUpdate: Failed to apply differential update',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 22:56:29 +00:00
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
const isFullEnabled = mode !== DownloadMode.DifferentialOnly;
|
|
|
|
if (!gotUpdate && isFullEnabled) {
|
2022-02-24 21:01:41 +00:00
|
|
|
this.logger.info(
|
|
|
|
`downloadUpdate: Downloading full update ${updateFileUrl}`
|
|
|
|
);
|
2022-03-03 22:34:51 +00:00
|
|
|
|
|
|
|
// We could have failed to update differentially due to low free disk
|
|
|
|
// space. Remove all cached updates since we are doing a full download
|
|
|
|
// anyway.
|
|
|
|
await rimrafPromise(cacheDir);
|
|
|
|
cacheDir = await createUpdateCacheDirIfNeeded();
|
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
await this.downloadAndReport(
|
|
|
|
updateFileUrl,
|
2022-03-03 22:34:51 +00:00
|
|
|
tempUpdatePath,
|
2022-02-24 21:01:41 +00:00
|
|
|
updateOnProgress
|
|
|
|
);
|
|
|
|
gotUpdate = true;
|
|
|
|
}
|
2022-02-25 18:44:03 +00:00
|
|
|
|
|
|
|
if (!gotUpdate) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2022-03-04 19:59:47 +00:00
|
|
|
this.logger.info(
|
|
|
|
'downloadUpdate: Downloaded update, moving into cache dir'
|
|
|
|
);
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
// Backup old files
|
2022-03-04 19:59:47 +00:00
|
|
|
const restoreDir = await getTempDir();
|
2022-03-28 19:05:44 +00:00
|
|
|
await gracefulRename(this.logger, cacheDir, restoreDir);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
// Move the files into the final position
|
2022-02-24 21:01:41 +00:00
|
|
|
try {
|
2022-03-28 19:05:44 +00:00
|
|
|
await gracefulRename(this.logger, tempDir, cacheDir);
|
2022-03-03 22:34:51 +00:00
|
|
|
} catch (error) {
|
2022-03-28 19:05:44 +00:00
|
|
|
try {
|
|
|
|
// Attempt to restore old files
|
|
|
|
await gracefulRename(this.logger, restoreDir, cacheDir);
|
|
|
|
} catch (restoreError) {
|
|
|
|
this.logger.warn(
|
|
|
|
'downloadUpdate: Failed to restore from backup folder, ignoring',
|
|
|
|
Errors.toLogFormat(restoreError)
|
|
|
|
);
|
|
|
|
}
|
2022-03-03 22:34:51 +00:00
|
|
|
|
2022-03-28 19:05:44 +00:00
|
|
|
this.logger.warn(
|
|
|
|
'downloadUpdate: running update from a temporary folder due to error',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
tempPathFailover = true;
|
|
|
|
return { updateFilePath: tempUpdatePath, signature };
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
2022-03-03 22:34:51 +00:00
|
|
|
|
2022-03-04 19:59:47 +00:00
|
|
|
try {
|
|
|
|
await deleteTempDir(restoreDir);
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(
|
|
|
|
'downloadUpdate: Failed to remove backup folder, ignoring',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
return { updateFilePath: targetUpdatePath, signature };
|
|
|
|
} finally {
|
2022-03-28 19:05:44 +00:00
|
|
|
if (!tempPathFailover) {
|
|
|
|
await deleteTempDir(tempDir);
|
|
|
|
}
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
private async downloadAndReport(
|
|
|
|
updateFileUrl: string,
|
|
|
|
targetUpdatePath: string,
|
|
|
|
updateOnProgress = false
|
|
|
|
): Promise<void> {
|
|
|
|
const downloadStream = got.stream(updateFileUrl, getGotOptions());
|
|
|
|
const writeStream = createWriteStream(targetUpdatePath);
|
|
|
|
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
2022-02-25 18:44:03 +00:00
|
|
|
if (updateOnProgress) {
|
2022-02-24 21:01:41 +00:00
|
|
|
let downloadedSize = 0;
|
|
|
|
|
|
|
|
downloadStream.on('data', data => {
|
|
|
|
downloadedSize += data.length;
|
2022-02-25 18:44:03 +00:00
|
|
|
this.throttledSendDownloadingUpdate(downloadedSize);
|
2022-02-24 21:01:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadStream.on('error', error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
downloadStream.on('end', () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
writeStream.on('error', error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
downloadStream.pipe(writeStream);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
private async getAutoDownloadUpdateSetting(): Promise<boolean> {
|
|
|
|
try {
|
|
|
|
return await this.settingsChannel.getSettingFromMainWindow(
|
|
|
|
'autoDownloadUpdate'
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(
|
|
|
|
'getAutoDownloadUpdateSetting: Failed to fetch, returning false',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-12-03 22:49:15 +00:00
|
|
|
private async getArch(): Promise<typeof process.arch> {
|
|
|
|
if (process.platform !== 'darwin' || process.arch === 'arm64') {
|
|
|
|
return process.arch;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// We might be running under Rosetta
|
2021-12-06 18:10:15 +00:00
|
|
|
const flag = 'sysctl.proc_translated';
|
|
|
|
const { stdout } = await promisify(execFile)('sysctl', ['-i', flag]);
|
|
|
|
|
|
|
|
if (stdout.includes(`${flag}: 1`)) {
|
2021-12-03 22:49:15 +00:00
|
|
|
this.logger.info('updater: running under Rosetta');
|
|
|
|
return 'arm64';
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(
|
2021-12-06 18:10:15 +00:00
|
|
|
`updater: Rosetta detection failed with ${Errors.toLogFormat(error)}`
|
2021-12-03 22:49:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.info('updater: not running under Rosetta');
|
|
|
|
return process.arch;
|
|
|
|
}
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function validatePath(basePath: string, targetPath: string): void {
|
|
|
|
const normalized = normalize(targetPath);
|
|
|
|
|
|
|
|
if (!isPathInside(normalized, basePath)) {
|
|
|
|
throw new Error(
|
|
|
|
`validatePath: Path ${normalized} is not under base path ${basePath}`
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
export function getUpdateCheckUrl(): string {
|
|
|
|
return `${getUpdatesBase()}/${getUpdatesFileName()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUpdatesBase(): string {
|
2021-08-27 20:21:42 +00:00
|
|
|
return config.get('updatesUrl');
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getUpdatesFileName(): string {
|
2021-08-06 21:21:01 +00:00
|
|
|
const prefix = getChannel();
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-12-03 22:49:15 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2019-03-28 17:09:26 +00:00
|
|
|
return `${prefix}-mac.yml`;
|
|
|
|
}
|
2020-09-16 19:31:05 +00:00
|
|
|
|
|
|
|
return `${prefix}.yml`;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 21:21:01 +00:00
|
|
|
function getChannel(): string {
|
|
|
|
const { version } = packageJson;
|
|
|
|
|
2022-06-14 22:08:38 +00:00
|
|
|
if (isStaging(version)) {
|
|
|
|
return 'staging';
|
|
|
|
}
|
2021-08-06 21:21:01 +00:00
|
|
|
if (isAlpha(version)) {
|
|
|
|
return 'alpha';
|
|
|
|
}
|
|
|
|
if (isBeta(version)) {
|
|
|
|
return 'beta';
|
|
|
|
}
|
|
|
|
return 'latest';
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isVersionNewer(newVersion: string): boolean {
|
|
|
|
const { version } = packageJson;
|
|
|
|
|
|
|
|
return gt(newVersion, version);
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
export function getVersion(info: JSONUpdateSchema): string | null {
|
2020-09-16 19:31:05 +00:00
|
|
|
return info && info.version;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
const validFile = /^[A-Za-z0-9.-]+$/;
|
|
|
|
export function isUpdateFileNameValid(name: string): boolean {
|
2019-08-02 21:11:10 +00:00
|
|
|
return validFile.test(name);
|
|
|
|
}
|
|
|
|
|
2021-12-03 22:49:15 +00:00
|
|
|
export function getUpdateFileName(
|
|
|
|
info: JSONUpdateSchema,
|
|
|
|
platform: typeof process.platform,
|
|
|
|
arch: typeof process.arch
|
|
|
|
): string {
|
2019-08-02 21:11:10 +00:00
|
|
|
if (!info || !info.path) {
|
|
|
|
throw new Error('getUpdateFileName: No path present in YAML file');
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 22:49:15 +00:00
|
|
|
let path: string | undefined;
|
|
|
|
if (platform === 'darwin') {
|
|
|
|
const { files } = info;
|
|
|
|
|
|
|
|
const candidates = files.filter(
|
|
|
|
({ url }) => url.includes(arch) && url.endsWith('.zip')
|
|
|
|
);
|
|
|
|
|
|
|
|
if (candidates.length === 1) {
|
|
|
|
path = candidates[0].url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path ?? info.path;
|
|
|
|
|
2019-08-02 21:11:10 +00:00
|
|
|
if (!isUpdateFileNameValid(path)) {
|
|
|
|
throw new Error(
|
|
|
|
`getUpdateFileName: Path '${path}' contains invalid characters`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
function getSHA512(
|
|
|
|
info: JSONUpdateSchema,
|
|
|
|
fileName: string
|
|
|
|
): string | undefined {
|
|
|
|
if (!info || !info.files) {
|
|
|
|
throw new Error('getSHA512: No files present in YAML file');
|
|
|
|
}
|
|
|
|
|
|
|
|
const foundFile = info.files.find(file => file.url === fileName);
|
|
|
|
|
|
|
|
return foundFile?.sha512;
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
function getSize(info: JSONUpdateSchema, fileName: string): number {
|
|
|
|
if (!info || !info.files) {
|
2022-02-24 21:01:41 +00:00
|
|
|
throw new Error('getSize: No files present in YAML file');
|
2021-08-19 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const foundFile = info.files.find(file => file.url === fileName);
|
|
|
|
|
|
|
|
return Number(foundFile?.size) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseYaml(yaml: string): JSONUpdateSchema {
|
2019-03-28 17:09:26 +00:00
|
|
|
return safeLoad(yaml, { schema: FAILSAFE_SCHEMA, json: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getUpdateYaml(): Promise<string> {
|
|
|
|
const targetUrl = getUpdateCheckUrl();
|
2021-10-06 16:25:22 +00:00
|
|
|
const body = await got(targetUrl, getGotOptions()).text();
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
if (!body) {
|
|
|
|
throw new Error('Got unexpected response back from update check');
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:25:22 +00:00
|
|
|
return body;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getBaseTempDir() {
|
|
|
|
// We only use tmpdir() when this code is run outside of an Electron app (as in: tests)
|
2019-05-24 01:27:42 +00:00
|
|
|
return app ? getTempPath(app.getPath('userData')) : tmpdir();
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
export async function createTempDir(): Promise<string> {
|
2022-03-04 19:59:47 +00:00
|
|
|
const targetDir = await getTempDir();
|
|
|
|
|
2021-11-29 12:05:51 +00:00
|
|
|
await mkdir(targetDir, { recursive: true });
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
return targetDir;
|
|
|
|
}
|
|
|
|
|
2022-03-04 19:59:47 +00:00
|
|
|
export async function getTempDir(): Promise<string> {
|
|
|
|
const baseTempDir = getBaseTempDir();
|
|
|
|
const uniqueName = getGuid();
|
|
|
|
|
|
|
|
// Create parent folder if not already present
|
|
|
|
if (!(await pathExists(baseTempDir))) {
|
2021-11-29 12:05:51 +00:00
|
|
|
await mkdir(baseTempDir, { recursive: true });
|
2022-03-04 19:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return join(baseTempDir, uniqueName);
|
|
|
|
}
|
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
function getUpdateCacheDir() {
|
|
|
|
// We only use tmpdir() when this code is run outside of an Electron app (as in: tests)
|
|
|
|
return app ? getUpdateCachePath(app.getPath('userData')) : tmpdir();
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createUpdateCacheDirIfNeeded(): Promise<string> {
|
|
|
|
const targetDir = getUpdateCacheDir();
|
2021-11-29 12:05:51 +00:00
|
|
|
await mkdir(targetDir, { recursive: true });
|
2022-02-24 21:01:41 +00:00
|
|
|
|
|
|
|
return targetDir;
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
export async function deleteTempDir(targetDir: string): Promise<void> {
|
2022-03-03 22:34:51 +00:00
|
|
|
if (await pathExists(targetDir)) {
|
|
|
|
const pathInfo = await stat(targetDir);
|
|
|
|
if (!pathInfo.isDirectory()) {
|
|
|
|
throw new Error(
|
|
|
|
`deleteTempDir: Cannot delete path '${targetDir}' because it is not a directory`
|
|
|
|
);
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const baseTempDir = getBaseTempDir();
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(targetDir, baseTempDir)) {
|
2019-03-28 17:09:26 +00:00
|
|
|
throw new Error(
|
|
|
|
`deleteTempDir: Cannot delete path '${targetDir}' since it is not within base temp dir`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await rimrafPromise(targetDir);
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
export function getCliOptions<T>(options: ParserConfiguration['options']): T {
|
2019-03-28 17:09:26 +00:00
|
|
|
const parser = createParser({ options });
|
|
|
|
const cliOptions = parser.parse(process.argv);
|
|
|
|
|
|
|
|
if (cliOptions.help) {
|
|
|
|
const help = parser.help().trimRight();
|
|
|
|
console.log(help);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
return cliOptions as unknown as T;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|