-
Notifications
You must be signed in to change notification settings - Fork 74
/
utils.ts
74 lines (64 loc) · 1.9 KB
/
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
import { spawn, SpawnOptions } from 'child_process';
import { writeFile } from 'fs';
import { get, RequestOptions } from 'https';
import { createInterface } from 'readline';
export const ROOT_DIR = __dirname;
export const TYPESCRIPT_FILENAME = 'index.d.ts';
export const FLOW_FILENAME = 'index.js.flow';
export function writeFileAsync(filename: string, content: string) {
return new Promise<void>((resolve, reject) => {
writeFile(filename, content, 'utf-8', error => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
export function spawnAsync(command: string, optionsOrArg: SpawnOptions | string, ...args: string[]): Promise<string> {
let options: SpawnOptions | undefined;
if (typeof optionsOrArg === 'string') {
args.unshift(optionsOrArg);
} else {
options = optionsOrArg;
}
return new Promise((resolve, reject) => {
const cp = spawn(command, args, {
cwd: ROOT_DIR,
...options,
});
if (cp.stdout) {
let data = '';
cp.stdout.on('data', chunk => (data += chunk));
cp.on('close', code => (code === 0 ? resolve(data) : reject(data)));
} else {
cp.on('close', code => (code === 0 ? resolve('') : reject()));
}
cp.on('error', reject);
});
}
export function getJsonAsync(url: RequestOptions): Promise<any> {
return new Promise((resolve, reject) => {
const req = get(url, res => {
let data = '';
res.on('data', chunk => (data += chunk));
res.on('end', () => {
try {
const json = JSON.parse(data);
resolve(json);
} catch (e) {
reject(e);
}
});
});
req.on('error', reject);
req.end();
});
}
const readline = createInterface(process.stdin, process.stdout);
export function questionAsync(message: string): Promise<string> {
return new Promise(resolve => {
readline.question(message, resolve);
});
}