Skip to content

Commit

Permalink
perf: replace chalk with colorette and supports-color
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Oct 2, 2021
1 parent 5240c26 commit 4de4cda
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
6 changes: 3 additions & 3 deletions bin/lint-staged.js
Expand Up @@ -5,9 +5,9 @@
const fs = require('fs')

// Force colors for packages that depend on https://www.npmjs.com/package/supports-color
const { supportsColor } = require('chalk')
if (supportsColor && supportsColor.level) {
process.env.FORCE_COLOR = supportsColor.level.toString()
const supportsColor = require('supports-color')
if (supportsColor.stdout) {
process.env.FORCE_COLOR = supportsColor.stdout.level.toString()
}

// Do not terminate main Listr process on SIGINT
Expand Down
8 changes: 4 additions & 4 deletions lib/figures.js
@@ -1,10 +1,10 @@
const chalk = require('chalk')
const { blue, redBright, yellow } = require('colorette')
const { figures } = require('listr2')

const { arrowRight, cross, warning } = figures

module.exports = {
info: chalk.blue(arrowRight),
error: chalk.red(cross),
warning: chalk.yellow(warning),
info: blue(arrowRight),
error: redBright(cross),
warning: yellow(warning),
}
33 changes: 17 additions & 16 deletions lib/messages.js
@@ -1,58 +1,59 @@
'use strict'

const chalk = require('chalk')
const { redBright, bold, yellow } = require('colorette')
const format = require('stringify-object')

const { error, info, warning } = require('./figures')

const configurationError = (opt, helpMsg, value) =>
`${chalk.redBright(`${error} Validation Error:`)}
`${redBright(`${error} Validation Error:`)}
Invalid value for '${chalk.bold(opt)}': ${chalk.bold(
Invalid value for '${bold(opt)}': ${bold(
format(value, { inlineCharacterLimit: Number.POSITIVE_INFINITY })
)}
${helpMsg}`

const NOT_GIT_REPO = chalk.redBright(`${error} Current directory is not a git directory!`)
const NOT_GIT_REPO = redBright(`${error} Current directory is not a git directory!`)

const FAILED_GET_STAGED_FILES = chalk.redBright(`${error} Failed to get staged files!`)
const FAILED_GET_STAGED_FILES = redBright(`${error} Failed to get staged files!`)

const incorrectBraces = (before, after) => `${warning} ${chalk.yellow(
`Detected incorrect braces with only single value: \`${before}\`. Reformatted as: \`${after}\``
)}
const incorrectBraces = (before, after) =>
yellow(
`${warning} Detected incorrect braces with only single value: \`${before}\`. Reformatted as: \`${after}\`
`
)

const NO_STAGED_FILES = `${info} No staged files found.`

const NO_TASKS = `${info} No staged files match any configured task.`

const skippingBackup = (hasInitialCommit) => {
const reason = hasInitialCommit ? '`--no-stash` was used' : 'there’s no initial commit yet'
return `${warning} ${chalk.yellow(`Skipping backup because ${reason}.\n`)}`
return yellow(`${warning} Skipping backup because ${reason}.\n`)
}

const DEPRECATED_GIT_ADD = `${warning} ${chalk.yellow(
`Some of your tasks use \`git add\` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.`
)}
const DEPRECATED_GIT_ADD = yellow(
`${warning} Some of your tasks use \`git add\` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.
`
)

const TASK_ERROR = 'Skipped because of errors from tasks.'

const SKIPPED_GIT_ERROR = 'Skipped because of previous git error.'

const GIT_ERROR = `\n ${error} ${chalk.red(`lint-staged failed due to a git error.`)}`
const GIT_ERROR = `\n ${redBright(`${error} lint-staged failed due to a git error.`)}`

const invalidOption = (name, value, message) => `${chalk.redBright(`${error} Validation Error:`)}
const invalidOption = (name, value, message) => `${redBright(`${error} Validation Error:`)}
Invalid value for option '${chalk.bold(name)}': ${chalk.bold(value)}
Invalid value for option '${bold(name)}': ${bold(value)}
${message}
See https://github.com/okonet/lint-staged#command-line-flags`

const PREVENTED_EMPTY_COMMIT = `
${warning} ${chalk.yellow(`lint-staged prevented an empty git commit.
${yellow(`${warning} lint-staged prevented an empty git commit.
Use the --allow-empty option to continue, or check your task configuration`)}
`

Expand Down
2 changes: 1 addition & 1 deletion lib/resolveTaskFn.js
@@ -1,6 +1,6 @@
'use strict'

const { redBright, dim } = require('chalk')
const { redBright, dim } = require('colorette')
const execa = require('execa')
const debug = require('debug')('lint-staged:task')
const { parseArgsStringToArgv } = require('string-argv')
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -27,8 +27,8 @@
"test:watch": "jest --watch"
},
"dependencies": {
"chalk": "^4.1.2",
"cli-truncate": "2.1.0",
"colorette": "^1.4.0",
"commander": "^8.2.0",
"cosmiconfig": "^7.0.1",
"debug": "^4.3.2",
Expand All @@ -39,7 +39,8 @@
"normalize-path": "^3.0.0",
"please-upgrade-node": "^3.2.0",
"string-argv": "0.3.1",
"stringify-object": "3.3.0"
"stringify-object": "3.3.0",
"supports-color": "8.1.1"
},
"devDependencies": {
"@babel/core": "^7.15.5",
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Expand Up @@ -1569,7 +1569,7 @@ chalk@^2.0.0:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@^4.0.0, chalk@^4.1.2:
chalk@^4.0.0:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==
Expand Down Expand Up @@ -4073,6 +4073,13 @@ strip-json-comments@^3.1.0, strip-json-comments@^3.1.1:
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==

supports-color@8.1.1, supports-color@^8.0.0:
version "8.1.1"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-8.1.1.tgz#cd6fc17e28500cff56c1b86c0a7fd4a54a73005c"
integrity sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==
dependencies:
has-flag "^4.0.0"

supports-color@^5.3.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"
Expand All @@ -4087,13 +4094,6 @@ supports-color@^7.0.0, supports-color@^7.1.0:
dependencies:
has-flag "^4.0.0"

supports-color@^8.0.0:
version "8.1.1"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-8.1.1.tgz#cd6fc17e28500cff56c1b86c0a7fd4a54a73005c"
integrity sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==
dependencies:
has-flag "^4.0.0"

supports-hyperlinks@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/supports-hyperlinks/-/supports-hyperlinks-2.2.0.tgz#4f77b42488765891774b70c79babd87f9bd594bb"
Expand Down

0 comments on commit 4de4cda

Please sign in to comment.