Skip to content

Commit

Permalink
Fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Jan 10, 2025
1 parent add4bd3 commit 4de5675
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,12 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {

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",
Expand Down
34 changes: 28 additions & 6 deletions packages/wrangler/src/deployment-bundle/run-custom-build.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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.`,
{
Expand Down

0 comments on commit 4de5675

Please sign in to comment.