Repository navigation
Expand file tree
/
Copy pathsite.mjs
More file actions
104 lines (90 loc) · 3.04 KB
/
Copy pathsite.mjs
File metadata and controls
104 lines (90 loc) · 3.04 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
#!/usr/bin/env node
/**
* champ96k.com — helper CLI for common local workflows.
*
* Usage:
* node scripts/site.mjs dev start the dev server (stops strays first)
* node scripts/site.mjs build clear stale cache, type-check, build
* node scripts/site.mjs preview clear cache, build, serve dist, run DOM checks
* node scripts/site.mjs sync GitHub sync (tolerates a bad GITHUB_TOKEN)
* node scripts/site.mjs verify clear cache + build + preview + DOM checks
* node scripts/site.mjs help this help
*
* dom-check.mjs expects a preview on port 4321, so that's the port used here.
*/
import { spawnSync } from 'node:child_process';
import { rmSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const run = (cmd, args, opts = {}) => {
const res = spawnSync(cmd, args, {
stdio: 'inherit',
cwd: root,
shell: true,
...opts,
});
return res.status ?? 1;
};
const clearCaches = () => {
for (const p of ['.astro', 'node_modules/.astro', 'dist']) {
rmSync(resolve(root, p), { recursive: true, force: true });
}
console.log('Cleared .astro / node_modules/.astro / dist.');
};
const stopDev = () => run('npx', ['astro', 'dev', 'stop'], {});
const stopPreview = () => run('npx', ['astro', 'preview', 'stop'], {});
const help = () => {
console.log(`
Site helpers — run from the repo root:
node scripts/site.mjs dev start dev server (stops stray ones first)
node scripts/site.mjs build clear cache, type-check, static build
node scripts/site.mjs preview clear cache, build, serve dist on :4321, DOM checks
node scripts/site.mjs sync GitHub data sync (ignores a bad GITHUB_TOKEN)
node scripts/site.mjs verify cache-clear + build + preview + DOM checks
node scripts/site.mjs help this help
`);
};
const arg = process.argv[2] ?? 'help';
switch (arg) {
case 'dev':
stopDev();
run('npm', ['run', 'dev']);
break;
case 'build':
clearCaches();
run('npm', ['run', 'check']);
run('npm', ['run', 'build']);
break;
case 'preview':
clearCaches();
run('npm', ['run', 'check']);
run('npm', ['run', 'build']);
stopPreview();
run('npm', ['run', 'preview', '--', '--port', '4321'], { detached: true });
// Give the server a moment, then run the DOM checks against it.
setTimeout(() => {
run('npm', ['run', 'test:pages']);
stopPreview();
}, 2500);
break;
case 'sync':
// The user's shell may export an invalid GITHUB_TOKEN that breaks auth.
run('env', ['-u', 'GITHUB_TOKEN', 'npm', 'run', 'sync:github']);
break;
case 'verify':
clearCaches();
run('npm', ['run', 'check']);
run('npm', ['run', 'build']);
stopPreview();
run('npm', ['run', 'preview', '--', '--port', '4321'], { detached: true });
setTimeout(() => {
run('npm', ['run', 'test:pages']);
stopPreview();
}, 2500);
break;
case 'help':
default:
help();
break;
}