Skip to content

Instantly share code, notes, and snippets.

@tomo-x7
Created November 2, 2024 08:20
Show Gist options
  • Save tomo-x7/9d8274f9dd062651c9840e666186546c to your computer and use it in GitHub Desktop.
Save tomo-x7/9d8274f9dd062651c9840e666186546c to your computer and use it in GitHub Desktop.
//@ts-check
import { watch } from "chokidar";
import { exec, execSync } from "node:child_process";
import { exit } from "node:process";
const watcher = watch(["./src", "./units", "./index.html"], {
persistent: true,
usePolling: true,
interval: 100,
});
let pid;
const startServer = () => {
console.log("start server");
pid = exec("pnpm run preview").pid;
console.log(`http-server process : ${pid}`);
};
const stopServer = () => {
if (pid) {
console.log(`stop server : ${pid}`);
try {
process.kill(pid);
pid = undefined;
} catch {}
} else {
console.log("server not running");
}
};
const build = () => {
try {
const hrstart = process.hrtime();
execSync('(echo "esbuild\n" &&node ./esbuild.js && echo "\nvite\n" && ./node_modules/.bin/vite build )> devbuild.log');
const hrend = process.hrtime(hrstart);
console.log("build success : %ds %dms", hrend[0], hrend[1] / 1000000);
} catch (e) {
console.error(e);
}
};
build()
startServer();
watcher.on("change", (...param) => {
console.log(`changed files : ${param[0]}`);
build()
});
const cleanup = () => {
stopServer();
exit(0);
};
process.on("SIGINT", cleanup);
process.on("SIGTERM", cleanup);
process.on("SIGQUIT", cleanup);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment