From 85bdd82dfe4cd3d514d7820dad3338b485084fbf Mon Sep 17 00:00:00 2001 From: Cristopher <32661241+Namchee@users.noreply.github.com> Date: Mon, 16 Dec 2024 21:22:37 +0700 Subject: [PATCH] feat: use `picocolors` instead of `chalk` (#2377) Co-authored-by: Artem Zakharchenko --- .github/workflows/ci.yml | 8 -------- cli/init.js | 18 +++++++++--------- cli/invariant.js | 4 ++-- package.json | 2 +- pnpm-lock.yaml | 15 +++++---------- test/e2e/cli-init.node.test.ts | 13 +++++++++++-- tsup.config.ts | 2 +- 7 files changed, 29 insertions(+), 33 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 911563621..4e8e8ee79 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -198,14 +198,6 @@ jobs: node-version: 18 cache: 'pnpm' - - name: Restore build cache - uses: actions/cache@v4 - with: - path: ./lib - key: ${{ runner.os }}-node-20-build-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-node-20-build- - - name: Install dependencies run: pnpm install diff --git a/cli/init.js b/cli/init.js index e10f81d3f..4520da238 100755 --- a/cli/init.js +++ b/cli/init.js @@ -1,6 +1,6 @@ const fs = require('fs') const path = require('path') -const chalk = require('chalk') +const colors = require('picocolors') const confirm = require('@inquirer/confirm').default const invariant = require('./invariant') const { SERVICE_WORKER_BUILD_PATH } = require('../config/constants') @@ -34,7 +34,7 @@ module.exports = async function init(args) { // will equal to false. else if (args.save == null) { console.log(`\ - ${chalk.cyan( + ${colors.cyan( 'INFO', )} In order to ease the future updates to the worker script, we recommend saving the path to the worker directory in your package.json.`) @@ -141,20 +141,20 @@ async function copyWorkerScript(destination, cwd) { */ function printSuccessMessage(paths) { console.log(` -${chalk.green('Worker script successfully copied!')} -${paths.map((path) => chalk.gray(` - ${path}\n`))} +${colors.green('Worker script successfully copied!')} +${paths.map((path) => colors.gray(` - ${path}\n`))} Continue by describing the network in your application: -${chalk.cyan.bold('https://mswjs.io/docs/getting-started')} +${colors.red(colors.bold('https://mswjs.io/docs/getting-started'))} `) } function printFailureMessage(pathsWithErrors) { console.error(`\ -${chalk.red('Copying the worker script failed at following paths:')} +${colors.red('Copying the worker script failed at following paths:')} ${pathsWithErrors - .map(([path, error]) => chalk.gray(` - ${path}`) + '\n' + ` ${error}`) + .map(([path, error]) => colors.gray(` - ${path}`) + '\n' + ` ${error}`) .join('\n\n')} `) } @@ -167,7 +167,7 @@ function saveWorkerDirectory(packageJsonPath, publicDir) { const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')) console.log( - chalk.gray('Updating "msw.workerDirectory" at "%s"...'), + colors.gray('Updating "msw.workerDirectory" at "%s"...'), packageJsonPath, ) @@ -200,7 +200,7 @@ function saveWorkerDirectory(packageJsonPath, publicDir) { function promptWorkerDirectoryUpdate(message, packageJsonPath, publicDir) { return confirm({ theme: { - prefix: chalk.yellowBright('?'), + prefix: colors.yellowBright('?'), }, message, }).then((answer) => { diff --git a/cli/invariant.js b/cli/invariant.js index 8661dbe87..12ffdc196 100644 --- a/cli/invariant.js +++ b/cli/invariant.js @@ -1,8 +1,8 @@ -const chalk = require('chalk') +const colors = require('picocolors') module.exports = function invariant(predicate, message, ...args) { if (!predicate) { - console.error(chalk.red(message), ...args) + console.error(colors.red(message), ...args) process.exit(1) } } diff --git a/package.json b/package.json index cc85ef407..8df028bd6 100644 --- a/package.json +++ b/package.json @@ -149,12 +149,12 @@ "@open-draft/until": "^2.1.0", "@types/cookie": "^0.6.0", "@types/statuses": "^2.0.4", - "chalk": "^4.1.2", "graphql": "^16.8.1", "headers-polyfill": "^4.0.2", "is-node-process": "^1.2.0", "outvariant": "^1.4.3", "path-to-regexp": "^6.3.0", + "picocolors": "^1.1.1", "strict-event-emitter": "^0.5.1", "type-fest": "^4.26.1", "yargs": "^17.7.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 283b24c93..10fb7b3a5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,9 +35,6 @@ importers: '@types/statuses': specifier: ^2.0.4 version: 2.0.5 - chalk: - specifier: ^4.1.2 - version: 4.1.2 graphql: specifier: ^16.8.1 version: 16.8.2 @@ -53,6 +50,9 @@ importers: path-to-regexp: specifier: ^6.3.0 version: 6.3.0 + picocolors: + specifier: ^1.1.1 + version: 1.1.1 strict-event-emitter: specifier: ^0.5.1 version: 0.5.1 @@ -3915,9 +3915,6 @@ packages: resolution: {integrity: sha512-vE7JKRyES09KiunauX7nd2Q9/L7lhok4smP9RZTDeD4MVs72Dp2qNFVz39Nz5a0FVEW0BJR6C0DYrq6unoziZA==} engines: {node: '>= 14.16'} - picocolors@1.0.0: - resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==} - picocolors@1.1.1: resolution: {integrity: sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==} @@ -5159,7 +5156,7 @@ snapshots: '@babel/code-frame@7.24.2': dependencies: '@babel/highlight': 7.24.2 - picocolors: 1.0.0 + picocolors: 1.1.1 '@babel/compat-data@7.24.1': {} @@ -5249,7 +5246,7 @@ snapshots: '@babel/helper-validator-identifier': 7.22.20 chalk: 2.4.2 js-tokens: 4.0.0 - picocolors: 1.0.0 + picocolors: 1.1.1 '@babel/parser@7.24.1': dependencies: @@ -9149,8 +9146,6 @@ snapshots: pathval@2.0.0: {} - picocolors@1.0.0: {} - picocolors@1.1.1: {} picomatch@2.3.1: {} diff --git a/test/e2e/cli-init.node.test.ts b/test/e2e/cli-init.node.test.ts index b19b9d50c..e77af57d7 100644 --- a/test/e2e/cli-init.node.test.ts +++ b/test/e2e/cli-init.node.test.ts @@ -37,8 +37,17 @@ afterAll(async () => { await fsMock.cleanup() }) -async function init(inlineArgs: Array) { - return fsMock.exec(`node ${cliPath} init ${inlineArgs.join(' ')}`) +async function init(inlineArgs: Array): ReturnType { + const result = await fsMock.exec( + `node ${cliPath} init ${inlineArgs.join(' ')}`, + ) + + return { + ...result, + // Strip stdout from color unicode characters: + stdout: result.stdout.replace(/\x1b\[\d+m/gi, ''), + stderr: result.stderr.replace(/\x1b\[\d+m/gi, ''), + } } test('copies the script to a given path without saving', async () => { diff --git a/tsup.config.ts b/tsup.config.ts index 8c606d11e..6bc3c796b 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -95,7 +95,7 @@ const reactNativeConfig: Options = { name: 'react-native', platform: 'node', entry: ['./src/native/index.ts'], - external: ['chalk', 'util', 'events', mswCore, ecosystemDependencies], + external: ['picocolors', 'util', 'events', mswCore, ecosystemDependencies], format: ['esm', 'cjs'], outDir: './lib/native', bundle: true,