Skip to content

Commit

Permalink
fix(cli): Redirect messages to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Aug 30, 2024
1 parent d096fdc commit 5dc8893
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
25 changes: 16 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 { CliOptions } from "./options";
import { exists, getDest } from "./util";
Expand Down Expand Up @@ -196,13 +198,14 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) {
if (copied) {
message += `copied ${copied} ${copied > 1 ? "files" : "file"}`;
}
message += ` with swc (%dms)`;
message += ` with swc (%dms)\n`;
message = format(message, (end[1] / 1000000).toFixed(2));

console.log(message, (end[1] / 1000000).toFixed(2));
stderr.write(message);
}

if (failed) {
console.log(
console.error(
`Failed to compile ${failed} ${
failed !== 1 ? "files" : "file"
} with swc.`
Expand Down Expand Up @@ -276,9 +279,11 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) {
});
if (!quiet && result === CompileStatus.Compiled) {
const end = process.hrtime(start);
console.log(
`Successfully compiled ${filename} with swc (%dms)`,
(end[1] / 1000000).toFixed(2)
stderr.write(
format(
`Successfully compiled ${filename} with swc (%dms)\n`,
(end[1] / 1000000).toFixed(2)
)
);
}
} catch (err: any) {
Expand All @@ -294,9 +299,11 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) {
);
if (!quiet && result === CompileStatus.Copied) {
const end = process.hrtime(start);
console.log(
`Successfully copied ${filename} with swc (%dms)`,
(end[1] / 1000000).toFixed(2)
stderr.write(
format(
`Successfully copied ${filename} with swc (%dms)\n`,
(end[1] / 1000000).toFixed(2)
)
);
}
} catch (err: any) {
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 5dc8893

Please sign in to comment.