Skip to content

Commit

Permalink
Watch dev-pm.config.js and update in running daemon (#98)
Browse files Browse the repository at this point in the history
This allows modifications without having to shutdown the deamon.
- processes are not restarted if the script command changes
- processes are stopped if the script is removed
- processes are not started if a new script is added
- identifier for updates is the name of a script
  • Loading branch information
nsams authored Nov 19, 2024
1 parent 9ac2343 commit ee3ebf8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
11 changes: 11 additions & 0 deletions .changeset/spotty-mirrors-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@comet/dev-process-manager": minor
---

Watch dev-pm.config.js and update in running daemon

This allows modifications without having to shutdown the deamon.
- processes are not restarted if the script command changes
- processes are stopped if the script is removed
- processes are not started if a new script is added
- identifier for updates is the name of a script
40 changes: 34 additions & 6 deletions src/commands/start-daemon.command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import colors from "colors";
import { existsSync } from "fs";
import { existsSync, watchFile } from "fs";
import { createServer, Server } from "net";

import { Config } from "../config.type";
Expand All @@ -21,15 +21,43 @@ export interface Daemon {
export const startDaemon = async (): Promise<void> => {
process.chdir(findConfigDir());
const pmConfigFileName = "dev-pm.config.js";
const { scripts: scriptDefinitions }: Config = await import(`${process.cwd()}/${pmConfigFileName}`);
const scripts = scriptDefinitions.map((scriptDefinition, id) => {
return new Script({ ...scriptDefinition, id });
});
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { scripts: scriptDefinitions }: Config = require(`${process.cwd()}/${pmConfigFileName}`);
const daemon: Daemon = {
scripts,
scripts: scriptDefinitions.map((scriptDefinition, id) => {
return new Script({ ...scriptDefinition, id });
}),
server: undefined,
};

watchFile(`${process.cwd()}/${pmConfigFileName}`, async () => {
console.log(`${pmConfigFileName} file changed, reloading scripts`);
delete require.cache[require.resolve(`${process.cwd()}/${pmConfigFileName}`)];
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { scripts: scriptDefinitions }: Config = require(`${process.cwd()}/${pmConfigFileName}`);

daemon.scripts = daemon.scripts.filter((script) => {
if (!scriptDefinitions.find((s) => s.name === script.name)) {
console.log("Removing script", script.name);
script.killProcess();
return false;
} else {
return true;
}
});

scriptDefinitions.forEach((scriptDefinition, id) => {
const script = daemon.scripts.find((s) => s.name === scriptDefinition.name);
if (script) {
console.log("Updating script definition", script.name);
script.updateScriptDefinition({ ...scriptDefinition, id });
} else {
console.log("Adding new script", scriptDefinition.name);
daemon.scripts.push(new Script({ ...scriptDefinition, id }));
}
});
});

if (existsSync(`.pm.sock`)) {
console.log(
"Could not start dev-pm server. A '.pm.sock' file already exists. \nThere are 2 possible reasons for this:\nA: Another dev-pm instance is already running. \nB: dev-pm crashed and left the file behind. In this case please remove the file manually.",
Expand Down
4 changes: 4 additions & 0 deletions src/daemon-command/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class Script {
this.logPrefix = color(`${this.name}: `);
}

updateScriptDefinition(scriptDefinition: ScriptDefinition) {
this.scriptDefinition = scriptDefinition;
}

get id(): number {
return this.scriptDefinition.id;
}
Expand Down

0 comments on commit ee3ebf8

Please sign in to comment.