-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev.js
More file actions
347 lines (298 loc) · 11.4 KB
/
Copy pathdev.js
File metadata and controls
347 lines (298 loc) · 11.4 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/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 while local compiler work is in progress.
*
* Usage: node dev.js (or `npm run dev` from repo root)
* node dev.js --no-zig (skip Zig watcher, website-only)
*/
const { spawn } = 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 BINARY_NAME = process.platform === 'win32' ? 'zigcss.exe' : 'zigcss';
const BIN_PATH = path.join(ROOT, 'bin', BINARY_NAME);
const ZIG_OUT_BIN = path.join(ROOT, 'zig-out', 'bin', BINARY_NAME);
const READY_PATH = path.join(ROOT, 'bin', '.zigcss-dev-ready');
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;
let currentBuild = null;
let activeZigProcess = null;
let shutdownRequested = false;
function removeReadyMarker() {
fs.rmSync(READY_PATH, { force: true });
}
function removeReadyMarkerForShutdown() {
try {
removeReadyMarker();
} catch (err) {
logError('zig', `Failed to clear readiness marker during shutdown: ${err.message}`);
}
}
function containsPath(root, candidate) {
const relative = path.relative(root, candidate);
return relative === '' || (
relative !== '..' &&
!relative.startsWith(`..${path.sep}`) &&
!path.isAbsolute(relative)
);
}
function publishBuiltBinary() {
const sourceStat = fs.lstatSync(ZIG_OUT_BIN);
if (!sourceStat.isFile() || sourceStat.isSymbolicLink()) {
throw new Error('zig-out binary is not a regular non-symlink file');
}
if (process.platform !== 'win32' && (sourceStat.mode & 0o111) === 0) {
throw new Error('zig-out binary is not executable');
}
const canonicalRoot = fs.realpathSync(ROOT);
const canonicalSource = fs.realpathSync(ZIG_OUT_BIN);
if (!containsPath(canonicalRoot, canonicalSource)) {
throw new Error('zig-out binary escapes the source checkout');
}
const binDirectory = path.dirname(BIN_PATH);
fs.mkdirSync(binDirectory, { recursive: true });
const binStat = fs.lstatSync(binDirectory);
if (!binStat.isDirectory() || binStat.isSymbolicLink()) {
throw new Error('bin destination is not a regular directory');
}
const temporaryBinary = path.join(
binDirectory,
`.zigcss-${process.pid}-${Date.now()}.tmp`,
);
const temporaryMarker = `${READY_PATH}.${process.pid}-${Date.now()}.tmp`;
try {
fs.copyFileSync(canonicalSource, temporaryBinary, fs.constants.COPYFILE_EXCL);
fs.chmodSync(temporaryBinary, 0o755);
if (process.platform === 'win32') fs.rmSync(BIN_PATH, { force: true });
fs.renameSync(temporaryBinary, BIN_PATH);
fs.writeFileSync(temporaryMarker, 'ready\n', { encoding: 'utf8', flag: 'wx', mode: 0o644 });
fs.renameSync(temporaryMarker, READY_PATH);
} finally {
fs.rmSync(temporaryBinary, { force: true });
fs.rmSync(temporaryMarker, { force: true });
}
}
function rebuildZig() {
if (zigBuildInProgress) {
pendingRebuild = true;
return currentBuild;
}
zigBuildInProgress = true;
removeReadyMarker();
log('zig', 'Rebuilding zigcss…');
const start = Date.now();
currentBuild = new Promise((resolve) => {
const child = spawn('zig', ['build'], {
cwd: ROOT,
stdio: ['ignore', 'pipe', 'pipe'],
});
activeZigProcess = child;
let settled = false;
let stderr = '';
const finish = (result) => {
if (settled) return;
settled = true;
zigBuildInProgress = false;
currentBuild = null;
activeZigProcess = null;
resolve(result);
if (pendingRebuild && !shutdownRequested) {
pendingRebuild = false;
void rebuildZig();
} else if (shutdownRequested) {
pendingRebuild = false;
}
};
child.stderr.on('data', (data) => { stderr += data.toString(); });
child.stdout.on('data', (data) => { process.stdout.write(data); });
child.on('close', (code, signal) => {
const elapsed = Date.now() - start;
if (code === 0 && signal === null && !shutdownRequested) {
try {
publishBuiltBinary();
log('zig', `\x1b[32m✓ Rebuilt in ${elapsed}ms\x1b[0m`);
finish({ ok: true, code: 0 });
return;
} catch (err) {
logError('zig', `Built OK but failed to publish binary: ${err.message}`);
}
} else {
logError('zig', `Build failed (${elapsed}ms${signal ? `, signal ${signal}` : ''}):`);
if (stderr) console.error(stderr);
}
removeReadyMarker();
finish({ ok: false, code: Number.isInteger(code) && code > 0 ? code : 1 });
});
child.on('error', (err) => {
if (err.code === 'ENOENT') {
logError('zig', 'Zig compiler not found. Install Zig 0.15.2 or use --no-zig for website-only mode.');
} else {
logError('zig', `Failed to start zig build: ${err.message}`);
}
removeReadyMarker();
finish({ ok: false, code: 1 });
});
});
return currentBuild;
}
// ─── Zig File Watcher ───────────────────────────────────────────────────────
function startZigWatcher(onFatal) {
let debounceTimer = null;
const watchers = [];
const watchedFiles = [];
log('zig', 'Watching src/ and root build inputs for Zig changes…');
const schedule = (filename) => {
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
log('zig', `Changed: ${filename}`);
void rebuildZig();
}, DEBOUNCE_MS);
};
const fail = (err) => {
removeReadyMarkerForShutdown();
logError('zig', `Watcher error: ${err.message}`);
onFatal();
};
try {
const sourceWatcher = fs.watch(SRC_DIR, { recursive: true }, (_event, filename) => {
if (!filename || !filename.endsWith('.zig')) return;
schedule(path.join('src', filename));
});
watchers.push(sourceWatcher);
// These inputs may be symlinks in the development container. Watching the
// parent directory does not reliably report changes to a symlink target, so
// poll each resolved file directly and keep the exact listener for cleanup.
for (const filename of ['build.zig', 'build.zig.zon', 'build_helpers.zig']) {
const inputPath = path.join(ROOT, filename);
const listener = (current, previous) => {
if (
current.mtimeMs === previous.mtimeMs &&
current.size === previous.size &&
current.ino === previous.ino
) return;
schedule(filename);
};
fs.watchFile(inputPath, { interval: 500, persistent: true }, listener);
watchedFiles.push({ inputPath, listener });
}
for (const watcher of watchers) watcher.on('error', fail);
return {
close() {
if (debounceTimer) clearTimeout(debounceTimer);
for (const watcher of watchers) watcher.close();
for (const { inputPath, listener } of watchedFiles) {
fs.unwatchFile(inputPath, listener);
}
},
};
} catch (err) {
logError('zig', `Failed to start watcher: ${err.message}`);
for (const watcher of watchers) watcher.close();
for (const { inputPath, listener } of watchedFiles) {
fs.unwatchFile(inputPath, listener);
}
return null;
}
}
// ─── Vite Dev Server ────────────────────────────────────────────────────────
function startVite() {
log('vite', 'Starting docs dev server…');
const vite = spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['run', 'dev'], {
cwd: DOCS_DIR,
stdio: 'inherit',
env: process.env,
});
return vite;
}
// ─── Main ───────────────────────────────────────────────────────────────────
async function main() {
console.log();
console.log(' \x1b[1m\x1b[36m⚡ ZigCSS Dev Server\x1b[0m');
console.log(' \x1b[90m─────────────────────\x1b[0m');
console.log();
// A website-only run must never inherit compiler readiness from a previous
// container or an unclean local shutdown.
removeReadyMarker();
let zigWatcher = null;
let viteProcess = null;
let shuttingDown = false;
function cleanup(code = 0, terminateVite = true) {
if (shuttingDown) return;
shuttingDown = true;
shutdownRequested = true;
console.log();
log('dev', 'Shutting down…');
removeReadyMarkerForShutdown();
if (zigWatcher) zigWatcher.close();
if (activeZigProcess && !activeZigProcess.killed) {
activeZigProcess.kill('SIGTERM');
}
if (terminateVite && viteProcess && !viteProcess.killed) {
viteProcess.kill('SIGTERM');
}
process.exitCode = code;
}
process.on('SIGINT', () => cleanup(0));
process.on('SIGTERM', () => cleanup(0));
if (!skipZig) {
zigWatcher = startZigWatcher(() => cleanup(1));
if (zigWatcher === null) {
cleanup(1, false);
return;
}
let initialBuild;
try {
initialBuild = await rebuildZig();
} catch (err) {
logError('dev', `Initial Zig readiness gate failed: ${err.message}`);
cleanup(1, false);
return;
}
if (!initialBuild.ok) {
logError('dev', 'Initial Zig build failed; docs server was not started. Use --no-zig explicitly for website-only mode.');
cleanup(initialBuild.code, false);
return;
}
} else {
log('zig', 'Zig watcher skipped (--no-zig)');
}
if (shuttingDown) return;
viteProcess = startVite();
viteProcess.on('error', (err) => {
logError('vite', `Failed to start: ${err.message}`);
logError('vite', 'Run `cd docs && npm ci --ignore-scripts` first.');
cleanup(1, false);
});
viteProcess.on('close', (code, signal) => {
const exitCode = Number.isInteger(code) ? code : 1;
log('vite', `Vite exited with ${signal ? `signal ${signal}` : `code ${exitCode}`}`);
cleanup(exitCode, false);
});
}
main().catch((err) => {
removeReadyMarkerForShutdown();
logError('dev', err?.stack || err?.message || String(err));
process.exitCode = 1;
});