/
bin-utils.ts
100 lines (88 loc) · 3.3 KB
/
bin-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Wire
* Copyright (C) 2019 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
import {LogFactory, Logger} from '@wireapp/commons/dist/commonjs/LogFactory';
import {exec} from 'child_process';
import commander from 'commander';
import fs from 'fs-extra';
import os from 'os';
import path from 'path';
import {promisify} from 'util';
interface BackupResult {
backupPaths: string[];
originalPaths: string[];
tempDir: string;
}
const createTempDir = () => fs.mkdtemp(path.join(os.tmpdir(), 'wire-build-'));
export async function backupFiles(filePaths: string[]): Promise<BackupResult> {
const tempDir = await createTempDir();
const backupPaths = await Promise.all(
filePaths.map(async filePath => {
const backupPath = path.join(tempDir, path.basename(filePath));
await fs.copy(path.resolve(filePath), backupPath);
return backupPath;
}),
);
return {backupPaths, originalPaths: filePaths, tempDir};
}
export async function restoreFiles({originalPaths, backupPaths, tempDir}: BackupResult): Promise<void> {
await Promise.all(backupPaths.map((tempPath, index) => fs.copy(tempPath, originalPaths[index], {overwrite: true})));
await fs.remove(tempDir);
}
export const getLogger = (namespace: string, name: string) =>
LogFactory.getLogger(name, {forceEnable: true, namespace: `@wireapp/${namespace}`, separator: '/'});
export function checkCommanderOptions(
commanderInstance: typeof commander,
logdownInstance: Logger,
options: string[],
): void {
options.forEach(option => {
if (!commanderInstance.hasOwnProperty(option)) {
logdownInstance.error(`Required option "${option}" was not provided.`);
commanderInstance.outputHelp();
process.exit(1);
}
});
}
export function logEntries<T extends Object>(config: T, name: string, callee: string): void {
const logger = getLogger(callee, 'build-tools');
Object.entries(config).forEach(([key, value]) => {
if (Array.isArray(value)) {
value = value.join(',');
} else if (value instanceof Object) {
logEntries(value, `${name}.${key}`, callee);
} else {
logger.info(`${name}.${key} set to "${value}". `);
}
});
}
interface ExecResult {
stderr: string;
stdout: string;
}
export async function execAsync(command: string, throwOnError: false): Promise<ExecResult>;
export async function execAsync(command: string, throwOnError?: true): Promise<string>;
export async function execAsync(command: string, throwOnError: boolean = true): Promise<ExecResult | string> {
const {stderr, stdout} = await promisify(exec)(command);
if (throwOnError) {
if (!!stderr) {
throw new Error(stderr);
}
return stdout.trim();
}
return {stderr: stderr.trim(), stdout: stdout.trim()};
}