From ff71640e8af8e96023c930c440a9a3e57bd97ab9 Mon Sep 17 00:00:00 2001 From: Daniel Ruf Date: Mon, 29 Apr 2019 08:44:29 +0200 Subject: [PATCH] feat: replace chalk with kleur (#1289) @DanielRuf * chore: replace chalk with kleur * build: add updated lockfile * chore: add flow-typed stub for kleur 3.0.3 * chore: remove unnecessary whitespace * test: update snapshot for update-banner.js * chore: migrate logger.js from chalk to kleur * chore: use import instead of require --- flow-typed/npm/chalk_v2.x.x.js | 98 ------------------ flow-typed/npm/kleur_vx.x.x.js | 33 ++++++ package.json | 2 +- src/lib/cli.js | 6 +- src/lib/logger.js | 30 +++--- src/lib/update-banner.js | 22 ++-- test/e2e/puppeteer_environment.js | 6 +- test/e2e/setup.js | 4 +- test/e2e/teardown.js | 4 +- test/functional/lib/environment.js | 10 +- test/functional/teardown.js | 4 +- .../__snapshots__/update-banner.spec.js.snap | 56 +++++----- yarn.lock | Bin 355303 -> 355299 bytes 13 files changed, 105 insertions(+), 170 deletions(-) delete mode 100644 flow-typed/npm/chalk_v2.x.x.js create mode 100644 flow-typed/npm/kleur_vx.x.x.js diff --git a/flow-typed/npm/chalk_v2.x.x.js b/flow-typed/npm/chalk_v2.x.x.js deleted file mode 100644 index 43548785f..000000000 --- a/flow-typed/npm/chalk_v2.x.x.js +++ /dev/null @@ -1,98 +0,0 @@ -// flow-typed signature: db5b2cdde8db39d47e27cc8ab84f89bf -// flow-typed version: d662d43161/chalk_v2.x.x/flow_>=v0.25.x - -// From: https://github.com/chalk/chalk/blob/master/index.js.flow - -declare module "chalk" { - declare type TemplateStringsArray = $ReadOnlyArray; - - declare type Level = $Values<{ - None: 0, - Basic: 1, - Ansi256: 2, - TrueColor: 3 - }>; - - declare type ChalkOptions = {| - enabled?: boolean, - level?: Level - |}; - - declare type ColorSupport = {| - level: Level, - hasBasic: boolean, - has256: boolean, - has16m: boolean - |}; - - declare interface Chalk { - (...text: string[]): string, - (text: TemplateStringsArray, ...placeholders: string[]): string, - constructor(options?: ChalkOptions): Chalk, - enabled: boolean, - level: Level, - rgb(r: number, g: number, b: number): Chalk, - hsl(h: number, s: number, l: number): Chalk, - hsv(h: number, s: number, v: number): Chalk, - hwb(h: number, w: number, b: number): Chalk, - bgHex(color: string): Chalk, - bgKeyword(color: string): Chalk, - bgRgb(r: number, g: number, b: number): Chalk, - bgHsl(h: number, s: number, l: number): Chalk, - bgHsv(h: number, s: number, v: number): Chalk, - bgHwb(h: number, w: number, b: number): Chalk, - hex(color: string): Chalk, - keyword(color: string): Chalk, - - +reset: Chalk, - +bold: Chalk, - +dim: Chalk, - +italic: Chalk, - +underline: Chalk, - +inverse: Chalk, - +hidden: Chalk, - +strikethrough: Chalk, - - +visible: Chalk, - - +black: Chalk, - +red: Chalk, - +green: Chalk, - +yellow: Chalk, - +blue: Chalk, - +magenta: Chalk, - +cyan: Chalk, - +white: Chalk, - +gray: Chalk, - +grey: Chalk, - +blackBright: Chalk, - +redBright: Chalk, - +greenBright: Chalk, - +yellowBright: Chalk, - +blueBright: Chalk, - +magentaBright: Chalk, - +cyanBright: Chalk, - +whiteBright: Chalk, - - +bgBlack: Chalk, - +bgRed: Chalk, - +bgGreen: Chalk, - +bgYellow: Chalk, - +bgBlue: Chalk, - +bgMagenta: Chalk, - +bgCyan: Chalk, - +bgWhite: Chalk, - +bgBlackBright: Chalk, - +bgRedBright: Chalk, - +bgGreenBright: Chalk, - +bgYellowBright: Chalk, - +bgBlueBright: Chalk, - +bgMagentaBright: Chalk, - +bgCyanBright: Chalk, - +bgWhiteBrigh: Chalk, - - supportsColor: ColorSupport - } - - declare module.exports: Chalk; -} diff --git a/flow-typed/npm/kleur_vx.x.x.js b/flow-typed/npm/kleur_vx.x.x.js new file mode 100644 index 000000000..652520314 --- /dev/null +++ b/flow-typed/npm/kleur_vx.x.x.js @@ -0,0 +1,33 @@ +// flow-typed signature: 27250091946c7286334b6d8e332b126f +// flow-typed version: <>/kleur_v3.0.3/flow_v0.81.0 + +/** + * This is an autogenerated libdef stub for: + * + * 'kleur' + * + * Fill this stub out by replacing all the `any` types. + * + * Once filled out, we encourage you to share your work with the + * community by sending a pull request to: + * https://github.com/flowtype/flow-typed + */ + +declare module 'kleur' { + declare module.exports: any; +} + +/** + * We include stubs for each file inside this npm package in case you need to + * require those files directly. Feel free to delete any files that aren't + * needed. + */ + + +// Filename aliases +declare module 'kleur/index' { + declare module.exports: $Exports<'kleur'>; +} +declare module 'kleur/index.js' { + declare module.exports: $Exports<'kleur'>; +} diff --git a/package.json b/package.json index 4720e51d6..bc4c4ecb2 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "async": "3.0.1-0", "body-parser": "1.19.0", "bunyan": "1.8.12", - "chalk": "2.4.2", "commander": "2.20.0", "compression": "1.7.4", "cookies": "0.7.3", @@ -36,6 +35,7 @@ "js-string-escape": "1.0.1", "js-yaml": "3.13.1", "jsonwebtoken": "8.5.1", + "kleur": "3.0.3", "lockfile": "1.0.4", "lodash": "4.17.11", "lunr-mutable-indexes": "2.3.2", diff --git a/src/lib/cli.js b/src/lib/cli.js index 328cdebf1..879e14b21 100644 --- a/src/lib/cli.js +++ b/src/lib/cli.js @@ -8,20 +8,20 @@ import path from 'path'; import semver from 'semver'; -import chalk from 'chalk'; +import { bgYellow, bgRed } from 'kleur'; import {startVerdaccio, listenDefaultCallback} from './bootstrap'; import findConfigFile from './config-path'; import {verdaccioUpdateBanner} from './update-banner'; import { parseConfigFile } from './utils'; if (process.getuid && process.getuid() === 0) { - global.console.warn(chalk.bgYellow('Verdaccio doesn\'t need superuser privileges. Don\'t run it under root.')); + global.console.warn(bgYellow('Verdaccio doesn\'t need superuser privileges. Don\'t run it under root.')); } const MIN_NODE_VERSION = '6.9.0'; if (semver.satisfies(process.version, `>=${MIN_NODE_VERSION}`) === false) { - global.console.error(chalk.bgRed(`Verdaccio requires at least Node.js ${MIN_NODE_VERSION} or higher, please upgrade your Node.js distribution`)); + global.console.error(bgRed(`Verdaccio requires at least Node.js ${MIN_NODE_VERSION} or higher, please upgrade your Node.js distribution`)); process.exit(1); } diff --git a/src/lib/logger.js b/src/lib/logger.js index 3ad2276fb..8eca035be 100644 --- a/src/lib/logger.js +++ b/src/lib/logger.js @@ -7,7 +7,7 @@ const cluster = require('cluster'); const Logger = require('bunyan'); const Error = require('http-errors'); const Stream = require('stream'); -const chalk = require('chalk'); +const { red, yellow, cyan, magenta, green, white, black, blue } = require('kleur'); const pkgJSON = require('../../package.json'); const _ = require('lodash'); const dayjs = require('dayjs'); @@ -155,13 +155,13 @@ function setup(logs) { // level to color const levels = { - fatal: chalk.red, - error: chalk.red, - warn: chalk.yellow, - http: chalk.magenta, - info: chalk.cyan, - debug: chalk.green, - trace: chalk.white, + fatal: red, + error: red, + warn: yellow, + http: magenta, + info: cyan, + debug: green, + trace: white, }; let max = 0; @@ -206,9 +206,9 @@ function fillInMsgTemplate(msg, obj, colors) { if (!colors || str.includes('\n')) { return str; } else if (is_error) { - return chalk.red(str); + return red(str); } else { - return chalk.green(str); + return green(str); } } else { return require('util').inspect(str, null, null, colors); @@ -232,10 +232,10 @@ function print(type, msg, obj, colors) { const subsystems = [ { - in: chalk.green('<--'), - out: chalk.yellow('-->'), - fs: chalk.black('-=-'), - default: chalk.blue('---'), + in: green('<--'), + out: yellow('-->'), + fs: black('-=-'), + default: blue('---'), }, { in: '<--', @@ -247,7 +247,7 @@ function print(type, msg, obj, colors) { const sub = subsystems[colors ? 0 : 1][obj.sub] || subsystems[+!colors].default; if (colors) { - return ` ${levels[type](pad(type))}${chalk.white(`${sub} ${finalMessage}`)}`; + return ` ${levels[type](pad(type))}${white(`${sub} ${finalMessage}`)}`; } else { return ` ${pad(type)}${sub} ${finalMessage}`; } diff --git a/src/lib/update-banner.js b/src/lib/update-banner.js index 754b2badb..17dc1c671 100644 --- a/src/lib/update-banner.js +++ b/src/lib/update-banner.js @@ -5,7 +5,7 @@ import request from 'request'; import semver from 'semver'; -import chalk from 'chalk'; +import { red, green, blue, white, bold } from 'kleur'; import _ from 'lodash'; import { UPDATE_BANNER, DEFAULT_REGISTRY, HTTP_STATUS } from './constants'; @@ -13,16 +13,16 @@ import { UPDATE_BANNER, DEFAULT_REGISTRY, HTTP_STATUS } from './constants'; const VERDACCIO_LATEST_REGISTRY_URL = `${DEFAULT_REGISTRY}/verdaccio/latest`; /** - * Creates NPM update banner using chalk + * Creates NPM update banner using kleur */ export function createBanner(currentVersion: string, newVersion: string, releaseType: string): string { const changelog = `${UPDATE_BANNER.CHANGELOG_URL}v${newVersion}`; - const versionUpdate = `${chalk.bold.red(currentVersion)} → ${chalk.bold.green(newVersion)}`; - const banner = chalk` - {white.bold A new ${_.upperCase(releaseType)} version of Verdaccio is available. ${versionUpdate} } - {white.bold Run ${chalk.green.bold('npm install -g verdaccio')} to update}. - {white.bold Registry: ${DEFAULT_REGISTRY}} - {blue.bold Changelog: ${changelog}} + const versionUpdate = `${bold().red(currentVersion)} → ${bold().green(newVersion)}`; + const banner = ` + ${white().bold('A new ' + _.upperCase(releaseType) + ' version of Verdaccio is available. ' + versionUpdate)} + ${white().bold('Run ' + green('npm install -g verdaccio') + ' to update.')} + ${white().bold('Registry: ' + DEFAULT_REGISTRY)} + ${blue().bold('Changelog: ' + changelog)} `; return banner; } @@ -31,9 +31,9 @@ export function createBanner(currentVersion: string, newVersion: string, release * creates error banner */ export function createErrorBanner(message: string): string { - const banner = chalk` - {red.bold Unable to check verdaccio version on ${DEFAULT_REGISTRY}} - {red.bold Error: ${message}} + const banner = ` + ${red().bold('Unable to check verdaccio version on ' + DEFAULT_REGISTRY)} + ${red().bold('Error: ' + message)} `; return banner; } diff --git a/test/e2e/puppeteer_environment.js b/test/e2e/puppeteer_environment.js index c07d39802..c74fc1507 100644 --- a/test/e2e/puppeteer_environment.js +++ b/test/e2e/puppeteer_environment.js @@ -1,4 +1,4 @@ -const chalk = require('chalk'); +const { yellow } = require('kleur'); const NodeEnvironment = require('jest-environment-node'); const puppeteer = require('puppeteer'); const fs = require('fs'); @@ -30,7 +30,7 @@ class PuppeteerEnvironment extends NodeEnvironment { this.global.__VERDACCIO_E2E__ = fork[0]; this.global.__VERDACCIO__PROTECTED_E2E__ = fork2[0]; - console.log(chalk.yellow('Setup Test Environment.')); + console.log(yellow('Setup Test Environment.')); await super.setup(); const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8'); if (!wsEndpoint) { @@ -44,7 +44,7 @@ class PuppeteerEnvironment extends NodeEnvironment { } async teardown() { - console.log(chalk.yellow('Teardown Test Environment.')); + console.log(yellow('Teardown Test Environment.')); await super.teardown(); this.global.__VERDACCIO_E2E__.stop(); this.global.__VERDACCIO__PROTECTED_E2E__.stop(); diff --git a/test/e2e/setup.js b/test/e2e/setup.js index 96768f530..5fd4d283e 100644 --- a/test/e2e/setup.js +++ b/test/e2e/setup.js @@ -1,4 +1,4 @@ -const chalk = require('chalk'); +const { green } = require('kleur'); const puppeteer = require('puppeteer'); const fs = require('fs'); const mkdirp = require('mkdirp'); @@ -8,7 +8,7 @@ const path = require('path'); const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); module.exports = async function() { - console.log(chalk.green('Setup Puppeteer')); + console.log(green('Setup Puppeteer')); const browser = await puppeteer.launch({ headless: true, /* slowMo: 300 */ args: ['--no-sandbox'] }); global.__BROWSER__ = browser; mkdirp.sync(DIR); diff --git a/test/e2e/teardown.js b/test/e2e/teardown.js index d02998e76..defef307f 100644 --- a/test/e2e/teardown.js +++ b/test/e2e/teardown.js @@ -1,4 +1,4 @@ -const chalk = require('chalk'); +const { green } = require('kleur'); const rimraf = require('rimraf'); const os = require('os'); const path = require('path'); @@ -6,7 +6,7 @@ const path = require('path'); const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); module.exports = async function() { - console.log(chalk.green('Teardown Puppeteer')); + console.log(green('Teardown Puppeteer')); await global.__BROWSER__.close(); rimraf.sync(DIR) }; diff --git a/test/functional/lib/environment.js b/test/functional/lib/environment.js index 0f843a39a..d865f5e94 100644 --- a/test/functional/lib/environment.js +++ b/test/functional/lib/environment.js @@ -1,6 +1,6 @@ // @flow -import chalk from 'chalk'; +import { yellow, green, blue, magenta } from 'kleur'; import path from 'path'; import NodeEnvironment from 'jest-environment-node'; import {VerdaccioConfig} from '../../lib/verdaccio-server'; @@ -50,7 +50,7 @@ class FunctionalEnvironment extends NodeEnvironment { storage: '/test-storage3' } ]; - console.log(chalk.green('Setup Verdaccio Servers')); + console.log(green('Setup Verdaccio Servers')); const app = await this.startWeb(); this.global.__WEB_SERVER__ = app; @@ -60,13 +60,13 @@ class FunctionalEnvironment extends NodeEnvironment { path.join(pathStore, config.storage), path.join(pathStore, config.config), `http://${DOMAIN_SERVERS}:${config.port}/`, config.port); - console.log(chalk.magentaBright(`Running registry ${config.config} on port ${config.port}`)); + console.log(magenta(`Running registry ${config.config} on port ${config.port}`)); const server: IServerBridge = new Server(verdaccioConfig.domainPath); serverList.push(server); const process = new VerdaccioProcess(verdaccioConfig, server, SILENCE_LOG, DEBUG_INJECT); const fork = await process.init(); - console.log(chalk.blue(`Fork PID ${fork[1]}`)); + console.log(blue(`Fork PID ${fork[1]}`)); forkList.push(fork); } @@ -76,7 +76,7 @@ class FunctionalEnvironment extends NodeEnvironment { async teardown() { await super.teardown(); - console.log(chalk.yellow('Teardown Test Environment.')); + console.log(yellow('Teardown Test Environment.')); if (!this.global.__SERVERS_PROCESS__) { throw new Error("There are no servers to stop"); } diff --git a/test/functional/teardown.js b/test/functional/teardown.js index 52a2860af..e5392aeea 100644 --- a/test/functional/teardown.js +++ b/test/functional/teardown.js @@ -1,4 +1,4 @@ module.exports = async function() { - // console.log(chalk.green('Teardown Verdaccio Functional')); - // console.log(chalk.blue('Teardown:: all server were closed')); + // console.log(green('Teardown Verdaccio Functional')); + // console.log(blue('Teardown:: all server were closed')); }; diff --git a/test/unit/api/__snapshots__/update-banner.spec.js.snap b/test/unit/api/__snapshots__/update-banner.spec.js.snap index d7a4f6aef..a4abb86b2 100644 --- a/test/unit/api/__snapshots__/update-banner.spec.js.snap +++ b/test/unit/api/__snapshots__/update-banner.spec.js.snap @@ -5,10 +5,10 @@ exports[`Verdaccio update banner should print major update banner 1`] = ` "calls": Array [ Array [ " - A new MAJOR version of Verdaccio is available. 3.0.0 → 4.5.6  - Run npm install -g verdaccio to update. - Registry: https://registry.npmjs.org - Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v4.5.6 + A new MAJOR version of Verdaccio is available. 3.0.0 → 4.5.6 + Run npm install -g verdaccio to update. + Registry: https://registry.npmjs.org + Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v4.5.6 ", ], ], @@ -26,10 +26,10 @@ exports[`Verdaccio update banner should print minor update banner 1`] = ` "calls": Array [ Array [ " - A new MINOR version of Verdaccio is available. 4.0.0 → 4.5.6  - Run npm install -g verdaccio to update. - Registry: https://registry.npmjs.org - Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v4.5.6 + A new MINOR version of Verdaccio is available. 4.0.0 → 4.5.6 + Run npm install -g verdaccio to update. + Registry: https://registry.npmjs.org + Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v4.5.6 ", ], ], @@ -47,10 +47,10 @@ exports[`Verdaccio update banner should print patch update banner 1`] = ` "calls": Array [ Array [ " - A new PATCH version of Verdaccio is available. 4.5.0 → 4.5.6  - Run npm install -g verdaccio to update. - Registry: https://registry.npmjs.org - Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v4.5.6 + A new PATCH version of Verdaccio is available. 4.5.0 → 4.5.6 + Run npm install -g verdaccio to update. + Registry: https://registry.npmjs.org + Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v4.5.6 ", ], ], @@ -68,8 +68,8 @@ exports[`Verdaccio update banner when default registry returns with error 1`] = "calls": Array [ Array [ " - Unable to check verdaccio version on https://registry.npmjs.org - Error: {\\"message\\":\\"internal server error\\",\\"statusCode\\":500} + Unable to check verdaccio version on https://registry.npmjs.org + Error: {\\"message\\":\\"internal server error\\",\\"statusCode\\":500} ", ], ], @@ -84,34 +84,34 @@ exports[`Verdaccio update banner when default registry returns with error 1`] = exports[`create banner should create a major update banner 1`] = ` " - A new MAJOR version of Verdaccio is available. 1.0.0 → 2.0.0  - Run npm install -g verdaccio to update. - Registry: https://registry.npmjs.org - Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v2.0.0 + A new MAJOR version of Verdaccio is available. 1.0.0 → 2.0.0 + Run npm install -g verdaccio to update. + Registry: https://registry.npmjs.org + Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v2.0.0 " `; exports[`create banner should create a minor update banner 1`] = ` " - A new MINOR version of Verdaccio is available. 1.0.0 → 1.1.0  - Run npm install -g verdaccio to update. - Registry: https://registry.npmjs.org - Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v1.1.0 + A new MINOR version of Verdaccio is available. 1.0.0 → 1.1.0 + Run npm install -g verdaccio to update. + Registry: https://registry.npmjs.org + Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v1.1.0 " `; exports[`create banner should create a patch update banner 1`] = ` " - A new PATCH version of Verdaccio is available. 1.0.0 → 1.0.1  - Run npm install -g verdaccio to update. - Registry: https://registry.npmjs.org - Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v1.0.1 + A new PATCH version of Verdaccio is available. 1.0.0 → 1.0.1 + Run npm install -g verdaccio to update. + Registry: https://registry.npmjs.org + Changelog: https://github.com/verdaccio/verdaccio/releases/tag/v1.0.1 " `; exports[`createErrorBanner should create an error banner 1`] = ` " - Unable to check verdaccio version on https://registry.npmjs.org - Error: message + Unable to check verdaccio version on https://registry.npmjs.org + Error: message " `; diff --git a/yarn.lock b/yarn.lock index db6d9e1aa459ad7f9b6180c1c0b983002d9caaf5..80548bf3fa55582aebf70e73bf30818aec4f0565 100644 GIT binary patch delta 62 zcmV-E0Kxy~*A?T}6@at>U!b=Wq5)h1w`rgOrvZm=qXD;XqXK5$2Qw}(E;B43m(PI% UJq2!XZEACe6@mk|6@moP=`2PVdH?_b delta 60 zcmV-C0K@;|*A?g26@at>U!VsvE;KGOEFibIp#iD^w_KnBUjc_=qXD;KqXK5$m(_s- SH