-
Notifications
You must be signed in to change notification settings - Fork 32
Spawn dashboard process from cli with openworkflow dashboard
#184
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
base: main
Are you sure you want to change the base?
Changes from all commits
106b56a
7c108bf
ba5405a
9efeb7c
ea8f98d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import * as p from "@clack/prompts"; | |
| import { consola } from "consola"; | ||
| import { config as loadDotenv } from "dotenv"; | ||
| import { createJiti } from "jiti"; | ||
| import { spawn } from "node:child_process"; | ||
| import { | ||
| existsSync, | ||
| mkdirSync, | ||
|
|
@@ -308,6 +309,70 @@ export async function workerStart(cliOptions: WorkerConfig): Promise<void> { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * openworkflow dashboard | ||
| * Starts the dashboard by delegating to `@openworkflow/dashboard` via npx. | ||
| */ | ||
| export async function dashboard(): Promise<void> { | ||
| consola.start("Starting dashboard..."); | ||
|
|
||
| const { configFile } = await loadConfigWithEnv(); | ||
| if (!configFile) { | ||
| throw new CLIError( | ||
| "No config file found.", | ||
| "Run `ow init` to create a config file before starting the dashboard.", | ||
| ); | ||
| } | ||
| consola.info(`Using config: ${configFile}`); | ||
|
|
||
| // eslint-disable-next-line sonarjs/no-os-command-from-path | ||
| const child = spawn("npx", ["@openworkflow/dashboard"], { | ||
| stdio: "inherit", | ||
| }); | ||
|
|
||
| await new Promise<void>((resolve, reject) => { | ||
| /** remove signal handlers after the child exits */ | ||
| function cleanupSignalHandlers(): void { | ||
| process.off("SIGINT", signalHandler); | ||
| process.off("SIGTERM", signalHandler); | ||
| } | ||
|
|
||
| child.on("error", (error) => { | ||
| cleanupSignalHandlers(); | ||
| reject( | ||
| new CLIError( | ||
| "Failed to start dashboard.", | ||
| `Could not spawn npx: ${error.message}`, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| child.on("exit", (code) => { | ||
| cleanupSignalHandlers(); | ||
| if (code === 0 || code === null) { | ||
| resolve(); | ||
| } else { | ||
| reject( | ||
| new CLIError( | ||
| "Dashboard exited with an error.", | ||
| `Exit code: ${String(code)}`, | ||
| ), | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| /** | ||
| * Graceful shutdown on signals. | ||
| * @param signal - Signal | ||
| */ | ||
| function signalHandler(signal: NodeJS.Signals): void { | ||
| child.kill(signal); | ||
| } | ||
| process.on("SIGINT", signalHandler); | ||
| process.on("SIGTERM", signalHandler); | ||
jamescmartinez marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+368
to
+372
|
||
| }); | ||
| } | ||
|
Comment on lines
+316
to
+374
|
||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,10 @@ | ||||||||||||||||||||||||||||||||||
| #!/usr/bin/env node | ||||||||||||||||||||||||||||||||||
| import { dirname, join } from "node:path"; | ||||||||||||||||||||||||||||||||||
| import { fileURLToPath } from "node:url"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // simple wrapper to load the dashboard server since the index.mjs file does not | ||||||||||||||||||||||||||||||||||
| // have a shebang line | ||||||||||||||||||||||||||||||||||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||||||||||||||||||||||||||||||||||
| const serverPath = join(__dirname, ".output", "server", "index.mjs"); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| await import(serverPath); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| await import(serverPath); | |
| try { | |
| await import(serverPath); | |
| } catch (error) { | |
| // Provide a clearer message if the dashboard server entrypoint is missing. | |
| if (error && (error.code === "ERR_MODULE_NOT_FOUND" || error.code === "MODULE_NOT_FOUND")) { | |
| console.error( | |
| `Dashboard server entrypoint not found at ${serverPath}.` + | |
| " Have you built the dashboard yet? Please run the dashboard build command (for example, `npm run build` in the dashboard package) and try again." | |
| ); | |
| } else { | |
| console.error(`Failed to start dashboard server from ${serverPath}.`); | |
| console.error(error); | |
| } | |
| process.exitCode = 1; | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| "tasks": { | ||
| "build": { | ||
| "dependsOn": ["^build"], | ||
| "outputs": ["dist/**"] | ||
| "outputs": ["dist/**", ".output/**"] | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.