Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generators that console log should be captured and redirected to logfile #282

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/commands/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const action = (program: Command) => async (options: RootCommandOptions)

await loadLocalSpecsSet();

log.overrideConsole();

const shell = options.shell ?? ((await inferShell()) as unknown as Shell | undefined);
if (shell == null) {
program.error(`Unable to identify shell, use the -s/--shell option to provide your shell`, { exitCode: 1 });
Expand Down
18 changes: 17 additions & 1 deletion src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/* eslint-disable @typescript-eslint/no-explicit-any */

import os from "node:os";
import path from "node:path";
import fs from "node:fs";
Expand Down Expand Up @@ -28,9 +30,23 @@ const debug = (content: object) => {
});
};

const getLogFunction =
(level: "error" | "log") =>
(...data: any[]) =>
debug({ msg: `console.${level}`, data: data.toString() });

const logConsole = {
...console,
log: getLogFunction("log"),
error: getLogFunction("error"),
};

// eslint-disable-next-line no-global-assign
const overrideConsole = () => (console = logConsole);

export const enable = async () => {
await reset();
logEnabled = true;
};

export default { reset, debug, enable };
export default { reset, debug, enable, overrideConsole };
Loading