Skip to content

Commit e7abd6b

Browse files
committed
picocolors → styleText
1 parent 5b5cda5 commit e7abd6b

10 files changed

Lines changed: 63 additions & 38 deletions

File tree

packages/knip/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
"minimist": "^1.2.8",
9999
"oxc-parser": "^0.126.0",
100100
"oxc-resolver": "^11.19.1",
101-
"picocolors": "^1.1.1",
102101
"picomatch": "^4.0.4",
103102
"smol-toml": "^1.6.1",
104103
"strip-json-comments": "5.0.3",

packages/knip/src/reporters/trace.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import pc from 'picocolors';
21
import type { GraphExplorer } from '../graph-explorer/explorer.ts';
32
import type { ExportsTreeNode } from '../graph-explorer/operations/build-exports-tree.ts';
43
import type { ModuleGraph } from '../types/module-graph.ts';
4+
import st from '../util/colors.ts';
55
import type { MainOptions } from '../util/create-options.ts';
66
import { toRelative } from '../util/path.ts';
77
import { toRegexOrString } from '../util/regex.ts';
@@ -31,8 +31,8 @@ export default ({ graph, explorer, options, workspaceFilePathFilter }: TraceRepo
3131
if (seen.has(key)) continue;
3232
seen.add(key);
3333
table.row();
34-
table.cell('filePath', pc.whiteBright(`${toRel(_import.filePath)}${pos}`));
35-
table.cell('package', pc.cyanBright(packageName));
34+
table.cell('filePath', st.whiteBright(`${toRel(_import.filePath)}${pos}`));
35+
table.cell('package', st.cyanBright(packageName));
3636
}
3737
}
3838
for (const line of table.toRows()) console.log(line);

packages/knip/src/reporters/util/util.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
import picocolors from 'picocolors';
21
import { ISSUE_TYPE_TITLE, SYMBOL_TYPE } from '../../constants.ts';
32
import type { Issue, IssueRecords, IssueSeverity, IssueSymbol, IssueType } from '../../types/issues.ts';
3+
import st from '../../util/colors.ts';
44
import { relative } from '../../util/path.ts';
55
import { Table } from '../../util/table.ts';
66

77
const plain = (text: string) => text;
8-
export const dim = picocolors.gray;
9-
export const bright = picocolors.whiteBright;
10-
const yellow = picocolors.yellow;
8+
export const dim = st.gray;
9+
export const bright = st.whiteBright;
1110

1211
export const getIssueTypeTitle = (reportType: keyof typeof ISSUE_TYPE_TITLE) => ISSUE_TYPE_TITLE[reportType];
1312

1413
export const getColoredTitle = (title: string, count: number) =>
15-
`${picocolors.yellowBright(picocolors.underline(title))} (${count})`;
14+
`${st.style(['yellowBright', 'underline'], title)} (${count})`;
1615

17-
export const getDimmedTitle = (title: string, count: number) =>
18-
`${yellow(`${picocolors.underline(title)} (${count})`)}`;
16+
export const getDimmedTitle = (title: string, count: number) => `${st.yellow(`${st.underline(title)} (${count})`)}`;
1917

