-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·75 lines (58 loc) · 1.96 KB
/
Copy pathcli.js
File metadata and controls
executable file
·75 lines (58 loc) · 1.96 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
#!/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 --script <filepath> Serve with a custom transform script
dash serve --watch Build, serve, and rebuild on changes`;
async function loadTransform() {
const scriptIdx = args.indexOf('--script');
if (scriptIdx === -1) {
return undefined;
}
const scriptPath = args[scriptIdx + 1];
if (!scriptPath) {
console.error('Error: --script requires a file path');
process.exit(1);
}
const mod = await import(resolve(scriptPath));
return mod.default;
}
if (command === 'build') {
await build({ transform: await loadTransform() });
} else if (command === 'serve') {
const watching = args.includes('--watch');
const transform = await loadTransform();
await build({ transform });
if (watching) {
let debounceTimer;
watch('.', { recursive: true }, (event, filename) => {
if (!filename) {
return;
}
if (filename.startsWith('dist/') || filename.startsWith('.')) {
return;
}
if (!/\.(html|css|js|mjs|png|jpe?g|svg|gif|webp|avif|woff2?|ttf|otf|eot|ico)$/i.test(filename)) {
return;
}
clearTimeout(debounceTimer);
debounceTimer = setTimeout(async () => {
console.log(`Changed: ${filename}, rebuilding...`);
await build({ transform, clean: false });
}, 100);
});
}
const portIdx = args.indexOf('--port');
const port = portIdx === -1 ? 3000 : Number(args[portIdx + 1]);
startServer(port);
} else {
console.log(USAGE);
}