Skip to content

Commit

Permalink
[pruning] move cli-color to devDependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
bcherny committed Aug 21, 2024
1 parent cb99ce4 commit c09dbcd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
23 changes: 19 additions & 4 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 @@ -51,7 +51,6 @@
"@apidevtools/json-schema-ref-parser": "^11.5.5",
"@types/json-schema": "^7.0.15",
"@types/lodash": "^4.17.0",
"cli-color": "^2.0.4",
"glob": "^10.3.12",
"is-glob": "^4.0.3",
"js-yaml": "^4.1.0",
Expand All @@ -71,6 +70,7 @@
"ava": "^6.1.2",
"browserify": "^17.0.0",
"browserify-shim": "^3.8.16",
"cli-color": "^2.0.4",
"concurrently": "^8.2.2",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
25 changes: 17 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {basename, dirname, extname, normalize, sep, posix} from 'path'
import {Intersection, JSONSchema, LinkedJSONSchema, NormalizedJSONSchema, Parent} from './types/JSONSchema'
import {JSONSchema4} from 'json-schema'
import yaml from 'js-yaml'
import type {Format} from 'cli-color'

// TODO: pull out into a separate package
export function Try<T>(fn: () => T, err: (e: Error) => any): T {
Expand Down Expand Up @@ -242,7 +243,7 @@ export function log(style: LogStyle, title: string, ...messages: unknown[]): voi
if (messages.length > 1 && typeof messages[messages.length - 1] !== 'string') {
lastMessage = messages.splice(messages.length - 1, 1)
}
console.info(require('cli-color').whiteBright.bgCyan('debug'), getStyledTextForLogging(style)?.(title), ...messages)
console.info(color()?.whiteBright.bgCyan('debug'), getStyledTextForLogging(style)?.(title), ...messages)
if (lastMessage) {
console.dir(lastMessage, {depth: 6, maxArrayLength: 6})
}
Expand All @@ -254,19 +255,19 @@ function getStyledTextForLogging(style: LogStyle): ((text: string) => string) |
}
switch (style) {
case 'blue':
return require('cli-color').whiteBright.bgBlue
return color()?.whiteBright.bgBlue
case 'cyan':
return require('cli-color').whiteBright.bgCyan
return color()?.whiteBright.bgCyan
case 'green':
return require('cli-color').whiteBright.bgGreen
return color()?.whiteBright.bgGreen
case 'magenta':
return require('cli-color').whiteBright.bgMagenta
return color()?.whiteBright.bgMagenta
case 'red':
return require('cli-color').whiteBright.bgRedBright
return color()?.whiteBright.bgRedBright
case 'white':
return require('cli-color').black.bgWhite
return color()?.black.bgWhite
case 'yellow':
return require('cli-color').whiteBright.bgYellow
return color()?.whiteBright.bgYellow
}
}

Expand Down Expand Up @@ -411,3 +412,11 @@ export function parseFileAsJSONSchema(filename: string | null, contents: string)
function isYaml(filename: string) {
return filename.endsWith('.yaml') || filename.endsWith('.yml')
}

function color(): Format {
let cliColor
try {
cliColor = require('cli-color')
} catch {}
return cliColor
}

0 comments on commit c09dbcd

Please sign in to comment.