-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.ts
55 lines (43 loc) · 1.45 KB
/
console.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
import { SpawnOptions } from 'child_process';
import crossSpawn from 'cross-spawn';
/**
* @see https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_child_process_spawnsync_command_args_options
*/
export function spawn(command: string, args: string[], options?: SpawnOptions) {
const process = crossSpawn(command, args, options);
return new Promise<string>((resolve, reject) => {
let data = '';
function exit(code: number, signal: string) {
code
? reject(Object.assign(new Error(signal), { code, process }))
: resolve(data);
}
process.on('exit', exit),
process.on('close', exit),
process.on('error', reject),
process.on('disconnect', reject);
process.stdout?.on('data', chunk => (data += chunk));
});
}
/**
* Ensure a Global NPM command installed
*/
export async function ensureCommand(command: string, library?: string) {
try {
await spawn(command, ['-h']);
} catch {
await spawn('npm', ['install', library || command + '-cli', '-g']);
}
}
let index = 0;
export function step<T>(
title: string,
logic: (index: number, title: string, end: boolean) => T,
reset?: boolean
): T {
title = ` ${++index}. ${title}`;
console.info(`\n${title}\n ${'-'.repeat(title.length - 1)}`);
const result = logic(index, title, reset);
if (reset) index = 0;
return result;
}