From add4bd3bed0b3eb7f2d3fc6ea4323f80e93a993a Mon Sep 17 00:00:00 2001 From: Samuel Macleod Date: Tue, 3 Dec 2024 14:10:42 +0000 Subject: [PATCH 1/2] Skip double custom build --- packages/wrangler/src/api/startDevWorker/BundlerController.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/wrangler/src/api/startDevWorker/BundlerController.ts b/packages/wrangler/src/api/startDevWorker/BundlerController.ts index 115690965f23..765f079dcbef 100644 --- a/packages/wrangler/src/api/startDevWorker/BundlerController.ts +++ b/packages/wrangler/src/api/startDevWorker/BundlerController.ts @@ -206,9 +206,6 @@ export class BundlerController extends Controller { // TODO: add comments re this ans ready ignoreInitial: true, }); - this.#customBuildWatcher.on("ready", () => { - void this.#runCustomBuild(config, String(pathsToWatch)); - }); this.#customBuildWatcher.on( "all", From 4de5675d9fb5816dce22b78b88ddb489c1868164 Mon Sep 17 00:00:00 2001 From: Samuel Macleod Date: Fri, 10 Jan 2025 20:18:07 +0000 Subject: [PATCH 2/2] Fix logging --- .../api/startDevWorker/BundlerController.ts | 5 ++- .../src/deployment-bundle/run-custom-build.ts | 34 +++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/packages/wrangler/src/api/startDevWorker/BundlerController.ts b/packages/wrangler/src/api/startDevWorker/BundlerController.ts index 765f079dcbef..6b4a2719f763 100644 --- a/packages/wrangler/src/api/startDevWorker/BundlerController.ts +++ b/packages/wrangler/src/api/startDevWorker/BundlerController.ts @@ -203,9 +203,12 @@ export class BundlerController extends Controller { this.#customBuildWatcher = watch(pathsToWatch, { persistent: true, - // TODO: add comments re this ans ready + // The initial custom build is always done in getEntry() ignoreInitial: true, }); + this.#customBuildWatcher.on("ready", () => { + void this.#runCustomBuild(config, String(pathsToWatch)); + }); this.#customBuildWatcher.on( "all", diff --git a/packages/wrangler/src/deployment-bundle/run-custom-build.ts b/packages/wrangler/src/deployment-bundle/run-custom-build.ts index abc3d4ee477d..ebad10d7afdd 100644 --- a/packages/wrangler/src/deployment-bundle/run-custom-build.ts +++ b/packages/wrangler/src/deployment-bundle/run-custom-build.ts @@ -1,5 +1,7 @@ import { existsSync, statSync } from "node:fs"; import path from "node:path"; +import { Writable } from "node:stream"; +import chalk from "chalk"; import { execaCommand } from "execa"; import { configFileName } from "../config"; import { UserError } from "../errors"; @@ -19,17 +21,37 @@ export async function runCustomBuild( configPath: string | undefined ) { if (build.command) { - logger.log("Running custom build:", build.command); + logger.log(chalk.blue("[custom build]"), "Running:", build.command); try { - await execaCommand(build.command, { + const res = execaCommand(build.command, { shell: true, - // we keep these two as "inherit" so that - // logs are still visible. - stdout: "inherit", - stderr: "inherit", ...(build.cwd && { cwd: build.cwd }), }); + res.stdout?.pipe( + new Writable({ + write(chunk: Buffer, _, callback) { + const lines = chunk.toString().split("\n"); + for (const line of lines) { + logger.log(chalk.blue("[custom build]"), line); + } + callback(); + }, + }) + ); + res.stderr?.pipe( + new Writable({ + write(chunk: Buffer, _, callback) { + const lines = chunk.toString().split("\n"); + for (const line of lines) { + logger.log(chalk.red("[custom build]"), line); + } + callback(); + }, + }) + ); + await res; } catch (e) { + logger.error(e); throw new UserError( `Running custom build \`${build.command}\` failed. There are likely more logs from your build command above.`, {