/
check.ts
86 lines (78 loc) · 2.37 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
import AsyncLimiterClass, {
AsyncCurrentLimiter,
} from "https://cdn.skypack.dev/@masx200/async-task-current-limiter@2.1.0?dts";
import { WalkEntry } from "https://deno.land/std@0.157.0/fs/_util.ts";
import { parse, walk } from "./deps.ts";
import { split_by_count } from "./utils/split_by_count.ts";
function searchFilesNames({
skip,
}: // limiter,
{
skip?: RegExp | RegExp[];
// limiter: AsyncCurrentLimiter;
}) {
// console.log("type check start!");
const entry_iter = walk(".", {
includeFiles: true,
includeDirs: false,
exts: ["ts"],
skip: [/node_modules/, skip].flat().filter(Boolean) as RegExp[],
});
return entry_iter;
}
if (import.meta.main) {
await start();
// .catch(console.error);
}
async function parallel_check(
entry_iter: AsyncIterableIterator<WalkEntry>,
limiter: AsyncCurrentLimiter,
) {
const files: string[] = [];
for await (const entry of entry_iter) {
files.push(entry.path);
}
const entries = split_by_count(files, 50);
await Promise.all(
entries.map((stack) =>
limiter.run(function (stack: string[]) {
return runDenoCheck(stack);
}.bind(null, stack))
),
);
}
async function start() {
const limiter = new AsyncLimiterClass(navigator.hardwareConcurrency);
const args = parse(Deno.args);
console.log(args);
const skip = typeof args.skip === "string"
? new RegExp(String(args.skip))
: Array.isArray(args.skip)
? args.skip.map((s: string | RegExp) => new RegExp(s))
: undefined;
const entry_iter = searchFilesNames({ skip });
await parallel_check(entry_iter, limiter);
// console.log("type check Done!");
}
async function runDenoCheck(stack: string[]) {
const cmd = ["deno", "check", "--unstable", ...stack];
console.log(cmd);
const process = Deno.run({
cmd: cmd,
stderr: "piped",
stdout: "piped",
});
const [status, stdout, stderr] = await Promise.all([
process.status(),
process.output(),
process.stderrOutput(),
]);
const decoder = new TextDecoder();
const out = decoder.decode(stdout);
const err = decoder.decode(stderr);
console.log(status, out, err);
process.close();
if (!status.success) {
throw new Error("type check failed:" + out + err);
}
}