Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the desired behavior? I think a restart would be more logical

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I would expect that. And you would also have edge-cases like waitOn or env get modified - should that also trigger a restart?

Copy link
Collaborator

@johnnyomair johnnyomair Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've thought mostly about waitOn and script. Maybe we could change the log to something like "... updated, please restart to apply 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}`);
johnnyomair marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -24,6 +24,10 @@ export class Script {
this.logPrefix = color(`${this.name}: `);
}

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

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