Skip to content

Commit cba291e

Browse files
refactor: replace chalk with picocolors (#105)
1 parent 1838100 commit cba291e

File tree

10 files changed

+50
-37
lines changed

10 files changed

+50
-37
lines changed

lib/colors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import picocolors from 'picocolors';
2+
import { WriteStream } from 'tty';
3+
4+
// makes color detection more accurate using node's own API for it
5+
// https://github.com/alexeyraspopov/picocolors/issues/85
6+
export const pc = picocolors.createColors(WriteStream.prototype.hasColors());

lib/help.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import chalk from 'chalk';
1+
import { pc } from './colors';
22

33
export default function help() {
44
// eslint-disable-next-line no-console
55
console.log(`
6-
${chalk.bold('pkg')} [options] <input>
6+
${pc.bold('pkg')} [options] <input>
77
8-
${chalk.dim('Options:')}
8+
${pc.dim('Options:')}
99
1010
-h, --help output usage information
1111
-v, --version output pkg version
@@ -23,29 +23,25 @@ export default function help() {
2323
--no-dict comma-separated list of packages names to ignore dictionaries. Use --no-dict * to disable all dictionaries
2424
-C, --compress [default=None] compression algorithm = Brotli or GZip
2525
26-
${chalk.dim('Examples:')}
26+
${pc.dim('Examples:')}
2727
28-
${chalk.gray('–')} Makes executables for Linux, macOS and Windows
29-
${chalk.cyan('$ pkg index.js')}
30-
${chalk.gray('–')} Takes package.json from cwd and follows 'bin' entry
31-
${chalk.cyan('$ pkg .')}
32-
${chalk.gray('–')} Makes executable for particular target machine
33-
${chalk.cyan('$ pkg -t node14-win-arm64 index.js')}
34-
${chalk.gray('–')} Makes executables for target machines of your choice
35-
${chalk.cyan('$ pkg -t node16-linux,node18-linux,node18-win index.js')}
36-
${chalk.gray(
37-
'–',
38-
)} Bakes '--expose-gc' and '--max-heap-size=34' into executable
39-
${chalk.cyan('$ pkg --options "expose-gc,max-heap-size=34" index.js')}
40-
${chalk.gray('–')} Consider packageA and packageB to be public
41-
${chalk.cyan('$ pkg --public-packages "packageA,packageB" index.js')}
42-
${chalk.gray('–')} Consider all packages to be public
43-
${chalk.cyan('$ pkg --public-packages "*" index.js')}
44-
${chalk.gray('–')} Bakes '--expose-gc' into executable
45-
${chalk.cyan('$ pkg --options expose-gc index.js')}
46-
${chalk.gray(
47-
'–',
48-
)} reduce size of the data packed inside the executable with GZip
49-
${chalk.cyan('$ pkg --compress GZip index.js')}
28+
${pc.gray('–')} Makes executables for Linux, macOS and Windows
29+
${pc.cyan('$ pkg index.js')}
30+
${pc.gray('–')} Takes package.json from cwd and follows 'bin' entry
31+
${pc.cyan('$ pkg .')}
32+
${pc.gray('–')} Makes executable for particular target machine
33+
${pc.cyan('$ pkg -t node14-win-arm64 index.js')}
34+
${pc.gray('–')} Makes executables for target machines of your choice
35+
${pc.cyan('$ pkg -t node16-linux,node18-linux,node18-win index.js')}
36+
${pc.gray('–')} Bakes '--expose-gc' and '--max-heap-size=34' into executable
37+
${pc.cyan('$ pkg --options "expose-gc,max-heap-size=34" index.js')}
38+
${pc.gray('–')} Consider packageA and packageB to be public
39+
${pc.cyan('$ pkg --public-packages "packageA,packageB" index.js')}
40+
${pc.gray('–')} Consider all packages to be public
41+
${pc.cyan('$ pkg --public-packages "*" index.js')}
42+
${pc.gray('–')} Bakes '--expose-gc' into executable
43+
${pc.cyan('$ pkg --options expose-gc index.js')}
44+
${pc.gray('–')} reduce size of the data packed inside the executable with GZip
45+
${pc.cyan('$ pkg --compress GZip index.js')}
5046
`);
5147
}

lib/refiner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path';
2-
import chalk from 'chalk';
2+
import { pc } from './colors';
33
import {
44
STORE_LINKS,
55
retrieveDenominator,
@@ -43,7 +43,7 @@ function purgeTopDirectories(records: FileRecords) {
4343

4444
if (links3) {
4545
delete records[file];
46-
log.debug(chalk.cyan('Deleting record file :', file));
46+
log.debug(pc.cyan(`Deleting record file: ${file}`));
4747
found = true;
4848
}
4949
}

lib/walker.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import assert from 'assert';
44
import fs from 'fs-extra';
55
import globby from 'globby';
66
import path from 'path';
7-
import chalk from 'chalk';
87
import { builtinModules } from 'module';
98
import picomatch from 'picomatch';
109

@@ -23,6 +22,7 @@ import {
2322
toNormalizedRealPath,
2423
} from './common';
2524

25+
import { pc } from './colors';
2626
import { follow } from './follow';
2727
import { log, wasReported } from './log';
2828
import * as detector from './detector';
@@ -468,7 +468,9 @@ class Walker {
468468
const { ignore } = pkgOptions.get();
469469
if (ignore) {
470470
// check if the file matches one of the ignore regex patterns
471-
const match = picomatch.isMatch(realFile, ignore);
471+
const match = picomatch.isMatch(realFile, ignore, {
472+
windows: win32,
473+
});
472474

473475
if (match) {
474476
log.debug(
@@ -818,7 +820,7 @@ class Walker {
818820
`%2: ${record.file}`,
819821
]);
820822
} else {
821-
log[level](`${chalk.yellow(failure.message)} in ${record.file}`);
823+
log[level](`${pc.yellow(failure.message)} in ${record.file}`);
822824
}
823825

824826
return;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
"@babel/parser": "7.23.0",
2727
"@babel/types": "7.23.0",
2828
"@yao-pkg/pkg-fetch": "3.5.13",
29-
"chalk": "^4.1.2",
3029
"fs-extra": "^9.1.0",
3130
"globby": "^11.1.0",
3231
"into-stream": "^6.0.0",
3332
"minimist": "^1.2.6",
3433
"multistream": "^4.1.0",
3534
"picomatch": "^4.0.2",
35+
"picocolors": "^1.1.0",
3636
"prebuild-install": "7.1.1",
3737
"resolve": "^1.22.0",
3838
"stream-meter": "^1.0.4"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"private": true,
33
"peerOptionalDependencies": {
4-
"chalk": "*"
4+
"chalk": "^4.1.2"
55
}
66
}

test/test-80-compression/test-x.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* eslint-disable no-unused-vars */
44

55
const m = require('minimist');
6-
const c = require('chalk');
6+
const pc = require('picocolors');
77

88
const loremIpsum =
99
'Unus audio pluribus sibi quibusdam amicitias habere qua satis molestum sapientes molestum est vel frui non pluribus nimias possit quam esse sollicitum adducas persequantur esse audio nihil sollicitum laxissimas enim rerum vel non ad tamquam habitos implicari placuisse quibusdam nihil.';

test/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
'use strict';
44

5-
const chalk = require('chalk');
65
const globby = require('globby');
76
const path = require('path');
7+
const pc = require('picocolors');
88
const utils = require('./utils.js');
99
const host = 'node' + process.version.match(/^v(\d+)/)[1];
1010
let target = process.argv[2] || 'host';
@@ -73,8 +73,8 @@ files.sort().some(function (file) {
7373
});
7474
} catch (error) {
7575
console.log();
76-
console.log(`> ${chalk.red('Error!')} ${error.message}`);
77-
console.log(`> ${chalk.red('Error!')} ${file} FAILED (in ${target})`);
76+
console.log(`> ${pc.red('Error!')} ${error.message}`);
77+
console.log(`> ${pc.red('Error!')} ${file} FAILED (in ${target})`);
7878
process.exit(2);
7979
}
8080
console.log(file, 'ok');

test/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ module.exports.pkg.sync = function (args, opts) {
140140
args.unshift(binPath);
141141
assert(es5, 'RUN BABEL FIRST!'); // args.unshift('-r', 'babel-register');
142142
if (Array.isArray(opts)) opts = { stdio: opts };
143+
// spawn uses process.env if no opts.env is provided, we need to manually add it if set
144+
if (!opts) opts = { env: { NO_COLOR: '1', ...process.env } };
145+
else if (!opts.env || !opts.env.NO_COLOR)
146+
opts.env = { NO_COLOR: '1', ...process.env, ...opts.env };
143147
try {
144148
const ss = module.exports.spawn.sync;
145149
return ss('node', args, opts);

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3737,6 +3737,11 @@ picocolors@^1.0.0, picocolors@^1.0.1:
37373737
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.1.tgz#a8ad579b571952f0e5d25892de5445bcfe25aaa1"
37383738
integrity sha512-anP1Z8qwhkbmu7MFP5iTt+wQKXgwzf7zTyGlcdzabySa9vd0Xt392U0rVmz9poOaBj0uHJKyyo9/upk0HrEQew==
37393739

3740+
picocolors@^1.1.0:
3741+
version "1.1.0"
3742+
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.1.0.tgz#5358b76a78cde483ba5cef6a9dc9671440b27d59"
3743+
integrity sha512-TQ92mBOW0l3LeMeyLV6mzy/kWr8lkd/hp3mTg7wYK7zJhuBStmGMBG0BdeDZS/dZx1IukaX6Bk11zcln25o1Aw==
3744+
37403745
picomatch@^2.3.1:
37413746
version "2.3.1"
37423747
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"

0 commit comments

Comments
 (0)