Skip to content

Instantly share code, notes, and snippets.

@npretto
Last active February 13, 2020 21:15
Show Gist options
  • Select an option

  • Save npretto/31acbf2e328d71ccc4502133d58a3720 to your computer and use it in GitHub Desktop.

Select an option

Save npretto/31acbf2e328d71ccc4502133d58a3720 to your computer and use it in GitHub Desktop.
overcomplicated watch for "multi folder" haxe projects
const chokidar = require("chokidar");
const { spawn } = require("child_process");
const compilerPort = 6066;
// TODO:
// [ ] start compilation server
// [ ] use compilation server
// [ ] parse paths in stdout and add cwd so they can be clicked
// [ ] proper support for openfl / assets (run openfl build only when assets change)
const client = {
sources: ["client/Sources", "common/common"],
compile: ["openfl", "build", "html5"],
cwd: "client",
postBuild: () => console.log(" =====> compiled client")
};
const server = {
sources: ["server/src", "common/common"],
compile: ["haxe", "server.hxml"],
cwd: "server",
postBuild: () => console.log(" =====> compiled server")
};
const targets = { client, server };
const runThings = (targets, settings) => {
const processes = {};
Object.entries(targets).forEach(([targetName, buildSetting]) => {
console.log("-----------");
console.log(buildSetting.sources);
chokidar
.watch(buildSetting.sources, { ignoreInitial: true })
.on("all", (event, path) => {
console.log(event, path);
const { compile, cwd, postBuild } = buildSetting;
const [command, ...args] = compile;
if (processes[targetName]) {
console.log(`killing ${targetName}`);
processes[targetName].kill();
console.log(`killed ${targetName}`);
}
console.log(`starting new ${targetName}`);
const child = spawn(command, args, { cwd });
processes[targetName] = child;
child.stdout.on("data", data => {
console.log(`[${targetName}] stdout: ${data}`);
});
child.stderr.on("data", data => {
console.error(`[${targetName}] stderr: ${data}`);
});
child.on("close", code => {
console.log(`[${targetName}] child process exited with code ${code}`);
processes[targetName] = undefined;
if (code === 0) {
postBuild();
}
});
});
});
};
runThings(targets, { compilerPort });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment