forked from denoland/deno_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.ts
executable file
·218 lines (199 loc) · 5.85 KB
/
check.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env -S deno run --quiet --allow-read --allow-write --allow-run --allow-env
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import $, * as dax from "https://deno.land/x/[email protected]/mod.ts";
const isCI = !!Deno.env.get("CI");
const divider =
"--------------------------------------------------------------";
class CommandState {
stdout: string = "";
stderr: string = "";
lastLine: string = "";
lastLineTimestamp: number = 0;
success: boolean;
running: boolean;
constructor(public name: string) {
this.success = true;
this.running = true;
}
makeReader(which: "stdout" | "stderr", callback: () => void) {
// deno-lint-ignore no-this-alias
const self = this;
const textDecoder = new TextDecoderStream();
textDecoder.readable.pipeTo(
new WritableStream({
write(chunk) {
self[which] += chunk;
if (chunk.includes("\n")) {
const lines = self[which].split("\n");
while (lines.length > 0) {
const line = lines[lines.length - 1].trim().replace(
// deno-lint-ignore no-control-regex
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
"",
);
if (line.length == 0) {
lines.pop();
continue;
}
if (line != self.lastLine) {
self.lastLine = line;
self.lastLineTimestamp = new Date().valueOf();
callback();
}
break;
}
}
},
}),
);
return textDecoder.writable;
}
toString() {
if (!this.running) {
if (this.success) {
return `✅ ${this.name}`;
} else {
return `❌ ${this.name}`;
}
}
return `⏳ ${this.name}`;
}
}
async function runCommands(
mode: string,
commands: { [name: string]: dax.CommandBuilder },
) {
function makeProgress() {
if (isCI) {
return null;
}
return $.progress(mode);
}
const pb = makeProgress();
const promises: Promise<void>[] = [];
const processes: Set<CommandState> = new Set();
function updateProgress(final: boolean = false) {
if (!pb) {
if (final) {
console.log(divider);
console.log("Done: " + [...processes.values()].flat().join(", "));
}
return;
}
const processString = [...processes.values()].flat().join(", ");
let lastLine = "";
let lastTime = 0;
let complete = 0;
for (const process of processes) {
if (process.running && process.lastLineTimestamp > lastTime) {
let line = process.lastLine;
if (line.length > 50) {
line = line.slice(0, 50) + "᠁";
}
lastLine = ` (${line})`;
lastTime = process.lastLineTimestamp;
}
if (!process.running) {
complete++;
}
}
pb.length(processes.size);
pb.position(complete);
pb.message(processString + lastLine);
}
for (const [name, command] of Object.entries(commands)) {
const state = new CommandState(name);
processes.add(state);
const promise = (async () => {
try {
await command
.stdout(state.makeReader("stdout", updateProgress))
.stderr(state.makeReader("stderr", updateProgress));
if (isCI) {
console.log(`✅ ${state.name} passed`);
console.log(state.stderr.trim());
console.log(state.stdout.trim());
}
} catch (e) {
state.success = false;
$.log(`❌ ${state.name} failed!`);
if (!e.message.includes("Exited with code")) {
console.log(e);
}
$.log(divider);
$.log(state.stderr.trim());
$.log(state.stdout.trim());
$.log(divider);
updateProgress();
} finally {
state.running = false;
updateProgress();
}
return;
})();
promises.push(promise);
}
updateProgress();
await Promise.all(promises);
updateProgress(true);
if (pb) {
pb.noClear();
pb.prefix("Done");
pb.forceRender();
pb.finish();
}
for (const process of processes) {
if (!process.success) {
Deno.exit(1);
}
}
}
export async function main(command: string, flag: string) {
if (command == "format") {
const check = flag == "--check";
if (check) {
await runCommands("Formatting (--check)", {
"cargo fmt": $`cargo fmt -- --check`,
"deno fmt":
$`deno fmt --check ./core/ ./ops/ ./serde_v8/ ./testing/ ./tools/`,
});
} else {
await runCommands("Formatting", {
"cargo fmt": $`cargo fmt`,
"deno fmt": $`deno fmt ./core/ ./ops/ ./serde_v8/ ./testing/ ./tools/`,
});
}
} else if (command == "lint") {
const fix = flag == "--fix";
if (fix) {
await runCommands("Linting (--fix)", {
"copyright": $`tools/copyright_checker.js --fix`,
"cargo clippy":
$`cargo clippy --fix --allow-dirty --allow-staged --locked --release --all-features --all-targets -- -D clippy::all`,
});
} else {
await runCommands("Linting", {
"deno check tools/": $`deno check ${
[...Deno.readDirSync("tools")].map((f) => `tools/${f.name}`)
}`,
"copyright": $`tools/copyright_checker.js`,
"deno lint": $`deno lint`,
"tsc":
$`deno run --allow-read --allow-env npm:[email protected]/tsc --noEmit -p testing/tsconfig.json`,
"cargo clippy":
$`cargo clippy --locked --release --all-features --all-targets -- -D clippy::all`,
});
}
}
}
if (import.meta.main) {
if (Deno.args.length == 0) {
console.error("Usage:");
console.error(" check.ts lint [--fix]");
console.error(" check.ts format [--check]");
Deno.exit(1);
}
const command = Deno.args[0];
const flag = Deno.args[1];
await main(command, flag);
}