-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.js
More file actions
128 lines (90 loc) · 3.1 KB
/
compile.js
File metadata and controls
128 lines (90 loc) · 3.1 KB
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
import { promises } from "node:fs";
import { promisify } from "node:util";
import { exec } from "node:child_process";
import path from "node:path";
import os from "node:os";
import PQueue from "p-queue";
import ignored from "./ignore.js";
const CONCURRENCY = Math.max(1, os.cpus().length - 1);
const execAsync = promisify(exec);
const [sourceIgnoredFiles, ignoredDirectories] = ignored;
const ignoredFiles = sourceIgnoredFiles.filter(x => x !== "index.ts");
const { readdir: readDirectoryAsync } = promises;
const executable = "node";
const [, , cwd = process.cwd()] = process.argv;
const root = cwd;
// Do not match type definition files *.d.ts but match *.ts:
// https://stackoverflow.com/a/43493203/1384679
const extension = /(^.?|\.[^d]|[^.]d|[^.][^d])\.ts$/i;
const testFilePattern = /\.test\.[tj]s$/i;
const quotePath = path => `"${path}"`;
const main = async cwd => {
console.log(`Compiling files in ${cwd}...`);
const entries = await readDirectoryAsync(cwd, { withFileTypes: true });
const files = entries
.filter(x => x.isFile())
.map(x => x.name)
.filter(x => extension.test(x))
.filter(x => !testFilePattern.test(x))
.filter(x => !ignoredFiles.includes(x));
const directories = entries
.filter(x => x.isDirectory())
.map(x => x.name)
.filter(x => !ignoredDirectories.includes(x));
for (const directory of directories) {
await main(path.join(cwd, directory));
}
const processFile = async file => {
const filePath = path.join(cwd, file);
const relativeFilePath = path.relative(root, filePath);
try {
console.log(`Compiling ${relativeFilePath}...`);
const args = [
"./node_modules/typescript/bin/tsc",
//"--diagnostics",
"--skipLibCheck",
"--module ES6",
"--target ES2020",
"--noImplicitAny",
"--strict",
"-d",
quotePath(path.posix.normalize(filePath))
];
const command = [executable, ...args].join(" ");
const windows = process.platform === "win32";
const execOptions = {
windowsVerbatimArguments: windows
};
console.time(command);
const { stdout, stderr } = await execAsync(command, execOptions);
console.timeEnd(command);
console.log(`Compiled with strict mode ${relativeFilePath}`);
if (stdout || stderr) {
console.log({ stdout, stderr });
}
const nonStrictCommand = [
executable,
...args.filter(x => x !== "--strict")
].join(" ");
console.time(nonStrictCommand);
const { stdout: secondStdout, stderr: secondStderr } = await execAsync(
nonStrictCommand,
execOptions
);
console.timeEnd(nonStrictCommand);
console.log(`Compiled without strict mode ${relativeFilePath}`);
if (secondStdout || secondStderr) {
console.log({ secondStdout, secondStderr });
}
} catch (error) {
console.error(error);
process.exit(1);
}
};
const queue = new PQueue({ concurrency: CONCURRENCY });
for (const file of files) {
queue.add(() => processFile(file));
}
await queue.onIdle();
};
main(cwd);