Skip to content

Commit

Permalink
fix(dependencies): replace colorette with chalk for better color …
Browse files Browse the repository at this point in the history
…support detection
  • Loading branch information
iiroj committed Mar 8, 2023
1 parent 1bbe37a commit f598725
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 53 deletions.
6 changes: 3 additions & 3 deletions bin/lint-staged.js
Expand Up @@ -4,16 +4,16 @@ import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { isColorSupported } from 'colorette'
import { supportsColor } from 'chalk'
import { Option, program } from 'commander'
import debug from 'debug'

import lintStaged from '../lib/index.js'
import { CONFIG_STDIN_ERROR } from '../lib/messages.js'

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

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

export const info = blue(figures.arrowRight)
export const info = chalk.blue(figures.arrowRight)

export const error = redBright(figures.cross)
export const error = chalk.redBright(figures.cross)

export const warning = yellow(figures.warning)
export const warning = chalk.yellow(figures.warning)
26 changes: 14 additions & 12 deletions lib/messages.js
@@ -1,23 +1,23 @@
import { redBright, bold, yellow } from 'colorette'
import chalk from 'chalk'
import inspect from 'object-inspect'

import { error, info, warning } from './figures.js'

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

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

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

export const incorrectBraces = (before, after) =>
yellow(
chalk.yellow(
`${warning} Detected incorrect braces with only single value: \`${before}\`. Reformatted as: \`${after}\`
`
)
Expand All @@ -36,10 +36,10 @@ export const skippingBackup = (hasInitialCommit, diff) => {
? '`--no-stash` was used'
: 'there’s no initial commit yet'

return yellow(`${warning} Skipping backup because ${reason}.\n`)
return chalk.yellow(`${warning} Skipping backup because ${reason}.\n`)
}

export const DEPRECATED_GIT_ADD = yellow(
export const DEPRECATED_GIT_ADD = chalk.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.
`
)
Expand All @@ -48,18 +48,20 @@ export const TASK_ERROR = 'Skipped because of errors from tasks.'

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

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

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

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

Expand Down
8 changes: 4 additions & 4 deletions lib/resolveTaskFn.js
@@ -1,4 +1,4 @@
import { redBright, dim } from 'colorette'
import chalk from 'chalk'
import { execa, execaCommand } from 'execa'
import debug from 'debug'
import { parseArgsStringToArgv } from 'string-argv'
Expand Down Expand Up @@ -32,7 +32,7 @@ const handleOutput = (command, result, ctx, isError = false) => {
const hasOutput = !!stderr || !!stdout

if (hasOutput) {
const outputTitle = isError ? redBright(`${error} ${command}:`) : `${info} ${command}:`
const outputTitle = isError ? chalk.redBright(`${error} ${command}:`) : `${info} ${command}:`
const output = []
.concat(ctx.quiet ? [] : ['', outputTitle])
.concat(stderr ? stderr : [])
Expand All @@ -41,7 +41,7 @@ const handleOutput = (command, result, ctx, isError = false) => {
} else if (isError) {
// Show generic error when task had no output
const tag = getTag(result)
const message = redBright(`\n${error} ${command} failed without output (${tag}).`)
const message = chalk.redBright(`\n${error} ${command} failed without output (${tag}).`)
if (!ctx.quiet) ctx.output.push(message)
}
}
Expand Down Expand Up @@ -116,7 +116,7 @@ const makeErr = (command, result, ctx) => {

handleOutput(command, result, ctx, true)
const tag = getTag(result)
return new Error(`${redBright(command)} ${dim(`[${tag}]`)}`)
return new Error(`${chalk.redBright(command)} ${chalk.dim(`[${tag}]`)}`)
}

/**
Expand Down
12 changes: 6 additions & 6 deletions lib/runAll.js
Expand Up @@ -2,7 +2,7 @@

import path from 'node:path'

import { dim } from 'colorette'
import chalk from 'chalk'
import debug from 'debug'
import { Listr } from 'listr2'
import normalize from 'normalize-path'
Expand Down Expand Up @@ -204,7 +204,7 @@ export const runAll = async (
const fileCount = task.fileList.length

return {
title: `${task.pattern}${dim(
title: `${task.pattern}${chalk.dim(
` — ${fileCount} ${fileCount === 1 ? 'file' : 'files'}`
)}`,
task: async (ctx, task) =>
Expand All @@ -216,7 +216,7 @@ export const runAll = async (
skip: () => {
// Skip task when no files matched
if (fileCount === 0) {
return `${task.pattern}${dim(' — no files')}`
return `${task.pattern}${chalk.dim(' — no files')}`
}
return false
},
Expand All @@ -227,15 +227,15 @@ export const runAll = async (

listrTasks.push({
title:
`${configName}${dim(` — ${files.length} ${files.length > 1 ? 'files' : 'file'}`)}` +
(chunkCount > 1 ? dim(` (chunk ${index + 1}/${chunkCount})...`) : ''),
`${configName}${chalk.dim(` — ${files.length} ${files.length > 1 ? 'files' : 'file'}`)}` +
(chunkCount > 1 ? chalk.dim(` (chunk ${index + 1}/${chunkCount})...`) : ''),
task: (ctx, task) => task.newListr(chunkListrTasks, { concurrent, exitOnError: true }),
skip: () => {
// Skip if the first step (backup) failed
if (ctx.errors.has(GitError)) return SKIPPED_GIT_ERROR
// Skip chunk when no every task is skipped (due to no matches)
if (chunkListrTasks.every((task) => task.skip())) {
return `${configName}${dim(' — no tasks to run')}`
return `${configName}${chalk.dim(' — no tasks to run')}`
}
return false
},
Expand Down
57 changes: 34 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -33,8 +33,8 @@
"test:watch": "jest --watch"
},
"dependencies": {
"chalk": "5.2.0",

This comment has been minimized.

Copy link
@alumni

alumni Mar 17, 2023

chalk@>=5 is esm-only. Maybe @colors/colors would have been a better option?

This comment has been minimized.

Copy link
@iiroj

iiroj Mar 17, 2023

Author Member

Hello, lint-staged is also ESM, so I didn't think this would be an issue. We previously used chalk so going back to it is logical.

"cli-truncate": "^3.1.0",
"colorette": "^2.0.19",
"commander": "^10.0.0",
"debug": "^4.3.4",
"execa": "^7.0.0",
Expand Down

0 comments on commit f598725

Please sign in to comment.