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

Highlight warn and error logs with chalk #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"start": "npm run clean && tsc --watch"
},
"dependencies": {
"chalk": "^4.1.2",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to use chalk v4 instead of v5 because v5 is ESM only

"eslint": "^8.56.0",
"glob": "^10.3.10",
"prettier": "^2.8.1",
Expand Down
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import chalk from "chalk";
import fs from "fs";
import * as util from "node:util";
import path from "path";
import semver, { SemVer } from "semver";

Expand Down Expand Up @@ -224,4 +226,17 @@ function listTargetVersions() {
console.info(targetVersions.map((version) => `- ${version}`).join("\n"));
}

const originalError = console.error;
const originalWarn = console.warn;

console.error = (...args) => {
const formattedArgs = args.map((arg) => (typeof arg === "object" ? util.inspect(arg, { depth: null, colors: true }) : chalk.red(arg)));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise error objects aren't displayed correctly

Before:
Bildschirmfoto 2025-02-28 um 13 35 42

Now:
Bildschirmfoto 2025-02-28 um 13 35 58

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we highlight objects as well?

originalError(...formattedArgs);
};

console.warn = (...args) => {
const formattedArgs = args.map((arg) => (typeof arg === "object" ? util.inspect(arg, { depth: null, colors: true }) : chalk.yellow(arg)));
originalWarn(...formattedArgs);
};

main();