-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·69 lines (54 loc) · 1.68 KB
/
cli.js
File metadata and controls
executable file
·69 lines (54 loc) · 1.68 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
#!/usr/bin/env bun
import { resolve } from 'node:path';
import { watch } from 'node:fs';
import { build } from './build.js';
import { startServer } from './serve.js';
const args = process.argv.slice(2);
const command = args[0];
const USAGE = `Usage:
dash build Build HTML files to dist/
dash build --script <filepath> Build with a custom transform script
dash serve Serve dist/ on port 3000
dash serve --port <number> Serve on a custom port
dash serve --watch Build, serve, and rebuild on changes`;
if (command === 'build') {
const scriptIdx = args.indexOf('--script');
if (scriptIdx !== -1) {
const scriptPath = args[scriptIdx + 1];
if (!scriptPath) {
console.error('Error: --script requires a file path');
process.exit(1);
}
const mod = await import(resolve(scriptPath));
await build(mod.default);
} else {
await build();
}
} else if (command === 'serve') {
const watching = args.includes('--watch');
if (watching) {
await build();
let debounceTimer;
watch('.', { recursive: true }, (event, filename) => {
if (!filename) {
return;
}
if (filename.startsWith('dist/') || filename.startsWith('.')) {
return;
}
if (!filename.endsWith('.html')) {
return;
}
clearTimeout(debounceTimer);
debounceTimer = setTimeout(async () => {
console.log(`Changed: ${filename}, rebuilding...`);
await build();
}, 100);
});
}
const portIdx = args.indexOf('--port');
const port = portIdx === -1 ? 3000 : Number(args[portIdx + 1]);
startServer(port);
} else {
console.log(USAGE);
}