publint icon indicating copy to clipboard operation
publint copied to clipboard

feat: allow to strip ANSI for the result of `formatMessage`

Open zanminkian opened this issue 1 year ago • 3 comments

import { formatMessage } from 'publint/utils'
import fs from 'node:fs/promises'

const pkg = JSON.parse(
  await fs.readFile('./path/to/package/package.json', 'utf8')
)

for (const message of messages) {
  console.log(formatMessage(message, pkg)) // Color
}

I'm creating a eslint plugin eslint-plugin-publint. I don't need ANSI in the result of formatMessage. Support an option to strip ANSI. Otherwise, I have to use (or copy the code of) strip-ansi to strip it. :)

zanminkian avatar Aug 19 '24 10:08 zanminkian

I guess it's not hard to add a ,color = false option to disable colors, feel free to send a PR for that if you'd like. But at the same time, I'm not sure if eslint-plugin-publint makes sense to me. publint is mean to run of published files, not source code, which eslint covers. So you'd usually run publint separately as its own process.

bluwy avatar Aug 19 '24 15:08 bluwy

publint is mean to run of published files, not source code, which eslint covers. So you'd usually run publint separately as its own process.

Generally, publint report suggestion/warning/error on package.json file, so does eslint. From this point of view, eslint plugin for publint is possible. Am I wrong?

zanminkian avatar Aug 21 '24 03:08 zanminkian

It does currently only reports on the package.json, but I'd like to expand it towards more files as well, particularly on published files (something I'm working on in the next version). So while you could make it work as an ESLint plugin today, it might not be suitable to do so in the future.

bluwy avatar Aug 21 '24 04:08 bluwy

i use it to replace colors

// inspired by https://github.com/chalk/ansi-regex/blob/main/index.js#L1
export const ANSI_COLORS_REGEXP =
	/[\u001B\u009B][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d\/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\u0007)|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))/g;

But then i just replace functions which add ANSI colors

picocolors: {
					default: `{
						bold: (s) => s,
						yellow: (s) => s
					}`,
				},

Glad to see that now i can just color: false with https://github.com/bluwy/publint/pull/110

kravetsone avatar Sep 16 '24 18:09 kravetsone