From dc0f49f1f327c627f5def364d211625a385cad29 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:33:36 +0000 Subject: [PATCH 1/2] refactor: replace `chalk` with `picocolors` --- lib/colors.ts | 6 ++++++ lib/help.ts | 48 ++++++++++++++++++++++-------------------------- lib/refiner.ts | 4 ++-- lib/walker.ts | 8 +++++--- package.json | 2 +- test/test.js | 6 +++--- test/utils.js | 4 ++++ yarn.lock | 5 +++++ 8 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 lib/colors.ts diff --git a/lib/colors.ts b/lib/colors.ts new file mode 100644 index 000000000..0c3f2ed3a --- /dev/null +++ b/lib/colors.ts @@ -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()); diff --git a/lib/help.ts b/lib/help.ts index 385dec2c3..e94105ba7 100644 --- a/lib/help.ts +++ b/lib/help.ts @@ -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] + ${pc.bold('pkg')} [options] - ${chalk.dim('Options:')} + ${pc.dim('Options:')} -h, --help output usage information -v, --version output pkg version @@ -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')} `); } diff --git a/lib/refiner.ts b/lib/refiner.ts index b68b68196..4c54aa3a8 100644 --- a/lib/refiner.ts +++ b/lib/refiner.ts @@ -1,5 +1,5 @@ import path from 'path'; -import chalk from 'chalk'; +import { pc } from './colors'; import { STORE_LINKS, retrieveDenominator, @@ -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; } } diff --git a/lib/walker.ts b/lib/walker.ts index c700cca3a..a4a703004 100644 --- a/lib/walker.ts +++ b/lib/walker.ts @@ -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'; @@ -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'; @@ -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( @@ -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; diff --git a/package.json b/package.json index cbca3670b..021bcdc3e 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/test.js b/test/test.js index 1384a1c09..50ce90aa7 100644 --- a/test/test.js +++ b/test/test.js @@ -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'; @@ -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'); diff --git a/test/utils.js b/test/utils.js index ec586fe11..f6ddcae1b 100644 --- a/test/utils.js +++ b/test/utils.js @@ -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); diff --git a/yarn.lock b/yarn.lock index 8aca3a286..55d72ef61 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" From 70a9d0bf4231ba8ad947b5064e34cd556d2c70a9 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:44:26 +0000 Subject: [PATCH 2/2] chore: fix tests resolving to esm-only chalk --- test/test-79-npm/require_optional/package.json | 2 +- test/test-80-compression/test-x.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test-79-npm/require_optional/package.json b/test/test-79-npm/require_optional/package.json index f5231caa4..37dd1960a 100644 --- a/test/test-79-npm/require_optional/package.json +++ b/test/test-79-npm/require_optional/package.json @@ -1,6 +1,6 @@ { "private": true, "peerOptionalDependencies": { - "chalk": "*" + "chalk": "^4.1.2" } } diff --git a/test/test-80-compression/test-x.js b/test/test-80-compression/test-x.js index 1d4708028..c302a2175 100644 --- a/test/test-80-compression/test-x.js +++ b/test/test-80-compression/test-x.js @@ -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.';