-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev.js
More file actions
198 lines (159 loc) · 6.03 KB
/
Copy pathdev.js
File metadata and controls
198 lines (159 loc) · 6.03 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
#!/usr/bin/env node
/**
* ZigCSS Dev Server
*
* Orchestrates two things in parallel:
* 1. Watches src/*.zig for changes → runs `zig build` → copies binary to bin/
* 2. Runs the Vite dev server for the docs website with live-reload
*
* When the Zig engine is rebuilt the Vite server is notified so the browser
* does a full page reload, picking up the new binary for playground compilations.
*
* Usage: node dev.js (or `npm run dev` from repo root)
* node dev.js --no-zig (skip Zig watcher, website-only)
*/
const { spawn, execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// ─── Configuration ──────────────────────────────────────────────────────────
const ROOT = __dirname;
const DOCS_DIR = path.join(ROOT, 'docs');
const SRC_DIR = path.join(ROOT, 'src');
const BIN_PATH = path.join(ROOT, 'bin', 'zigcss');
const ZIG_OUT_BIN = path.join(ROOT, 'zig-out', 'bin', 'zigcss');
const DEBOUNCE_MS = 300;
const skipZig = process.argv.includes('--no-zig');
// ─── Helpers ────────────────────────────────────────────────────────────────
function log(tag, msg) {
const time = new Date().toLocaleTimeString();
console.log(`\x1b[90m${time}\x1b[0m \x1b[36m[${tag}]\x1b[0m ${msg}`);
}
function logError(tag, msg) {
const time = new Date().toLocaleTimeString();
console.error(`\x1b[90m${time}\x1b[0m \x1b[31m[${tag}]\x1b[0m ${msg}`);
}
// ─── Zig Rebuild ────────────────────────────────────────────────────────────
let zigBuildInProgress = false;
let pendingRebuild = false;
function rebuildZig() {
if (zigBuildInProgress) {
pendingRebuild = true;
return;
}
zigBuildInProgress = true;
log('zig', 'Rebuilding zigcss…');
const start = Date.now();
const child = spawn('zig', ['build'], {
cwd: ROOT,
stdio: ['ignore', 'pipe', 'pipe'],
});
let stderr = '';
child.stderr.on('data', (d) => { stderr += d.toString(); });
child.stdout.on('data', (d) => { process.stdout.write(d); });
child.on('close', (code) => {
zigBuildInProgress = false;
const elapsed = Date.now() - start;
if (code === 0) {
// Copy the built binary into bin/
try {
fs.mkdirSync(path.dirname(BIN_PATH), { recursive: true });
fs.copyFileSync(ZIG_OUT_BIN, BIN_PATH);
fs.chmodSync(BIN_PATH, 0o755);
log('zig', `\x1b[32m✓ Rebuilt in ${elapsed}ms\x1b[0m`);
} catch (err) {
logError('zig', `Built OK but failed to copy binary: ${err.message}`);
}
} else {
logError('zig', `Build failed (${elapsed}ms):`);
if (stderr) console.error(stderr);
}
if (pendingRebuild) {
pendingRebuild = false;
rebuildZig();
}
});
child.on('error', (err) => {
zigBuildInProgress = false;
if (err.code === 'ENOENT') {
logError('zig', 'Zig compiler not found. Install with: brew install zig');
logError('zig', 'Continuing with website-only mode…');
} else {
logError('zig', `Failed to start zig build: ${err.message}`);
}
});
}
// ─── Zig File Watcher ───────────────────────────────────────────────────────
function startZigWatcher() {
let debounceTimer = null;
log('zig', `Watching ${path.relative(ROOT, SRC_DIR)}/ for .zig changes…`);
// Do an initial build
rebuildZig();
try {
const watcher = fs.watch(SRC_DIR, { recursive: true }, (_event, filename) => {
if (!filename || !filename.endsWith('.zig')) return;
// Debounce rapid changes
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
log('zig', `Changed: ${filename}`);
rebuildZig();
}, DEBOUNCE_MS);
});
watcher.on('error', (err) => {
logError('zig', `Watcher error: ${err.message}`);
});
return watcher;
} catch (err) {
logError('zig', `Failed to start watcher: ${err.message}`);
return null;
}
}
// ─── Vite Dev Server ────────────────────────────────────────────────────────
function startVite() {
log('vite', 'Starting docs dev server…');
const vite = spawn('npm', ['run', 'dev'], {
cwd: DOCS_DIR,
stdio: 'inherit',
env: {
...process.env,
// Tell the Vite plugin where the binary lives (absolute path)
ZIGCSS_BIN: BIN_PATH,
},
});
vite.on('error', (err) => {
logError('vite', `Failed to start: ${err.message}`);
logError('vite', 'Run `cd docs && npm install` first.');
process.exit(1);
});
return vite;
}
// ─── Main ───────────────────────────────────────────────────────────────────
function main() {
console.log();
console.log(' \x1b[1m\x1b[36m⚡ ZigCSS Dev Server\x1b[0m');
console.log(' \x1b[90m─────────────────────\x1b[0m');
console.log();
let zigWatcher = null;
if (!skipZig) {
zigWatcher = startZigWatcher();
} else {
log('zig', 'Zig watcher skipped (--no-zig)');
}
const viteProcess = startVite();
// Graceful shutdown
function cleanup() {
console.log();
log('dev', 'Shutting down…');
if (zigWatcher) zigWatcher.close();
if (viteProcess && !viteProcess.killed) {
viteProcess.kill('SIGTERM');
}
process.exit(0);
}
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
viteProcess.on('close', (code) => {
log('vite', `Vite exited with code ${code}`);
cleanup();
});
}
main();