-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck.ts
More file actions
185 lines (157 loc) · 4.48 KB
/
check.ts
File metadata and controls
185 lines (157 loc) · 4.48 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
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
type StepResult = {
name: string;
success: boolean;
duration: number;
output?: string;
};
type RunResult = {
exitCode: number;
stdout: string;
stderr: string;
};
async function run(cmd: string[]): Promise<RunResult> {
const proc = Bun.spawn(cmd, {
stdout: "pipe",
stderr: "pipe",
});
const exitCode = await proc.exited;
const stdout = await new Response(proc.stdout).text();
const stderr = await new Response(proc.stderr).text();
return { exitCode, stdout, stderr };
}
async function runStep(
name: string,
fn: () => Promise<{ success: boolean; output?: string }>
): Promise<StepResult> {
const start = performance.now();
const { success, output } = await fn();
const duration = (performance.now() - start) / 1000;
return { name, success, duration, output };
}
function formatDuration(seconds: number): string {
return seconds >= 1
? `${seconds.toFixed(1)}s`
: `${(seconds * 1000).toFixed(0)}ms`;
}
function printResult(result: StepResult): void {
const icon = result.success ? "\x1b[32m✓\x1b[0m" : "\x1b[31m✗\x1b[0m";
console.log(`${icon} ${result.name} (${formatDuration(result.duration)})`);
if (!result.success && result.output) {
console.log();
console.log(result.output);
}
}
async function lint(): Promise<{ success: boolean; output?: string }> {
const result = await run([
"bunx",
"oxlint",
"-c",
"./.oxlintrc.json",
"--deny-warnings",
]);
return {
success: result.exitCode === 0,
output: result.exitCode !== 0 ? result.stderr || result.stdout : undefined,
};
}
async function typecheck(): Promise<{ success: boolean; output?: string }> {
const result = await run(["bun", "tsc", "--noEmit"]);
return {
success: result.exitCode === 0,
output: result.exitCode !== 0 ? result.stderr || result.stdout : undefined,
};
}
async function format(): Promise<{ success: boolean; output?: string }> {
const result = await run(["bunx", "oxfmt"]);
return {
success: result.exitCode === 0,
output: result.exitCode !== 0 ? result.stderr || result.stdout : undefined,
};
}
async function formatCheck(): Promise<{ success: boolean; output?: string }> {
const result = await run(["bunx", "oxfmt", "--check"]);
return {
success: result.exitCode === 0,
output: result.exitCode !== 0 ? result.stderr || result.stdout : undefined,
};
}
type Command = "all" | "lint" | "typecheck" | "format" | "ci";
function parseArgs(): { command: Command } {
const args = Bun.argv.slice(2);
const command =
(args.find((a: string) => !a.startsWith("-")) as Command) ?? "all";
return { command };
}
async function runAll(): Promise<void> {
// 1. Format first (may modify files)
const formatResult = await runStep("format", format);
printResult(formatResult);
if (!formatResult.success) {
process.exit(1);
}
// 2. Run lint and typecheck in parallel
const [lintResult, typecheckResult] = await Promise.all([
runStep("lint", lint),
runStep("typecheck", typecheck),
]);
printResult(lintResult);
printResult(typecheckResult);
if (!lintResult.success || !typecheckResult.success) {
process.exit(1);
}
}
async function runCi(): Promise<void> {
// Run all checks in parallel (no format modification in CI)
const [lintResult, typecheckResult, formatResult] = await Promise.all([
runStep("lint", lint),
runStep("typecheck", typecheck),
runStep("format", formatCheck),
]);
printResult(lintResult);
printResult(typecheckResult);
printResult(formatResult);
if (
!lintResult.success ||
!typecheckResult.success ||
!formatResult.success
) {
process.exit(1);
}
}
async function runSingle(command: Command): Promise<void> {
let result: StepResult;
switch (command) {
case "lint":
result = await runStep("lint", lint);
break;
case "typecheck":
result = await runStep("typecheck", typecheck);
break;
case "format":
result = await runStep("format", format);
break;
default:
console.error(`Unknown command: ${command}`);
console.log("Usage: bun check.ts [command]");
console.log("Commands: all, lint, typecheck, format, ci");
process.exit(1);
}
printResult(result);
if (!result.success) {
process.exit(1);
}
}
async function main(): Promise<void> {
const { command } = parseArgs();
switch (command) {
case "all":
await runAll();
break;
case "ci":
await runCi();
break;
default:
await runSingle(command);
}
}
void main();