-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
171 lines (152 loc) · 4.26 KB
/
util.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* eslint-disable @typescript-eslint/no-var-requires */
const { createReadStream, createWriteStream } = require('fs');
const http = require('http');
const os = require('os');
const path = require('path');
const readline = require('readline');
const sh = require('shelljs');
const { Transform, pipeline } = require('stream');
const { promisify } = require('util');
const { createGzip } = require('zlib');
let BROWSER_OPEN_COMMAND;
switch (os.platform()) {
case 'win32':
BROWSER_OPEN_COMMAND = 'start';
break;
case 'darwin':
BROWSER_OPEN_COMMAND = 'open';
break;
case 'aix':
BROWSER_OPEN_COMMAND = 'defaultbrowser';
break;
default:
BROWSER_OPEN_COMMAND = 'xdg-open';
}
async function get(url) {
return new Promise((resolve, reject) => {
http.get(url, resolve).on('error', reject);
});
}
function gzip(file) {
return promisify(pipeline)(
createReadStream(file),
createGzip(),
createWriteStream(`${file}.gz`)
);
}
function noFail(fn) {
const { fatal } = sh.config;
sh.config.fatal = false;
const result = fn();
sh.config.fatal = fatal;
return result;
}
function onExit(cb) {
[ 'exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'SIGTERM' ].forEach(event => {
process.on(event, cb);
});
[ 'uncaughtException', 'unhandledRejection' ].forEach(event => {
process.on(event, err => {
console.error(err);
cb();
process.exitCode = 1;
});
});
}
function root(...components) {
return path.resolve(__dirname, ...components);
}
class LineAggregator extends Transform {
constructor(options) {
super(options);
this.carry = '';
}
_transform(chunk, encoding, callback) {
if (encoding === 'buffer') {
chunk = chunk.toString('utf-8');
encoding = 'utf-8';
}
const lines = chunk.split(/\r?\n/);
if (lines.length > 0) {
lines[0] = this.carry + lines[0];
this.carry = lines[lines.length - 1];
lines.splice(lines.length - 1, 1);
if (lines.length > 0) {
this.push(lines.join(os.EOL) + os.EOL, encoding);
}
}
callback();
}
_flush(callback) {
this.push(this.carry, 'utf-8');
this.carry = '';
callback();
}
}
class ShiftyCli {
moveCursor = promisify(readline.moveCursor.bind(readline, process.stdout));
clearScreenDown = promisify(readline.clearScreenDown.bind(readline, process.stdout));
write = promisify(process.stdout.write.bind(process.stdout));
constructor(prompt, lineHandler) {
this.lineHandler = lineHandler;
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt
});
this.rl.on('line', this.onLine.bind(this));
this.rl.prompt();
this.synchronizer = Promise.resolve();
}
addInputStreams(...streams) {
for (const is of streams) {
is.pipe(new LineAggregator({ encoding: 'utf-8' }))
.on('data', this.onData.bind(this));
}
}
synchronize(cb) {
this.synchronizer = this.synchronizer.then(cb)
.catch(console.error);
}
onLine(line) {
const cb = async () => {
if (this.lineHandler(line)) {
this.rl.prompt();
}
};
this.synchronize(cb);
}
async onData(chunk) {
const cb = async () => {
const line = this.rl.line;
const { rows, cols } = this.rl.getCursorPos();
await this.moveCursor(-cols - 2, -rows);
await this.clearScreenDown();
this.rl.line = '';
await this.write(chunk);
await this.write(os.EOL);
this.rl.prompt();
this.rl.write(line);
};
this.synchronize(cb);
}
close() {
this.rl.close();
this.rl.removeAllListeners();
}
}
async function sleep(millis) {
return new Promise(resolve => {
setTimeout(resolve, millis);
});
}
module.exports = {
BROWSER_OPEN_COMMAND,
get,
gzip,
noFail,
onExit,
root,
ShiftyCli,
sleep
};