Open History to see the last commit
1const examples = [
2 "./examples/cloudflare-dev",
3 "./examples/cloudflare-worker",
4 "./examples/cloudflare-pr-package",
5 "./examples/cloudflare-worker-async",
6 "./examples/cloudflare-tanstack",
7 "./examples/cloudflare-tanstack-start-solid",
8 "./examples/cloudflare-neon-drizzle",
9 "./examples/aws-lambda",
10] as const;
11
12type CommandResult = {
13 label: string;
14 command: readonly string[];
15 exitCode: number | null;
16 stdout: string;
17 stderr: string;
18};
19
20type TaskState = {
21 label: string;
22 command: readonly string[];
23 status: "pending" | "running" | "ok" | "failed";
24 startedAt?: number;
25 endedAt?: number;
26 exitCode?: number | null;
27};
28
29const readStream = async (
30 stream: ReadableStream<Uint8Array>,
31): Promise<string> => new Response(stream).text();
32
33const elapsedSeconds = (state: TaskState): string => {
34 if (state.startedAt === undefined) {
35 return "0s";
36 }
37 const endedAt = state.endedAt ?? performance.now();
38 return `${Math.round((endedAt - state.startedAt) / 1000)}s`;
39};
40
41const makeStatusRenderer = (states: readonly TaskState[]) => {
42 const interactive = process.stdout.isTTY === true;
43 let renderedLines = 0;
44
45 const lines = () => [
46 "Example tests",
47 ...states.map((state) => {
48 const icon =
49 state.status === "ok"
50 ? "ok"
51 : state.status === "failed"
52 ? "failed"
53 : state.status;
54 const exit =
55 state.exitCode === undefined || state.exitCode === 0
56 ? ""
57 : ` exit ${state.exitCode ?? "signal"}`;
58 return ` ${icon.padEnd(7)} ${state.label} ${elapsedSeconds(state)}${exit}`;
59 }),
60 ];
61
62 return {
63 render() {
64 if (!interactive) {
65 return;
66 }
67 if (renderedLines > 0) {
68 process.stdout.write(`\x1b[${renderedLines}F\x1b[J`);
69 }
70 const output = lines();
71 process.stdout.write(`${output.join("\n")}\n`);
72 renderedLines = output.length;
73 },
74 finish() {
75 if (interactive) {
76 this.render();
77 return;
78 }
79 for (const line of lines()) {
80 console.log(line);
81 }
82 },
83 };
84};
85
86const run = async (
87 state: TaskState,
88 render: () => void,
89): Promise<CommandResult> => {
90 state.status = "running";
91 state.startedAt = performance.now();
92 render();
93
94 const child = Bun.spawn([...state.command], {
95 stdout: "pipe",
96 stderr: "pipe",
97 stdin: "inherit",
98 });
99 const [exitCode, stdout, stderr] = await Promise.all([
100 child.exited,
101 readStream(child.stdout),
102 readStream(child.stderr),
103 ]);
104
105 state.status = exitCode === 0 ? "ok" : "failed";
106 state.exitCode = exitCode;
107 state.endedAt = performance.now();
108 render();
109
110 return {
111 label: state.label,
112 command: state.command,
113 exitCode,
114 stdout,
115 stderr,
116 };
117};
118
119const runParallel = async (
120 tasks: readonly { label: string; command: readonly string[] }[],
121): Promise<readonly CommandResult[]> => {
122 const states = tasks.map(
123 (task): TaskState => ({
124 ...task,
125 status: "pending",
126 }),
127 );
128 const renderer = makeStatusRenderer(states);
129 renderer.render();
130 const interval = setInterval(() => renderer.render(), 1000);
131
132 try {
133 return await Promise.all(
134 states.map((state) => run(state, () => renderer.render())),
135 );
136 } finally {
137 clearInterval(interval);
138 renderer.finish();
139 }
140};
141
142const testResults = await runParallel(
143 examples.map((example) => ({
144 label: example,
145 command: ["bun", "run", "--filter", example, "test"],
146 })),
147);
148const failedTests = testResults.filter((result) => result.exitCode !== 0);
149
150if (failedTests.length > 0) {
151 console.error("\nFailed example tests:");
152 for (const failure of failedTests) {
153 const exit = failure.exitCode === null ? "signal" : failure.exitCode;
154 console.error(
155 `- ${failure.label} (exit ${exit}): ${failure.command.join(" ")}`,
156 );
157 }
158
159 for (const failure of failedTests) {
160 console.error(`\n--- ${failure.label} stdout ---`);
161 if (failure.stdout.length > 0) {
162 console.error(failure.stdout.trimEnd());
163 } else {
164 console.error("(empty)");
165 }
166
167 console.error(`\n--- ${failure.label} stderr ---`);
168 if (failure.stderr.length > 0) {
169 console.error(failure.stderr.trimEnd());
170 } else {
171 console.error("(empty)");
172 }
173 }
174 process.exit(1);
175}
176
177const [formatFailure] = await runParallel([
178 { label: "format", command: ["bun", "run", "format"] },
179]);
180if (formatFailure.exitCode !== 0) {
181 console.error("\nFormat failed:");
182 if (formatFailure.stdout.length > 0) {
183 console.error(formatFailure.stdout.trimEnd());
184 }
185 if (formatFailure.stderr.length > 0) {
186 console.error(formatFailure.stderr.trimEnd());
187 }
188 process.exit(formatFailure.exitCode ?? 1);
189}