Skip to content

Commit

Permalink
Merge pull request #79 from vivid-planet/id-for-script
Browse files Browse the repository at this point in the history
feat: every script is now accessible through an dedicated numeric id
  • Loading branch information
johnnyomair authored Aug 2, 2023
2 parents 155d107 + 96cd913 commit 827328e
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-wolves-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/dev-process-manager": minor
---

every script is now accessible through an dedicated numeric id
5 changes: 3 additions & 2 deletions src/commands/logs.command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LogsCommandOptions } from "../daemon-command/logs.daemon-command";
import { connect } from "./connect";

export const logs = async (names: string[]): Promise<void> => {
export const logs = async (options: LogsCommandOptions): Promise<void> => {
const client = await connect();
client.write(`logs ${JSON.stringify(names)}`);
client.write(`logs ${JSON.stringify(options)}`);
};
4 changes: 2 additions & 2 deletions src/commands/start-daemon.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export const startDaemon = async (): Promise<void> => {
process.chdir(findConfigDir());
const pmConfigFileName = "dev-pm.config.js";
const { scripts: scriptDefinitions }: { scripts: ScriptDefinition[] } = await import(`${process.cwd()}/${pmConfigFileName}`);
const scripts = scriptDefinitions.map((scriptDefinition) => {
return new Script(scriptDefinition);
const scripts = scriptDefinitions.map((scriptDefinition, id) => {
return new Script({ ...scriptDefinition, id });
});
const daemon: Daemon = {
scripts,
Expand Down
5 changes: 3 additions & 2 deletions src/commands/stop.command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StopCommandOptions } from "../daemon-command/stop.daemon-command";
import { connect } from "./connect";

export const stop = async (names: string[]): Promise<void> => {
export const stop = async (options: StopCommandOptions): Promise<void> => {
const client = await connect();
client.write(`stop ${JSON.stringify(names)}`);
client.write(`stop ${JSON.stringify(options)}`);
};
8 changes: 5 additions & 3 deletions src/daemon-command/logs.daemon-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Socket } from "net";

import { Daemon } from "../commands/start-daemon.command";
import { handleLogSocketClose } from "./handle-log-socket-close";
import { scriptsMatchingPattern } from "./scripts-matching-pattern";
import { scriptsMatchingPattern, ScriptsMatchingPatternOptions } from "./scripts-matching-pattern";

export function logsDaemonCommand(daemon: Daemon, socket: Socket, names: string[]): void {
const scriptsToProcess = scriptsMatchingPattern(daemon, names);
export type LogsCommandOptions = ScriptsMatchingPatternOptions;

export function logsDaemonCommand(daemon: Daemon, socket: Socket, options: LogsCommandOptions): void {
const scriptsToProcess = scriptsMatchingPattern(daemon, { patterns: options.patterns });
if (!scriptsToProcess.length) {
socket.write("No matching scripts found in dev-pm config");
socket.end();
Expand Down
7 changes: 3 additions & 4 deletions src/daemon-command/restart.daemon-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { Socket } from "net";

import { Daemon } from "../commands/start-daemon.command";
import { handleLogSocketClose } from "./handle-log-socket-close";
import { scriptsMatchingPattern } from "./scripts-matching-pattern";
import { scriptsMatchingPattern, ScriptsMatchingPatternOptions } from "./scripts-matching-pattern";

export interface RestartCommandOptions {
names: string[];
export interface RestartCommandOptions extends ScriptsMatchingPatternOptions {
follow: boolean;
}
export async function restartDaemonCommand(daemon: Daemon, socket: Socket, options: RestartCommandOptions): Promise<void> {
const scriptsToProcess = scriptsMatchingPattern(daemon, options.names);
const scriptsToProcess = scriptsMatchingPattern(daemon, { patterns: options.patterns });
if (!scriptsToProcess.length) {
socket.write("No matching scripts found in dev-pm config");
socket.end();
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}: `);
}

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

get name(): string {
return this.scriptDefinition.name;
}
Expand Down
30 changes: 18 additions & 12 deletions src/daemon-command/scripts-matching-pattern.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { Daemon } from "../commands/start-daemon.command";
import { Script } from "./script";

export function scriptsMatchingPattern(daemon: Daemon, names: string[]): Script[] {
if (names.length == 0 || names.includes("all")) {
// all
return daemon.scripts;
}
export interface ScriptsMatchingPatternOptions {
patterns: string[];
}

export function scriptsMatchingPattern(daemon: Daemon, { patterns }: ScriptsMatchingPatternOptions): Script[] {
const uniquePatterns = [...new Set(patterns.map((pattern) => pattern.split(",")).flat())];
const ids = uniquePatterns.filter((pattern) => pattern && !isNaN(+pattern)).map(Number) || [];
const names = uniquePatterns.filter((pattern) => pattern && isNaN(+pattern)) || [];

return daemon.scripts.filter((script) => {
return names.some((name) => {
if (name[0] === "@") {
if (script.groups.includes(name.substring(1))) return true;
} else {
if (script.name == name) return true;
}
return false;
if (ids.length === 0 && names.length === 0) return true;

const idExists = ids.some((id) => script.id === id);
const nameExists = names.some((name) => {
if (name === "all") return true;
if (name[0] === "@" && script.groups.includes(name[0])) return true;
return script.name === name;
});

return idExists || nameExists;
});
}
7 changes: 3 additions & 4 deletions src/daemon-command/start.daemon-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { Socket } from "net";

import { Daemon } from "../commands/start-daemon.command";
import { handleLogSocketClose } from "./handle-log-socket-close";
import { scriptsMatchingPattern } from "./scripts-matching-pattern";
import { scriptsMatchingPattern, ScriptsMatchingPatternOptions } from "./scripts-matching-pattern";

export interface StartCommandOptions {
names: string[];
export interface StartCommandOptions extends ScriptsMatchingPatternOptions {
follow: boolean;
}
export async function startDaemonCommand(daemon: Daemon, socket: Socket, options: StartCommandOptions): Promise<void> {
const scriptsToProcess = scriptsMatchingPattern(daemon, options.names);
const scriptsToProcess = scriptsMatchingPattern(daemon, { patterns: options.patterns });
if (!scriptsToProcess.length) {
socket.write("No matching scripts found in dev-pm config");
socket.end();
Expand Down
10 changes: 5 additions & 5 deletions src/daemon-command/status.daemon-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import prettyBytes from "pretty-bytes";

import { Daemon } from "../commands/start-daemon.command";
import { ScriptStatus } from "./script";
import { scriptsMatchingPattern } from "./scripts-matching-pattern";
import { scriptsMatchingPattern, ScriptsMatchingPatternOptions } from "./scripts-matching-pattern";

export interface StatusCommandOptions {
names: string[];
export interface StatusCommandOptions extends ScriptsMatchingPatternOptions {
interval: number | undefined;
}

Expand All @@ -37,7 +36,7 @@ async function pidusageRecursive(pid: number): Promise<{ cpu: number; memory: nu
}

export async function statusDaemonCommand(daemon: Daemon, socket: Socket, options: StatusCommandOptions): Promise<void> {
const scriptsToProcess = scriptsMatchingPattern(daemon, options.names);
const scriptsToProcess = scriptsMatchingPattern(daemon, { patterns: options.patterns });
if (!scriptsToProcess.length) {
socket.write("No matching scripts found in dev-pm config");
socket.end();
Expand All @@ -58,6 +57,7 @@ export async function statusDaemonCommand(daemon: Daemon, socket: Socket, option
do {
const table = new CLITable({
head: [
colors.blue.bold("ID"),
colors.blue.bold("Script"),
colors.blue.bold("Status"),
colors.blue.bold("CPU"),
Expand Down Expand Up @@ -87,7 +87,7 @@ export async function statusDaemonCommand(daemon: Daemon, socket: Socket, option
//
}
}
table.push([script.name, status, cpu, memory, pid?.toString(), script.restartCount]);
table.push([script.id, script.name, status, cpu, memory, pid?.toString(), script.restartCount]);
}

if (!socket.writable) break;
Expand Down
8 changes: 5 additions & 3 deletions src/daemon-command/stop.daemon-command.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Socket } from "net";

import { Daemon } from "../commands/start-daemon.command";
import { scriptsMatchingPattern } from "./scripts-matching-pattern";
import { scriptsMatchingPattern, ScriptsMatchingPatternOptions } from "./scripts-matching-pattern";

export async function stopDaemonCommand(daemon: Daemon, socket: Socket, names: string[]): Promise<void> {
const scriptsToProcess = scriptsMatchingPattern(daemon, names);
export type StopCommandOptions = ScriptsMatchingPatternOptions;

export async function stopDaemonCommand(daemon: Daemon, socket: Socket, options: StopCommandOptions): Promise<void> {
const scriptsToProcess = scriptsMatchingPattern(daemon, { patterns: options.patterns });
if (!scriptsToProcess.length) {
socket.write("No matching scripts found in dev-pm config");
socket.end();
Expand Down
28 changes: 14 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@ const program = new Command();
program.version(require("../package.json").version);

program
.command("start [name...]")
.command("start [patterns...]")
.option("--follow", "Follow logs after starting script")
.action((names, options) => {
start({ names, follow: !!options.follow });
.action((patterns: string[], options) => {
start({ patterns, follow: !!options.follow });
});

program
.command("logs [name...]")
.command("logs [patterns...]")
.aliases(["log"])
.action((names) => {
logs(names);
.action((patterns: string[]) => {
logs({ patterns });
});

program
.command("status [name...]")
.command("status [patterns...]")
.aliases(["list", "ls"])
.addOption(new Option("-i, --interval [seconds]", "Keep status open and refresh periodically at given interval").preset("1"))
.action((names, options) => {
status({ names, interval: options.interval ? parseInt(options.interval) : undefined });
.action((patterns: string[], options) => {
status({ patterns, interval: options.interval ? parseInt(options.interval) : undefined });
});

program
.command("restart [name...]")
.command("restart [patterns...]")
.option("--follow", "Follow logs after starting script")
.action((names, options) => {
restart({ names, follow: !!options.follow });
.action((patterns: string[], options) => {
restart({ patterns, follow: !!options.follow });
});

program.command("stop [name...]").action((names) => {
stop(names);
program.command("stop [patterns...]").action((patterns: string[]) => {
stop({ patterns });
});

program
Expand Down
1 change: 1 addition & 0 deletions src/script-definition.type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface ScriptDefinition {
id: number;
name: string;
script: string;
group?: string | string[];
Expand Down

0 comments on commit 827328e

Please sign in to comment.