-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-test.js
More file actions
248 lines (224 loc) · 6.65 KB
/
Copy pathdev-test.js
File metadata and controls
248 lines (224 loc) · 6.65 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* @fileoverview Development test runner for all VanJS templates.
* @description This script automates testing of all template variants by:
* 1. Installing/updating dependencies (pnpm for Node, deno for Deno)
* 2. Starting the dev server for each template
* 3. Verifying the server responds on port 5173
* 4. Killing the server and reporting results
*
* @usage
* # Run all templates
* node dev-test.js
*
* # Run specific template(s) using filter regex
* node dev-test.js --filter=node-base
* node dev-test.js --filter=deno-routing
* node dev-test.js --filter="node|deno"
*
* @requires Node.js 18+
*/
import { spawn } from "node:child_process";
import fs from "node:fs/promises";
import path from "node:path";
const cwd = process.cwd();
const WAIT_MS = 5000;
const PORT = 5173;
const results = [];
async function run(cmd, args, options = {}, timeoutMs = 60_000) {
const { cwd: cwdPath, env: envVars, ...rest } = options;
return new Promise((resolve, reject) => {
const proc = spawn(cmd, args, {
cwd: cwdPath,
stdio: "pipe",
env: { ...process.env, ...envVars },
...rest,
});
let stdout = "";
let stderr = "";
proc.stdout.on("data", (d) => (stdout += d.toString()));
proc.stderr.on("data", (d) => (stderr += d.toString()));
const timer = setTimeout(() => {
proc.kill("SIGTERM");
reject(
new Error(
`Command timed out after ${timeoutMs}ms: ${cmd} ${args.join(" ")}`,
),
);
}, timeoutMs);
proc.on("close", (code) => {
clearTimeout(timer);
if (code === 0) resolve(stdout);
else {
reject(
new Error(
`Command failed with code ${code}: ${cmd} ${
args.join(" ")
}\n${stderr}`,
),
);
}
});
proc.on("error", (err) => {
clearTimeout(timer);
reject(err);
});
});
}
async function waitForPort(port, timeoutMs = 15_000) {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
try {
const res = await fetch(`http://localhost:${port}`);
if (
res.ok ||
res.status === 301 ||
res.status === 302 ||
res.status === 404
) {
return true;
}
} catch {
// port not ready
}
await new Promise((r) => setTimeout(r, 500));
}
throw new Error(`Port ${port} did not respond within ${timeoutMs}ms`);
}
async function killProcessTree(proc) {
if (proc.pid) {
try {
process.kill(-proc.pid, "SIGTERM");
} catch {
// already dead
}
}
}
const args = process.argv.slice(2);
const filterArg = args.find((a) => a.startsWith("--filter="));
const filterRe = filterArg ? new RegExp(filterArg.split("=")[1]) : null;
const directories = await fs.readdir(cwd);
let templates = directories.filter((d) => d.startsWith("template-"));
if (filterRe) templates = templates.filter((d) => filterRe.test(d));
for (const template of templates) {
const templatePath = path.join(cwd, template);
const isNode = template.startsWith("template-node-") ||
template.startsWith("template-vike");
const isDeno = template.startsWith("template-deno-");
const start = Date.now();
try {
let devProc;
if (isNode) {
const hasNodeModules = await fs
.stat(path.join(templatePath, "node_modules"))
.then(() => true)
.catch(() => false);
const workspaceFile = path.join(cwd, "pnpm-workspace.yaml");
const tempWorkspaceFile = path.join(cwd, "pnpm-workspace.yaml.bak");
let movedWorkspace = false;
if (await fs.stat(workspaceFile).then(() => true).catch(() => false)) {
await fs.rename(workspaceFile, tempWorkspaceFile);
movedWorkspace = true;
}
try {
if (hasNodeModules) {
console.log(`[${template}] Updating dependencies...`);
await run("pnpm", ["update"], {
cwd: templatePath,
env: { ...process.env, CI: "true" },
}, 120_000);
} else {
console.log(`[${template}] Installing dependencies...`);
await run("pnpm", ["install"], {
cwd: templatePath,
env: { ...process.env, CI: "true" },
}, 120_000);
}
} finally {
if (movedWorkspace) {
await fs.rename(tempWorkspaceFile, workspaceFile);
}
}
console.log(`[${template}] Starting dev server...`);
devProc = spawn("pnpm", ["run", "dev"], {
cwd: templatePath,
stdio: "pipe",
detached: true,
});
} else if (isDeno) {
const hasNodeModules = await fs
.stat(path.join(templatePath, "node_modules"))
.then(() => true)
.catch(() => false);
if (hasNodeModules) {
console.log(`[${template}] Updating dependencies...`);
await run(
"deno",
["cache", "--reload", "deno.json"],
{ cwd: templatePath },
120_000,
);
} else {
console.log(`[${template}] Caching dependencies...`);
await run(
"deno",
["cache", "deno.json"],
{ cwd: templatePath },
120_000,
);
}
console.log(`[${template}] Starting dev server...`);
devProc = spawn("deno", ["task", "dev"], {
cwd: templatePath,
stdio: "pipe",
detached: true,
});
} else {
results.push({
template,
type: "unknown",
status: "skip",
duration: "0ms",
error: "",
});
continue;
}
let stderr = "";
devProc.stderr?.on("data", (d) => (stderr += d.toString()));
await waitForPort(PORT, 15_000);
const exited = new Promise((resolve) => {
devProc.on("close", resolve);
});
await new Promise((resolve) => setTimeout(resolve, WAIT_MS));
// check if still alive
if (devProc.exitCode !== null) {
throw new Error(
`Dev server exited early with code ${devProc.exitCode}\n${stderr}`,
);
}
killProcessTree(devProc);
await exited.catch(() => {});
const duration = `${Date.now() - start}ms`;
console.log(`[${template}] Passed (${duration})`);
results.push({
template,
type: isNode ? "node" : "deno",
status: "pass",
duration,
error: "",
});
} catch (err) {
const duration = `${Date.now() - start}ms`;
console.error(`[${template}] Failed (${duration}): ${err.message}`);
results.push({
template,
type: isNode ? "node" : "deno",
status: "fail",
duration,
error: err.message.split("\n")[0],
});
}
}
console.log("\n");
console.table(results);
const failed = results.filter((r) => r.status === "fail");
process.exit(failed.length > 0 ? 1 : 0);