-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (46 loc) · 1.43 KB
/
index.js
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
#!/usr/bin/env node
import fs from "fs";
import { spawn } from "child_process";
import parseArgs from "./parsers/cli.parser.js";
import { colorLog } from "./utils.js";
import { COLORS } from "./constants.js";
const args = process.argv.slice(2);
let currentProcess = null;
let debounceTimer = null;
const startDevelopmentServer = (namedArgs, file) => {
if (currentProcess) {
currentProcess.kill();
}
const command = namedArgs.com;
currentProcess = spawn(command, [file], { stdio: "inherit" });
const serverErrMsg = namedArgs?.sve ?? `Server Stopped Due To Some Error`;
currentProcess.on("close", (exitCode) => {
if (exitCode == 0)
return colorLog(
COLORS.FgGreen,
"execution successful , waiting for changes....."
);
colorLog(COLORS.BgRed, `${serverErrMsg} : exitCode`);
});
};
const chokidarBano = (namedArgs, file) => {
fs.watch(file, (eventType, fileName) => {
if (fileName && eventType === "change") {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
colorLog(COLORS.BgYellow, `File Changed : ${fileName}`);
startDevelopmentServer(namedArgs, file);
}, 1000);
}
});
};
const init = (namedArgs, file, flags) => {
startDevelopmentServer(namedArgs, file);
chokidarBano(namedArgs, file);
};
try {
const { namedArgs, flags, file } = parseArgs(args);
init(namedArgs, file, flags);
} catch (error) {
colorLog(COLORS.BgRed, error.message);
}