signal-desktop/ts/updater/generateSignature.ts

98 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2019 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2020-09-16 19:31:05 +00:00
/* eslint-disable no-console */
import { join, resolve } from 'path';
import { readdir as readdirCallback } from 'fs';
import pify from 'pify';
import * as Errors from '../types/errors';
import { getCliOptions } from './common';
import { writeSignature } from './signature';
import * as packageJson from '../../package.json';
const readdir = pify(readdirCallback);
const OPTIONS = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Print this help and exit.',
},
{
names: ['private', 'p'],
type: 'string',
help: 'Path to private key file (default: ./private.key)',
default: 'private.key',
},
{
names: ['update', 'u'],
type: 'string',
help: 'Path to the update package (default: the .exe or .zip in ./release)',
},
{
names: ['version', 'v'],
type: 'string',
help: `Version number of this package (default: ${packageJson.version})`,
default: packageJson.version,
},
];
type OptionsType = {
private: string;
update: string;
version: string;
};
const cliOptions = getCliOptions<OptionsType>(OPTIONS);
go(cliOptions).catch(error => {
console.error('Something went wrong!', Errors.toLogFormat(error));
});
async function go(options: OptionsType) {
const { private: privateKeyPath, version } = options;
2021-12-03 22:49:15 +00:00
let updatePaths: Array<string>;
if (options.update) {
updatePaths = [options.update];
} else {
updatePaths = await findUpdatePaths();
}
2021-12-03 22:49:15 +00:00
await Promise.all(
updatePaths.map(async updatePath => {
console.log('Signing with...');
console.log(` version: ${version}`);
console.log(` update file: ${updatePath}`);
console.log(` private key file: ${privateKeyPath}`);
2021-12-03 22:49:15 +00:00
await writeSignature(updatePath, version, privateKeyPath);
})
);
}
const IS_EXE = /\.exe$/;
const IS_ZIP = /\.zip$/;
2021-12-03 22:49:15 +00:00
async function findUpdatePaths(): Promise<Array<string>> {
const releaseDir = resolve('release');
const files: Array<string> = await readdir(releaseDir);
const max = files.length;
2021-12-03 22:49:15 +00:00
const results = new Array<string>();
for (let i = 0; i < max; i += 1) {
const file = files[i];
const fullPath = join(releaseDir, file);
if (IS_EXE.test(file) || IS_ZIP.test(file)) {
2021-12-03 22:49:15 +00:00
results.push(fullPath);
}
}
2021-12-03 22:49:15 +00:00
if (results.length === 0) {
throw new Error("No suitable file found in 'release' folder!");
}
return results;
}