From 911589afe6eb58dcd5fe42889e572cafb99c4357 Mon Sep 17 00:00:00 2001 From: Johannes Obermair Date: Wed, 2 Aug 2023 17:15:12 +0200 Subject: [PATCH] Expose config type Can be used to type check the configuration file. --- .changeset/weak-pants-buy.md | 7 +++++++ example/dev-pm.config.js | 11 ++++++++++- src/commands/start-daemon.command.ts | 4 ++-- src/config.type.ts | 6 ++++++ src/index.ts | 2 ++ 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 .changeset/weak-pants-buy.md create mode 100644 src/config.type.ts diff --git a/.changeset/weak-pants-buy.md b/.changeset/weak-pants-buy.md new file mode 100644 index 0000000..604bb33 --- /dev/null +++ b/.changeset/weak-pants-buy.md @@ -0,0 +1,7 @@ +--- +"@comet/dev-process-manager": minor +--- + +Expose config type + +Can be used to type check the configuration file. See [example/dev-pm.config.js](example/dev-pm.config.js) for an example. diff --git a/example/dev-pm.config.js b/example/dev-pm.config.js index 177ae9b..7b34a10 100644 --- a/example/dev-pm.config.js +++ b/example/dev-pm.config.js @@ -1,4 +1,11 @@ -module.exports = { +// @ts-check + +/** + * Normally you would use import('@comet/dev-process-manager').Config + * + * @type {import('../lib').Config} + */ +const config = { scripts: [ { name: "sleep-1", @@ -13,3 +20,5 @@ module.exports = { }, ], }; + +module.exports = config; diff --git a/src/commands/start-daemon.command.ts b/src/commands/start-daemon.command.ts index 0aff892..43f97ce 100644 --- a/src/commands/start-daemon.command.ts +++ b/src/commands/start-daemon.command.ts @@ -2,6 +2,7 @@ import colors from "colors"; import { existsSync } from "fs"; import { createServer, Server } from "net"; +import { Config } from "../config.type"; import { logsDaemonCommand } from "../daemon-command/logs.daemon-command"; import { restartDaemonCommand } from "../daemon-command/restart.daemon-command"; import { Script } from "../daemon-command/script"; @@ -10,7 +11,6 @@ import { shutdownDaemonCommand } from "../daemon-command/shutdown.daemon-command import { startDaemonCommand } from "../daemon-command/start.daemon-command"; import { statusDaemonCommand } from "../daemon-command/status.daemon-command"; import { stopDaemonCommand } from "../daemon-command/stop.daemon-command"; -import { ScriptDefinition } from "../script-definition.type"; import { findConfigDir } from "../utils/find-config-dir"; export interface Daemon { @@ -21,7 +21,7 @@ export interface Daemon { export const startDaemon = async (): Promise => { process.chdir(findConfigDir()); const pmConfigFileName = "dev-pm.config.js"; - const { scripts: scriptDefinitions }: { scripts: ScriptDefinition[] } = await import(`${process.cwd()}/${pmConfigFileName}`); + const { scripts: scriptDefinitions }: Config = await import(`${process.cwd()}/${pmConfigFileName}`); const scripts = scriptDefinitions.map((scriptDefinition, id) => { return new Script({ ...scriptDefinition, id }); }); diff --git a/src/config.type.ts b/src/config.type.ts new file mode 100644 index 0000000..a27d976 --- /dev/null +++ b/src/config.type.ts @@ -0,0 +1,6 @@ +import { ScriptDefinition } from "./script-definition.type"; + +export interface Config { + // ID is set by the daemon + scripts: Omit[]; +} diff --git a/src/index.ts b/src/index.ts index 0912706..ba3f846 100755 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,8 @@ import { logs, restart, shutdown, start, status } from "./commands"; import { startDaemon } from "./commands/start-daemon.command"; import { stop } from "./commands/stop.command"; +export { Config } from "./config.type"; + const env = dotenv.config(); dotenvExpand.expand(env);