From e37d8d4085f8ba75aa60941cd2cc9b73a5b72625 Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Thu, 8 Aug 2024 16:32:15 +0100 Subject: [PATCH] centralize dev logging using event emitter --- packages/cli-v3/src/dev/devOutput.ts | 61 +++++++++++++++++++++++ packages/cli-v3/src/dev/devSession.ts | 16 ++++-- packages/cli-v3/src/dev/workerRuntime.ts | 5 +- packages/cli-v3/src/utilities/eventBus.ts | 14 ++++++ 4 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 packages/cli-v3/src/dev/devOutput.ts create mode 100644 packages/cli-v3/src/utilities/eventBus.ts diff --git a/packages/cli-v3/src/dev/devOutput.ts b/packages/cli-v3/src/dev/devOutput.ts new file mode 100644 index 000000000..fd69b9b03 --- /dev/null +++ b/packages/cli-v3/src/dev/devOutput.ts @@ -0,0 +1,61 @@ +import { ResolvedConfig } from "@trigger.dev/core/v3/build"; +import { DevCommandOptions } from "../commands/dev.js"; +import { logger } from "../utilities/logger.js"; +import { chalkGrey, chalkLink, chalkWorker, cliLink } from "../utilities/cliOutput.js"; +import { eventBus, EventBusEventArgs } from "../utilities/eventBus.js"; + +export type DevOutputOptions = { + name: string | undefined; + dashboardUrl: string; + config: ResolvedConfig; + args: DevCommandOptions; +}; + +export function startDevOutput(options: DevOutputOptions) { + const { dashboardUrl, config } = options; + + const rebuildStarted = (...[target]: EventBusEventArgs<"rebuildStarted">) => { + logger.log(chalkGrey("○ Rebuilding background worker…")); + }; + + const buildStarted = (...[target]: EventBusEventArgs<"buildStarted">) => { + logger.log(chalkGrey("○ Building background worker…")); + }; + + const workerSkipped = () => { + logger.log(chalkGrey("○ No changes detected, skipping build…")); + }; + + const backgroundWorkerInitialized = ( + ...[worker]: EventBusEventArgs<"backgroundWorkerInitialized"> + ) => { + const testUrl = `${dashboardUrl}/projects/v3/${config.project}/test?environment=dev`; + const runsUrl = `${dashboardUrl}/projects/v3/${config.project}/runs?envSlug=dev`; + + const pipe = chalkGrey("|"); + const bullet = chalkGrey("○"); + const arrow = chalkGrey("->"); + + const testLink = chalkLink(cliLink("Test tasks", testUrl)); + const runsLink = chalkLink(cliLink("View runs", runsUrl)); + + const workerStarted = chalkGrey("Background worker started"); + const workerVersion = chalkWorker(worker.serverWorker!.version); + + logger.log( + `${bullet} ${workerStarted} ${arrow} ${workerVersion} ${pipe} ${testLink} ${pipe} ${runsLink}` + ); + }; + + eventBus.on("rebuildStarted", rebuildStarted); + eventBus.on("buildStarted", buildStarted); + eventBus.on("workerSkipped", workerSkipped); + eventBus.on("backgroundWorkerInitialized", backgroundWorkerInitialized); + + return () => { + eventBus.off("rebuildStarted", rebuildStarted); + eventBus.off("buildStarted", buildStarted); + eventBus.off("workerSkipped", workerSkipped); + eventBus.off("backgroundWorkerInitialized", backgroundWorkerInitialized); + }; +} diff --git a/packages/cli-v3/src/dev/devSession.ts b/packages/cli-v3/src/dev/devSession.ts index a81b7bcf9..e297e704f 100644 --- a/packages/cli-v3/src/dev/devSession.ts +++ b/packages/cli-v3/src/dev/devSession.ts @@ -23,6 +23,8 @@ import { EphemeralDirectory, getTmpDir } from "../utilities/tempDirectories.js"; import { copyManifestToDir } from "../build/manifests.js"; import { startWorkerRuntime } from "./workerRuntime.js"; import { chalkGrey } from "../utilities/cliOutput.js"; +import { eventBus } from "../utilities/eventBus.js"; +import { startDevOutput } from "./devOutput.js"; export type DevSessionOptions = { name: string | undefined; @@ -51,6 +53,13 @@ export async function startDevSession({ dashboardUrl, }); + const stopOutput = startDevOutput({ + name, + dashboardUrl, + config: rawConfig, + args: rawArgs, + }); + logger.debug("Starting dev session", { destination: destination.path, rawConfig }); const externalsExtension = createExternalsBuildExtension("dev", rawConfig); @@ -94,7 +103,7 @@ export async function startDevSession({ logger.debug("on-end plugin started"); if (bundled) { - logger.log(chalkGrey("○ Rebuilding background worker…")); + eventBus.emit("rebuildStarted", "dev"); } }); b.onEnd(async (result: esbuild.BuildResult) => { @@ -122,6 +131,8 @@ export async function startDevSession({ }; async function runBundle() { + eventBus.emit("buildStarted", "dev"); + const bundleResult = await bundleWorker({ target: "dev", cwd: rawConfig.workingDir, @@ -134,8 +145,6 @@ export async function startDevSession({ jsxAutomatic: rawConfig.build.jsx.automatic, }); - logger.log(chalkGrey("○ Building background worker…")); - await updateBundle(bundleResult); return bundleResult.stop; @@ -150,6 +159,7 @@ export async function startDevSession({ destination.remove(); stopBundling?.().catch((error) => {}); runtime.shutdown().catch((error) => {}); + stopOutput(); }, }; } diff --git a/packages/cli-v3/src/dev/workerRuntime.ts b/packages/cli-v3/src/dev/workerRuntime.ts index 50ae653d1..ab2b896b0 100644 --- a/packages/cli-v3/src/dev/workerRuntime.ts +++ b/packages/cli-v3/src/dev/workerRuntime.ts @@ -24,6 +24,7 @@ import { } from "@trigger.dev/core/v3/zodMessageHandler"; import { resolveDotEnvVars } from "../utilities/dotEnv.js"; import { VERSION } from "../version.js"; +import { eventBus } from "../utilities/eventBus.js"; export interface WorkerRuntime { shutdown(): Promise; @@ -157,7 +158,7 @@ class DevWorkerRuntime implements WorkerRuntime { async initializeWorker(manifest: BuildManifest, options?: { cwd?: string }): Promise { if (this.lastBuild && this.lastBuild.contentHash === manifest.contentHash) { - logger.log(chalkGrey("○ No changes detected, skipping build…")); + eventBus.emit("workerSkipped"); return; } @@ -204,6 +205,8 @@ class DevWorkerRuntime implements WorkerRuntime { backgroundWorker.serverWorker = backgroundWorkerRecord.data; this.backgroundWorkerCoordinator.registerWorker(backgroundWorker); this.lastBuild = manifest; + + eventBus.emit("backgroundWorkerInitialized", backgroundWorker); } async #getEnvVars(): Promise> { diff --git a/packages/cli-v3/src/utilities/eventBus.ts b/packages/cli-v3/src/utilities/eventBus.ts new file mode 100644 index 000000000..fd254b4d8 --- /dev/null +++ b/packages/cli-v3/src/utilities/eventBus.ts @@ -0,0 +1,14 @@ +import { BuildTarget } from "@trigger.dev/core/v3"; +import { EventEmitter } from "node:events"; +import { BackgroundWorker } from "../dev/backgroundWorker.js"; + +export type EventBusEvents = { + rebuildStarted: [BuildTarget]; + buildStarted: [BuildTarget]; + workerSkipped: []; + backgroundWorkerInitialized: [BackgroundWorker]; +}; + +export type EventBusEventArgs = EventBusEvents[T]; + +export const eventBus = new EventEmitter();