-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
66 lines (59 loc) · 1.89 KB
/
build.mjs
File metadata and controls
66 lines (59 loc) · 1.89 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
import { spawn } from 'node:child_process';
const flags = new Set(process.argv.slice(2));
const FAST = flags.has('--fast');
const RETRY = flags.has('--retry');
const steps = [
{ name: 'vite build', cmd: 'vite', args: ['build'] },
{ name: 'post-build', cmd: 'node', args: ['scripts/post-build.mjs'] },
];
if (!FAST) {
steps.push({
name: 'prerender-crawl',
cmd: 'node',
args: ['scripts/prerender-crawl.mjs', ...(RETRY ? ['--retry'] : [])],
});
}
function run(step) {
return new Promise((resolve, reject) => {
const isWin = process.platform === 'win32';
const child = spawn(step.cmd, step.args, {
stdio: 'inherit',
shell: isWin,
});
child.on('close', (code) => {
if (code === 0) resolve();
else reject(new Error(`${step.name} exited with code ${code}`));
});
child.on('error', reject);
});
}
function fmtSec(ms) {
const s = ms / 1000;
if (s < 60) return `${s.toFixed(2)}s`;
const m = Math.floor(s / 60);
return `${m}m ${(s - m * 60).toFixed(1)}s`;
}
const globalStart = Date.now();
const timings = [];
for (const step of steps) {
const s = Date.now();
try {
await run(step);
} catch (err) {
const elapsed = Date.now() - s;
timings.push({ name: step.name, ms: elapsed, failed: true });
console.error(`\n[build] ${step.name} failed after ${fmtSec(elapsed)}`);
console.error(`[build] total elapsed: ${fmtSec(Date.now() - globalStart)}`);
process.exit(1);
}
const elapsed = Date.now() - s;
timings.push({ name: step.name, ms: elapsed });
console.log(`[build] ${step.name}: ${fmtSec(elapsed)}`);
}
const total = Date.now() - globalStart;
console.log(`\n[build] ── summary ──`);
for (const t of timings) {
const pct = ((t.ms / total) * 100).toFixed(0);
console.log(` ${t.name.padEnd(16)} ${fmtSec(t.ms).padStart(10)} (${pct}%)`);
}
console.log(` ${'total'.padEnd(16)} ${fmtSec(total).padStart(10)}`);