Skip to content

Commit

Permalink
fix(cli): Redirect messages to stderr (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari authored Oct 29, 2024
1 parent a3889a3 commit 76e98e3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-actors-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@swc/cli": patch
---

fix(cli): Redirect messages to stderr
24 changes: 15 additions & 9 deletions packages/cli/src/swc/dir.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { existsSync, promises } from "fs";
import { dirname, resolve } from "path";
import Piscina from "piscina";
import { stderr } from "process";
import { format } from "util";
import { CompileStatus } from "./constants";
import { Callbacks, CliOptions } from "./options";
import { exists, getDest } from "./util";
Expand Down Expand Up @@ -208,22 +210,22 @@ async function initialCompilation(
if (copied) {
message += `copied ${copied} ${copied > 1 ? "files" : "file"}`;
}
message += ` with swc (%dms)`;
message += format(" with swc (%dms)\n", duration.toFixed(2));

if (callbacks?.onSuccess) {
if (!failed) {
callbacks.onSuccess({ duration, compiled, copied });
}
} else if (!quiet) {
console.log(message, duration.toFixed(2));
stderr.write(message);
}
}

if (failed) {
if (callbacks?.onFail) {
callbacks.onFail({ duration, reasons });
} else {
console.log(
console.error(
`Failed to compile ${failed} ${
failed !== 1 ? "files" : "file"
} with swc.`
Expand Down Expand Up @@ -316,9 +318,11 @@ async function watchCompilation(
filename,
});
} else if (!quiet) {
console.log(
`Successfully compiled ${filename} with swc (%dms)`,
duration.toFixed(2)
stderr.write(
format(
`Successfully compiled ${filename} with swc (%dms)\n`,
duration.toFixed(2)
)
);
}
}
Expand Down Expand Up @@ -353,9 +357,11 @@ async function watchCompilation(
filename,
});
} else if (!quiet) {
console.log(
`Successfully copied ${filename} with swc (%dms)`,
duration.toFixed(2)
stderr.write(
format(
`Successfully copied ${filename} with swc (%dms)\n`,
duration.toFixed(2)
)
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/swc/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as swc from "@swc/core";
import slash from "slash";
import { mkdirSync, writeFileSync, promises } from "fs";
import { dirname, join, relative } from "path";
import { stderr } from "process";

export async function exists(path: string): Promise<boolean> {
let pathExists = true;
Expand Down Expand Up @@ -113,10 +114,10 @@ export function assertCompilationResult<T>(
}
if (!quiet && compiled + copied > 0) {
const copyResult = copied === 0 ? " " : ` (copied ${copied}) `;
console.info(
stderr.write(
`Successfully compiled ${compiled} ${
compiled !== 1 ? "files" : "file"
}${copyResult}with swc.`
}${copyResult}with swc.\n`
);
}

Expand Down

0 comments on commit 76e98e3

Please sign in to comment.