Skip to content

Commit 372dd60

Browse files
author
Shinigami
authoredMay 31, 2020
feat: use chalk instead of colors (#433)
* chore: remove colors dependency * feat: add chalk dependency * feat: replace colors with chalk * fix: fix missing chalk usage * fix: re-enable color force
1 parent c4a7de4 commit 372dd60

File tree

7 files changed

+109
-33
lines changed

7 files changed

+109
-33
lines changed
 

‎package-lock.json

+82-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
},
6060
"dependencies": {
6161
"async": "3.2.0",
62-
"colors": "1.4.0",
62+
"chalk": "4.0.0",
6363
"commander": "5.1.0",
6464
"glob": "7.1.6",
6565
"parse-glob": "3.0.4",

‎src/cli/formatter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as chalk from 'chalk'
12
import { EventEmitter } from 'events'
23
import { sync as globSync } from 'glob'
34
import { parse, resolve } from 'path'
@@ -94,7 +95,7 @@ formatter.setFormat = function (format) {
9495

9596
if (formatHandel === undefined) {
9697
console.log(
97-
'No supported formatter, supported formatters: %s'.red,
98+
chalk.red('No supported formatter, supported formatters: %s'),
9899
arrSupportedFormatters.join(', ')
99100
)
100101
process.exit(1)

‎src/cli/formatters/compact.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as colors from 'colors'
1+
import * as chalk from 'chalk'
22
import { FormatterCallback } from '../formatter'
33

44
const compactFormatter: FormatterCallback = function (
@@ -8,9 +8,8 @@ const compactFormatter: FormatterCallback = function (
88
) {
99
const nocolor = options.nocolor
1010

11-
if (nocolor !== false) {
12-
colors.enable()
13-
}
11+
const chalkInstance =
12+
nocolor !== false ? new chalk.Instance({ level: 1 }) : chalk
1413

1514
formatter.on('file', (event) => {
1615
event.messages.forEach((message) => {
@@ -31,7 +30,10 @@ const compactFormatter: FormatterCallback = function (
3130
if (allHintCount > 0) {
3231
console.log('')
3332
const message = '%d problems'
34-
console.log(nocolor ? message : message.red, event.allHintCount)
33+
console.log(
34+
nocolor ? message : chalkInstance.red(message),
35+
event.allHintCount
36+
)
3537
}
3638
})
3739
}

‎src/cli/formatters/default.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as chalk from 'chalk'
12
import { FormatterCallback } from '../formatter'
23

34
const defaultFormatter: FormatterCallback = function (
@@ -14,12 +15,15 @@ const defaultFormatter: FormatterCallback = function (
1415
formatter.on('config', (event) => {
1516
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1617
const configPath = event.configPath!
17-
console.log(' Config loaded: %s', nocolor ? configPath : configPath.cyan)
18+
console.log(
19+
' Config loaded: %s',
20+
nocolor ? configPath : chalk.cyan(configPath)
21+
)
1822
console.log('')
1923
})
2024

2125
formatter.on('file', (event) => {
22-
console.log(` ${event.file.white}`)
26+
console.log(` ${chalk.white(event.file)}`)
2327

2428
const arrLogs = HTMLHint.format(event.messages, {
2529
colors: !nocolor,
@@ -43,15 +47,15 @@ const defaultFormatter: FormatterCallback = function (
4347
if (allHintCount > 0) {
4448
message = 'Scanned %d files, found %d errors in %d files (%d ms)'
4549
console.log(
46-
nocolor ? message : message.red,
50+
nocolor ? message : chalk.red(message),
4751
allFileCount,
4852
allHintCount,
4953
allHintFileCount,
5054
time
5155
)
5256
} else {
5357
message = 'Scanned %d files, no errors found (%d ms).'
54-
console.log(nocolor ? message : message.green, allFileCount, time)
58+
console.log(nocolor ? message : chalk.green(message), allFileCount, time)
5559
}
5660
})
5761
}

‎src/cli/formatters/unix.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as colors from 'colors'
1+
import * as chalk from 'chalk'
22
import { FormatterCallback } from '../formatter'
33

44
const unixFormatter: FormatterCallback = function (
@@ -8,9 +8,8 @@ const unixFormatter: FormatterCallback = function (
88
) {
99
const nocolor = options.nocolor
1010

11-
if (nocolor !== false) {
12-
colors.enable()
13-
}
11+
const chalkInstance =
12+
nocolor !== false ? new chalk.Instance({ level: 1 }) : chalk
1413

1514
formatter.on('file', (event) => {
1615
event.messages.forEach((message) => {
@@ -30,7 +29,10 @@ const unixFormatter: FormatterCallback = function (
3029
if (allHintCount > 0) {
3130
console.log('')
3231
const message = '%d problems'
33-
console.log(nocolor ? message : message.red, event.allHintCount)
32+
console.log(
33+
nocolor ? message : chalkInstance.red(message),
34+
event.allHintCount
35+
)
3436
}
3537
})
3638
}

‎src/cli/htmlhint.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
import { queue as asyncQueue, series as asyncSeries } from 'async'
4-
import 'colors'
4+
import * as chalk from 'chalk'
55
import * as program from 'commander'
66
import { existsSync, readFileSync, statSync } from 'fs'
77
import * as glob from 'glob'
@@ -112,8 +112,7 @@ function listRules() {
112112

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

0 commit comments

Comments
 (0)
Please sign in to comment.