-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.js
More file actions
60 lines (47 loc) · 1.53 KB
/
watch.js
File metadata and controls
60 lines (47 loc) · 1.53 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
import chokidar from "chokidar";
import { exec, spawn } from "child_process";
import path from "path";
let isFirstBuild = true;
function buildAndMaybeServe() {
console.log("🔨 Ejecutando host local...");
exec("node dev-build.js", (err, stdout, stderr) => {
if (err) {
console.error("❌ Error al compilar:\n", stderr || err.message);
return;
}
console.log("✅ Compilación completa\n", stdout);
if (isFirstBuild) {
isFirstBuild = false;
console.log("🚀 Iniciando servidor en http://localhost:5500");
const server = spawn("npx", ["live-server", ".dev", "--port=5500", "--quiet"], {
stdio: "inherit",
shell: true,
});
server.on("close", (code) => {
console.log(`🛑 Servidor finalizado con código ${code}`);
process.exit(code ?? 0);
});
}
});
}
const watcher = chokidar.watch(".", {
ignored: [/dist/, /\.dev/, /node_modules/, /\.plugin-deps/, /\.git/],
ignoreInitial: true,
});
console.log("👀 Observando archivos del proyecto...");
watcher.on("change", (filePath) => {
const fileName = path.basename(filePath);
console.log(`📝 Cambio detectado en: ${fileName}`);
buildAndMaybeServe();
});
watcher.on("add", (filePath) => {
const fileName = path.basename(filePath);
console.log(`➕ Archivo detectado: ${fileName}`);
buildAndMaybeServe();
});
watcher.on("unlink", (filePath) => {
const fileName = path.basename(filePath);
console.log(`➖ Archivo eliminado: ${fileName}`);
buildAndMaybeServe();
});
buildAndMaybeServe();