-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsc2.js
106 lines (84 loc) · 2.61 KB
/
tsc2.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
// @ts-check
"use strict";
const { exec } = require("child_process");
////////////////////////////////////
//#region Util
const ansi = {
reset: "\x1b[0m",
label: "\x1b[90m",
duration: "\x1b[35m",
err: "\x1b[91m",
ok: "\x1b[92m",
};
function getTimeString () {
let time = new Date().toLocaleTimeString(undefined, { hour12: false });
time = time.startsWith("24") ? `00${time.slice(2)}` : time;
return ansi.label + time + ansi.reset;
}
/**
* @param {number} start
*/
function getElapsedString (start, time = Date.now() - start) {
let timeString;
if (time >= 1000) {
timeString = `${(time / 1000).toFixed(2).replace(/\.0+$/, "")} s`;
} else if (time >= 1) {
timeString = `${time.toFixed(0)} ms`;
} else {
timeString = `${Math.floor(time * 1000)} μs`;
}
return ansi.duration + timeString + ansi.reset;
}
//#endregion
////////////////////////////////////
(async () => {
const tsc = exec(`npx tspc --project src/tsconfig.json ${process.argv.slice(2).map(a => a.includes(" ") ? `"${a}"` : a).join(" ")}`);
let lastStart = -1;
await /** @type {Promise<void>} */(new Promise((resolve) => {
tsc.stdout?.on("data", data => {
data = data
.replace(/\[\x1b\[90m\d{1,2}:\d{2}:\d{2}[ \xa0\u202f][AP]M\x1b\[0m\][ \xa0\u202f]/gi, ""); // remove time
const lines = data.split("\n");
for (let line of lines) {
if (line.trim().length === 0) {
// ignore boring lines
continue;
}
if (line.startsWith("> ")) {
// ignore "> tsc --build --watch --pretty --preserveWatchOutput" line
continue;
}
if (line.includes("Starting compilation in watch mode...")) {
lastStart = Date.now();
} else if (line.includes("Starting incremental compilation...")) {
if (lastStart !== -1) {
// ignore duplicate "starting incremental compilation" line
continue;
}
lastStart = Date.now();
}
if (!process.env.TSC_NO_COLOURIZE_ERRORS) {
line = line
.replace(/(?<!\d)0 errors/, ansi.ok + "0 errors" + ansi.reset)
.replace(/(?<!\d)(?!0)(\d+) errors/, ansi.err + "$1 errors" + ansi.reset);
}
if (!process.env.TSC_NO_LOG_DURATION && line.includes(". Watching for file changes.")) {
line = line.replace(". Watching for file changes.", ` after ${getElapsedString(lastStart)}`);
lastStart = -1;
}
process.stdout.write(`${getTimeString()} ${line}\n`);
}
if (data.includes("Found 0 errors. Watching for file changes.")) {
resolve();
}
});
tsc.stderr?.on("data", data => {
process.stderr.write(data);
});
tsc.on("exit", (exitCode) => {
if (exitCode !== 0)
process.exit(exitCode);
resolve();
});
}));
})();