Skip to content

Commit

Permalink
refactor: replace chalk with picocolors (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev authored Oct 16, 2024
1 parent 1838100 commit cba291e
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 37 deletions.
6 changes: 6 additions & 0 deletions lib/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import picocolors from 'picocolors';
import { WriteStream } from 'tty';

// makes color detection more accurate using node's own API for it
// https://github.com/alexeyraspopov/picocolors/issues/85
export const pc = picocolors.createColors(WriteStream.prototype.hasColors());
48 changes: 22 additions & 26 deletions lib/help.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import chalk from 'chalk';
import { pc } from './colors';

export default function help() {
// eslint-disable-next-line no-console
console.log(`
${chalk.bold('pkg')} [options] <input>
${pc.bold('pkg')} [options] <input>
${chalk.dim('Options:')}
${pc.dim('Options:')}
-h, --help output usage information
-v, --version output pkg version
Expand All @@ -23,29 +23,25 @@ export default function help() {
--no-dict comma-separated list of packages names to ignore dictionaries. Use --no-dict * to disable all dictionaries
-C, --compress [default=None] compression algorithm = Brotli or GZip
${chalk.dim('Examples:')}
${pc.dim('Examples:')}
${chalk.gray('–')} Makes executables for Linux, macOS and Windows
${chalk.cyan('$ pkg index.js')}
${chalk.gray('–')} Takes package.json from cwd and follows 'bin' entry
${chalk.cyan('$ pkg .')}
${chalk.gray('–')} Makes executable for particular target machine
${chalk.cyan('$ pkg -t node14-win-arm64 index.js')}
${chalk.gray('–')} Makes executables for target machines of your choice
${chalk.cyan('$ pkg -t node16-linux,node18-linux,node18-win index.js')}
${chalk.gray(
'–',
)} Bakes '--expose-gc' and '--max-heap-size=34' into executable
${chalk.cyan('$ pkg --options "expose-gc,max-heap-size=34" index.js')}
${chalk.gray('–')} Consider packageA and packageB to be public
${chalk.cyan('$ pkg --public-packages "packageA,packageB" index.js')}
${chalk.gray('–')} Consider all packages to be public
${chalk.cyan('$ pkg --public-packages "*" index.js')}
${chalk.gray('–')} Bakes '--expose-gc' into executable
${chalk.cyan('$ pkg --options expose-gc index.js')}
${chalk.gray(
'–',
)} reduce size of the data packed inside the executable with GZip
${chalk.cyan('$ pkg --compress GZip index.js')}
${pc.gray('–')} Makes executables for Linux, macOS and Windows
${pc.cyan('$ pkg index.js')}
${pc.gray('–')} Takes package.json from cwd and follows 'bin' entry
${pc.cyan('$ pkg .')}
${pc.gray('–')} Makes executable for particular target machine
${pc.cyan('$ pkg -t node14-win-arm64 index.js')}
${pc.gray('–')} Makes executables for target machines of your choice
${pc.cyan('$ pkg -t node16-linux,node18-linux,node18-win index.js')}
${pc.gray('–')} Bakes '--expose-gc' and '--max-heap-size=34' into executable
${pc.cyan('$ pkg --options "expose-gc,max-heap-size=34" index.js')}
${pc.gray('–')} Consider packageA and packageB to be public
${pc.cyan('$ pkg --public-packages "packageA,packageB" index.js')}
${pc.gray('–')} Consider all packages to be public
${pc.cyan('$ pkg --public-packages "*" index.js')}
${pc.gray('–')} Bakes '--expose-gc' into executable
${pc.cyan('$ pkg --options expose-gc index.js')}
${pc.gray('–')} reduce size of the data packed inside the executable with GZip
${pc.cyan('$ pkg --compress GZip index.js')}
`);
}
4 changes: 2 additions & 2 deletions lib/refiner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import chalk from 'chalk';
import { pc } from './colors';
import {
STORE_LINKS,
retrieveDenominator,
Expand Down Expand Up @@ -43,7 +43,7 @@ function purgeTopDirectories(records: FileRecords) {

if (links3) {
delete records[file];
log.debug(chalk.cyan('Deleting record file :', file));
log.debug(pc.cyan(`Deleting record file: ${file}`));
found = true;
}
}
Expand Down
8 changes: 5 additions & 3 deletions lib/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import assert from 'assert';
import fs from 'fs-extra';
import globby from 'globby';
import path from 'path';
import chalk from 'chalk';
import { builtinModules } from 'module';
import picomatch from 'picomatch';

Expand All @@ -23,6 +22,7 @@ import {
toNormalizedRealPath,
} from './common';

import { pc } from './colors';
import { follow } from './follow';
import { log, wasReported } from './log';
import * as detector from './detector';
Expand Down Expand Up @@ -468,7 +468,9 @@ class Walker {
const { ignore } = pkgOptions.get();
if (ignore) {
// check if the file matches one of the ignore regex patterns
const match = picomatch.isMatch(realFile, ignore);
const match = picomatch.isMatch(realFile, ignore, {
windows: win32,
});

if (match) {
log.debug(
Expand Down Expand Up @@ -818,7 +820,7 @@ class Walker {
`%2: ${record.file}`,
]);
} else {
log[level](`${chalk.yellow(failure.message)} in ${record.file}`);
log[level](`${pc.yellow(failure.message)} in ${record.file}`);
}

return;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
"@babel/parser": "7.23.0",
"@babel/types": "7.23.0",
"@yao-pkg/pkg-fetch": "3.5.13",
"chalk": "^4.1.2",
"fs-extra": "^9.1.0",
"globby": "^11.1.0",
"into-stream": "^6.0.0",
"minimist": "^1.2.6",
"multistream": "^4.1.0",
"picomatch": "^4.0.2",
"picocolors": "^1.1.0",
"prebuild-install": "7.1.1",
"resolve": "^1.22.0",
"stream-meter": "^1.0.4"
Expand Down
2 changes: 1 addition & 1 deletion test/test-79-npm/require_optional/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"peerOptionalDependencies": {
"chalk": "*"
"chalk": "^4.1.2"
}
}
2 changes: 1 addition & 1 deletion test/test-80-compression/test-x.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable no-unused-vars */

const m = require('minimist');
const c = require('chalk');
const pc = require('picocolors');

const loremIpsum =
'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.';
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

'use strict';

const chalk = require('chalk');
const globby = require('globby');
const path = require('path');
const pc = require('picocolors');
const utils = require('./utils.js');
const host = 'node' + process.version.match(/^v(\d+)/)[1];
let target = process.argv[2] || 'host';
Expand Down Expand Up @@ -73,8 +73,8 @@ files.sort().some(function (file) {
});
} catch (error) {
console.log();
console.log(`> ${chalk.red('Error!')} ${error.message}`);
console.log(`> ${chalk.red('Error!')} ${file} FAILED (in ${target})`);
console.log(`> ${pc.red('Error!')} ${error.message}`);
console.log(`> ${pc.red('Error!')} ${file} FAILED (in ${target})`);
process.exit(2);
}
console.log(file, 'ok');
Expand Down
4 changes: 4 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ module.exports.pkg.sync = function (args, opts) {
args.unshift(binPath);
assert(es5, 'RUN BABEL FIRST!'); // args.unshift('-r', 'babel-register');
if (Array.isArray(opts)) opts = { stdio: opts };
// spawn uses process.env if no opts.env is provided, we need to manually add it if set
if (!opts) opts = { env: { NO_COLOR: '1', ...process.env } };
else if (!opts.env || !opts.env.NO_COLOR)
opts.env = { NO_COLOR: '1', ...process.env, ...opts.env };
try {
const ss = module.exports.spawn.sync;
return ss('node', args, opts);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3737,6 +3737,11 @@ picocolors@^1.0.0, picocolors@^1.0.1:
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.1.tgz#a8ad579b571952f0e5d25892de5445bcfe25aaa1"
integrity sha512-anP1Z8qwhkbmu7MFP5iTt+wQKXgwzf7zTyGlcdzabySa9vd0Xt392U0rVmz9poOaBj0uHJKyyo9/upk0HrEQew==

picocolors@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.1.0.tgz#5358b76a78cde483ba5cef6a9dc9671440b27d59"
integrity sha512-TQ92mBOW0l3LeMeyLV6mzy/kWr8lkd/hp3mTg7wYK7zJhuBStmGMBG0BdeDZS/dZx1IukaX6Bk11zcln25o1Aw==

picomatch@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
Expand Down

0 comments on commit cba291e

Please sign in to comment.