2018
type LogIssueLine = {
2119
owner?: string;
@@ -29,7 +27,7 @@ export const getIssueLine = ({ owner, filePath, symbols, parentSymbol, severity
2927
const symbol = symbols ? `: ${symbols.map(s => s.symbol).join(', ')}` : '';
3028
const parent = parentSymbol ? ` (${parentSymbol})` : '';
3129
const print = severity === 'warn' ? dim : plain;
32-
return `${owner ? `${picocolors.cyan(owner)} ` : ''}${print(`${relative(cwd, filePath)}${symbol}${parent}`)}`;
30+
return `${owner ? `${st.cyan(owner)} ` : ''}${print(`${relative(cwd, filePath)}${symbol}${parent}`)}`;
3331
};
3432

3533
export const convert = (issue: Issue | IssueSymbol) => ({

packages/knip/src/reporters/watch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import picocolors from 'picocolors';
1+
import st from '../util/colors.ts';
22
import type { ConsoleStreamer } from '../ConsoleStreamer.ts';
33
import type { Entries } from '../types/entries.ts';
44
import type { Issues } from '../types/issues.ts';
@@ -28,7 +28,7 @@ export default (options: MainOptions, { issues, streamer, duration, size }: Watc
2828

2929
if (issuesForType.length > 0) {
3030
if (title) {
31-
lines.push(`${picocolors.yellowBright(picocolors.underline(title))} (${issuesForType.length})`);
31+
lines.push(`${st.style(['yellowBright', 'underline'], title)} (${issuesForType.length})`);
3232
}
3333
lines.push(...getTableForType(issuesForType, options.cwd).toRows());
3434
}
@@ -43,8 +43,8 @@ export default (options: MainOptions, { issues, streamer, duration, size }: Watc
4343

4444
const messages =
4545
totalIssues === 0
46-
? ['✂️ Excellent, Knip found no issues.', '', picocolors.gray(summary)]
47-
: [...lines, '', picocolors.gray(summary)];
46+
? ['✂️ Excellent, Knip found no issues.', '', st.gray(summary)]
47+
: [...lines, '', st.gray(summary)];
4848

4949
if (options.isDebug) console.log(messages.join('\n'));
5050
else streamer.cast(messages);

packages/knip/src/util/colors.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { styleText } from 'node:util';
2+
3+
type Modifier = Parameters<typeof styleText>[0];
4+
type Input = string | number | null | undefined;
5+
6+
const isColors = !process.env.NO_COLOR && (process.env.FORCE_COLOR === '1' || !!process.stdout.isTTY);
7+
8+
const make =
9+
(mod: Modifier) =>
10+
(text: Input): string => {
11+
const str = String(text);
12+
return isColors ? styleText(mod, str, { validateStream: false }) : str;
13+
};
14+
15+
const st = {
16+
red: make('red'),
17+
green: make('green'),
18+
yellow: make('yellow'),
19+
cyan: make('cyan'),
20+
white: make('white'),
21+
gray: make('gray'),
22+
dim: make('dim'),
23+
underline: make('underline'),
24+
cyanBright: make('cyanBright'),
25+
whiteBright: make('whiteBright'),
26+
yellowBright: make('yellowBright'),
27+
style: (mods: Modifier, text: Input): string => {
28+
const str = String(text);
29+
return isColors ? styleText(mods, str, { validateStream: false }) : str;
30+
},
31+
};
32+
33+
export default st;

packages/knip/src/util/debug.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* oxlint-disable no-console */
22
import util, { parseArgs } from 'node:util';
3-
import picocolors from 'picocolors';
3+
import st from './colors.ts';
44

55
const { values } = parseArgs({ strict: false, options: { debug: { type: 'boolean' } } });
66

@@ -16,9 +16,7 @@ const inspectOptions = { maxArrayLength: null, depth: null, colors: IS_COLORS };
1616
export const inspect = (obj: unknown) => console.log(util.inspect(obj, inspectOptions));
1717

1818
const ctx = (text: string | [string, string]) =>
19-
typeof text === 'string'
20-
? picocolors.yellow(`[${text}]`)
21-
: `${picocolors.yellow(`[${text[0]}]`)} ${picocolors.cyan(text[1])}`;
19+
typeof text === 'string' ? st.yellow(`[${text}]`) : `${st.yellow(`[${text[0]}]`)} ${st.cyan(text[1])}`;
2220

2321
// Inspect arrays, otherwise Node [will, knip, ...n-100 more items]
2422
const logArray = (collection: string[]) => {

packages/knip/src/util/log.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* oxlint-disable no-console */
2-
import picocolors from 'picocolors';
2+
import st from './colors.ts';
33

44
export const logWarning = (prefix: string, message: string) => {
5-
console.warn(`${picocolors.yellow(prefix)}: ${message}`);
5+
console.warn(`${st.yellow(prefix)}: ${message}`);
66
};
77

88
export const logError = (prefix: string, message: string) => {
9-
console.error(`${picocolors.red(prefix)}: ${message}`);
9+
console.error(`${st.red(prefix)}: ${message}`);
1010
};

packages/knip/src/util/trace.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pc from 'picocolors';
1+
import st from './colors.ts';
22
import type { ExportsTreeNode } from '../graph-explorer/operations/build-exports-tree.ts';
33

44
export interface TraceMemberStatus {
@@ -14,13 +14,13 @@ export const formatTrace = (
1414
): string => {
1515
const lines: string[] = [];
1616

17-
const file = pc.white;
18-
const id = pc.cyanBright;
19-
const ref = pc.cyanBright;
20-
const via = pc.dim;
21-
const ok = pc.green;
22-
const fail = pc.red;
23-
const dim = pc.dim;
17+
const file = st.white;
18+
const id = st.cyanBright;
19+
const ref = st.cyanBright;
20+
const via = st.dim;
21+
const ok = st.green;
22+
const fail = st.red;
23+
const dim = st.dim;
2424

2525
const entryMarker = node.isEntry ? dim(' ⎆') : '';
2626
lines.push(`${file(toRelative(node.filePath))}${dim(':')}${id(node.identifier)}${entryMarker}`);

packages/knip/test/helpers/diff.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/* oxlint-disable no-console */
2-
import pc from 'picocolors';
2+
import st from '../../src/util/colors.ts';
33

44
export const showDiff = (actual: string, expected: string) => {
55
const actualLines = actual.split('\n');
66
const expectedLines = expected.split('\n');
77
const maxLen = Math.max(actualLines.length, expectedLines.length);
8-
console.log(`\ndiff (${pc.red('-')} expected, ${pc.green('+')} actual):`);
8+
console.log(`\ndiff (${st.red('-')} expected, ${st.green('+')} actual):`);
99
for (let i = 0; i < maxLen; i++) {
1010
const a = actualLines[i];
1111
const e = expectedLines[i];
1212
if (a === e) {
1313
console.log(` ${a}`);
1414
} else {
15-
if (a !== undefined) console.log(`${pc.red('-')} ${a}`);
16-
if (e !== undefined) console.log(`${pc.green('+')} ${e}`);
15+
if (a !== undefined) console.log(`${st.red('-')} ${a}`);
16+
if (e !== undefined) console.log(`${st.green('+')} ${e}`);
1717
}
1818
}
1919
console.log();

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)