Skip to content

Commit

Permalink
feat: use chalk instead of colors (#433)
Browse files Browse the repository at this point in the history
* chore: remove colors dependency

* feat: add chalk dependency

* feat: replace colors with chalk

* fix: fix missing chalk usage

* fix: re-enable color force
  • Loading branch information
Shinigami authored May 31, 2020
1 parent c4a7de4 commit 372dd60
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 33 deletions.
96 changes: 82 additions & 14 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
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
"dependencies": {
"async": "3.2.0",
"colors": "1.4.0",
"chalk": "4.0.0",
"commander": "5.1.0",
"glob": "7.1.6",
"parse-glob": "3.0.4",
Expand Down
3 changes: 2 additions & 1 deletion src/cli/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as chalk from 'chalk'
import { EventEmitter } from 'events'
import { sync as globSync } from 'glob'
import { parse, resolve } from 'path'
Expand Down Expand Up @@ -94,7 +95,7 @@ formatter.setFormat = function (format) {

if (formatHandel === undefined) {
console.log(
'No supported formatter, supported formatters: %s'.red,
chalk.red('No supported formatter, supported formatters: %s'),
arrSupportedFormatters.join(', ')
)
process.exit(1)
Expand Down
12 changes: 7 additions & 5 deletions src/cli/formatters/compact.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from 'colors'
import * as chalk from 'chalk'
import { FormatterCallback } from '../formatter'

const compactFormatter: FormatterCallback = function (
Expand All @@ -8,9 +8,8 @@ const compactFormatter: FormatterCallback = function (
) {
const nocolor = options.nocolor

if (nocolor !== false) {
colors.enable()
}
const chalkInstance =
nocolor !== false ? new chalk.Instance({ level: 1 }) : chalk

formatter.on('file', (event) => {
event.messages.forEach((message) => {
Expand All @@ -31,7 +30,10 @@ const compactFormatter: FormatterCallback = function (
if (allHintCount > 0) {
console.log('')
const message = '%d problems'
console.log(nocolor ? message : message.red, event.allHintCount)
console.log(
nocolor ? message : chalkInstance.red(message),
event.allHintCount
)
}
})
}
Expand Down
12 changes: 8 additions & 4 deletions src/cli/formatters/default.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as chalk from 'chalk'
import { FormatterCallback } from '../formatter'

const defaultFormatter: FormatterCallback = function (
Expand All @@ -14,12 +15,15 @@ const defaultFormatter: FormatterCallback = function (
formatter.on('config', (event) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const configPath = event.configPath!
console.log(' Config loaded: %s', nocolor ? configPath : configPath.cyan)
console.log(
' Config loaded: %s',
nocolor ? configPath : chalk.cyan(configPath)
)
console.log('')
})

formatter.on('file', (event) => {
console.log(` ${event.file.white}`)
console.log(` ${chalk.white(event.file)}`)

const arrLogs = HTMLHint.format(event.messages, {
colors: !nocolor,
Expand All @@ -43,15 +47,15 @@ const defaultFormatter: FormatterCallback = function (
if (allHintCount > 0) {
message = 'Scanned %d files, found %d errors in %d files (%d ms)'
console.log(
nocolor ? message : message.red,
nocolor ? message : chalk.red(message),
allFileCount,
allHintCount,
allHintFileCount,
time
)
} else {
message = 'Scanned %d files, no errors found (%d ms).'
console.log(nocolor ? message : message.green, allFileCount, time)
console.log(nocolor ? message : chalk.green(message), allFileCount, time)
}
})
}
Expand Down
12 changes: 7 additions & 5 deletions src/cli/formatters/unix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from 'colors'
import * as chalk from 'chalk'
import { FormatterCallback } from '../formatter'

const unixFormatter: FormatterCallback = function (
Expand All @@ -8,9 +8,8 @@ const unixFormatter: FormatterCallback = function (
) {
const nocolor = options.nocolor

if (nocolor !== false) {
colors.enable()
}
const chalkInstance =
nocolor !== false ? new chalk.Instance({ level: 1 }) : chalk

formatter.on('file', (event) => {
event.messages.forEach((message) => {
Expand All @@ -30,7 +29,10 @@ const unixFormatter: FormatterCallback = function (
if (allHintCount > 0) {
console.log('')
const message = '%d problems'
console.log(nocolor ? message : message.red, event.allHintCount)
console.log(
nocolor ? message : chalkInstance.red(message),
event.allHintCount
)
}
})
}
Expand Down
5 changes: 2 additions & 3 deletions src/cli/htmlhint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import { queue as asyncQueue, series as asyncSeries } from 'async'
import 'colors'
import * as chalk from 'chalk'
import * as program from 'commander'
import { existsSync, readFileSync, statSync } from 'fs'
import * as glob from 'glob'
Expand Down Expand Up @@ -112,8 +112,7 @@ function listRules() {

for (const id in rules) {
rule = rules[id]
// eslint-disable-next-line @typescript-eslint/unbound-method
console.log(' %s : %s', rule.id.bold, rule.description)
console.log(' %s : %s', chalk.bold(rule.id), rule.description)
}
}

Expand Down

0 comments on commit 372dd60

Please sign in to comment.