-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
65 lines (59 loc) · 1.85 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
/* eslint-disable @eslint-community/eslint-comments/disable-enable-pair */
/* eslint-disable no-console */
import { exec } from 'node:child_process';
import util from 'node:util';
const fgReset = '\x1b[0m';
const fgRed = '\x1b[31m';
const fgYellow = '\x1b[33m';
const fgBlue = '\x1b[34m';
const logError = (/** @type {string[]} */ ...args) => {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(fgRed, ...args, fgReset);
};
const logWarn = (/** @type {string[]} */ ...args) => {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(fgYellow, ...args, fgReset);
};
const logDebug = (/** @type {string[]} */ ...args) => {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(fgBlue, ...args, fgReset);
};
const reinspectLog = (/** @type {string} */ log) =>
log
.replaceAll(/\[error\]/g, `${fgRed}[error]${fgReset}`)
.replaceAll(/\[warn\]/g, `${fgYellow}[warn]${fgReset}`)
.replaceAll(/\[debug\]/g, `${fgBlue}[debug]${fgReset}`);
const execAsync = util.promisify(exec);
const execCommand = (/** @type {string} */ command) =>
execAsync(command)
.catch((error) => error)
// eslint-disable-next-line complexity
.then(({ stdout, stderr }) => {
if (stdout && stdout.length > 0) {
process.stdout.write(reinspectLog(stdout));
}
if (stderr && stderr.length > 0) {
process.stderr.write(reinspectLog(stderr));
throw new Error('Error has exited');
}
if (
stdout.includes('error') &&
!stdout.includes('0 error') &&
!stdout.includes('no error') &&
!stdout.includes('No results')
) {
throw new Error('Error has exited');
}
return undefined;
});
export {
logError as error,
logWarn as warn,
logDebug as debug,
reinspectLog,
exec,
execAsync,
execCommand,
fgReset,
fgRed
};