@@ -3,6 +3,7 @@ import {basename, dirname, extname, normalize, sep, posix} from 'path'
33import { Intersection , JSONSchema , LinkedJSONSchema , NormalizedJSONSchema , Parent } from './types/JSONSchema'
44import { JSONSchema4 } from 'json-schema'
55import yaml from 'js-yaml'
6+ import type { Format } from 'cli-color'
67
78// TODO: pull out into a separate package
89export function Try < T > ( fn : ( ) => T , err : ( e : Error ) => any ) : T {
@@ -242,7 +243,7 @@ export function log(style: LogStyle, title: string, ...messages: unknown[]): voi
242243 if ( messages . length > 1 && typeof messages [ messages . length - 1 ] !== 'string' ) {
243244 lastMessage = messages . splice ( messages . length - 1 , 1 )
244245 }
245- console . info ( require ( 'cli- color' ) . whiteBright . bgCyan ( 'debug' ) , getStyledTextForLogging ( style ) ?.( title ) , ...messages )
246+ console . info ( color ( ) ? .whiteBright . bgCyan ( 'debug' ) , getStyledTextForLogging ( style ) ?.( title ) , ...messages )
246247 if ( lastMessage ) {
247248 console . dir ( lastMessage , { depth : 6 , maxArrayLength : 6 } )
248249 }
@@ -254,19 +255,19 @@ function getStyledTextForLogging(style: LogStyle): ((text: string) => string) |
254255 }
255256 switch ( style ) {
256257 case 'blue' :
257- return require ( 'cli- color' ) . whiteBright . bgBlue
258+ return color ( ) ? .whiteBright . bgBlue
258259 case 'cyan' :
259- return require ( 'cli- color' ) . whiteBright . bgCyan
260+ return color ( ) ? .whiteBright . bgCyan
260261 case 'green' :
261- return require ( 'cli- color' ) . whiteBright . bgGreen
262+ return color ( ) ? .whiteBright . bgGreen
262263 case 'magenta' :
263- return require ( 'cli- color' ) . whiteBright . bgMagenta
264+ return color ( ) ? .whiteBright . bgMagenta
264265 case 'red' :
265- return require ( 'cli- color' ) . whiteBright . bgRedBright
266+ return color ( ) ? .whiteBright . bgRedBright
266267 case 'white' :
267- return require ( 'cli- color' ) . black . bgWhite
268+ return color ( ) ? .black . bgWhite
268269 case 'yellow' :
269- return require ( 'cli- color' ) . whiteBright . bgYellow
270+ return color ( ) ? .whiteBright . bgYellow
270271 }
271272}
272273
@@ -411,3 +412,11 @@ export function parseFileAsJSONSchema(filename: string | null, contents: string)
411412function isYaml ( filename : string ) {
412413 return filename . endsWith ( '.yaml' ) || filename . endsWith ( '.yml' )
413414}
415+
416+ function color ( ) : Format {
417+ let cliColor
418+ try {
419+ cliColor = require ( 'cli-color' )
420+ } catch { }
421+ return cliColor
422+ }
0 commit comments