bluwy / publint

Lint packaging errors
https://publint.dev
MIT License
960 stars 21 forks source link

feat: Format message without color #87

Closed regseb closed 9 months ago

regseb commented 10 months ago

The formatMessage() function returns the text: "^[[1mpkg.exports["./array"].import[0]^[[22m is ^[[1m./array.js^[[22m and is written in ^[[33mESM^[[39m, but is interpreted as ^[[33mCJS^[[39m. Consider using the ^[[33m.mjs^[[39m extension, e.g. ^[[1m./array.mjs^[[22m" (with ANSI escape codes). This pull request adds an option to the function to return the message's plain text: "pkg.exports["./array"].import[0] is ./array.js and is written in ESM, but is interpreted as CJS. Consider using the .mjs extension, e.g. ./array.mjs".

For context: I'm developing Metalint, a tool that aggregates the results of several linters. I'd like to integrate publint, but the color and bold in the messages are a problem (e.g. when the results are written in a file).

bluwy commented 10 months ago

Perhaps you can use strip-ansi to strip them out instead? There's also a few ways to have picocolors not apply any colors too: https://github.com/alexeyraspopov/picocolors/blob/b6261487e7b81aaab2440e397a356732cad9e342/picocolors.js#L3-L9

regseb commented 10 months ago

I can remove the ANSI escape codes, but I think it's a shame to add them and then remove them. It's cleaner not to put them in.

The isColorSupported variable is processed when the module is imported, it uses global variables and picocolors is an internal dependency of publint that isn't documented (you could use another library to add ANSI codes that doesn't support the NO_COLOR variable). The following code is ugly:

const hasNoColor = "NO_COLOR" in process.env;
if (!hasNoColor) {
    process.env.NO_COLOR = true;
}
import { publint } from "publint";
if (!hasNoColor) {
    delete process.env.NO_COLOR;
}

There are workarounds, but I think it's better to add this functionality directly into publint.

bluwy commented 10 months ago

I prefer to not add this additional API as there are already general ways of disabling colors. The colors are part of the messages that makes it readable. Re NO_COLORS or its other detection, most color libraries respect the same convention these days so it's not dependant on the picocolors implementation detail.

bluwy commented 9 months ago

Closing this for now.