From a5eefe08236c1902cc0ef8aa5b1541401089a933 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 3 Sep 2024 08:14:37 -0700 Subject: [PATCH] switch back to dynamically including library --- package-lock.json | 3 ++- package.json | 2 +- src/utils.ts | 13 +++++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index d511107d..0ff87b05 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,6 @@ "js-yaml": "^4.1.0", "lodash": "^4.17.21", "minimist": "^1.2.8", - "picocolors": "^1.1.0", "prettier": "^3.2.5" }, "bin": { @@ -38,6 +37,7 @@ "eslint": "^8.56.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-prettier": "^5.1.3", + "picocolors": "^1.1.0", "rimraf": "^5.0.5", "shx": "^0.3.4", "tsify": "^5.0.4", @@ -4631,6 +4631,7 @@ "version": "1.1.0", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.0.tgz", "integrity": "sha512-TQ92mBOW0l3LeMeyLV6mzy/kWr8lkd/hp3mTg7wYK7zJhuBStmGMBG0BdeDZS/dZx1IukaX6Bk11zcln25o1Aw==", + "dev": true, "license": "ISC" }, "node_modules/picomatch": { diff --git a/package.json b/package.json index 248feaca..a6cca5c1 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,6 @@ "js-yaml": "^4.1.0", "lodash": "^4.17.21", "minimist": "^1.2.8", - "picocolors": "^1.1.0", "prettier": "^3.2.5" }, "devDependencies": { @@ -74,6 +73,7 @@ "eslint": "^8.56.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-prettier": "^5.1.3", + "picocolors": "^1.1.0", "rimraf": "^5.0.5", "shx": "^0.3.4", "tsify": "^5.0.4", diff --git a/src/utils.ts b/src/utils.ts index 354ece78..aede06d7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,7 +3,6 @@ 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 pc from 'picocolors' // TODO: pull out into a separate package export function Try(fn: () => T, err: (e: Error) => any): T { @@ -243,7 +242,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(pc.bgCyan(pc.whiteBright('debug')), getStyledTextForLogging(style)?.(title), ...messages) + console.info(color()?.bgCyan(color()?.whiteBright('debug')), getStyledTextForLogging(style)?.(title), ...messages) if (lastMessage) { console.dir(lastMessage, {depth: 6, maxArrayLength: 6}) } @@ -253,6 +252,8 @@ function getStyledTextForLogging(style: LogStyle): ((text: string) => string) | if (!process.env.VERBOSE) { return } + const pc = color() + if (!pc) return switch (style) { case 'blue': return text => pc.bgBlue(pc.whiteBright(text)) @@ -412,3 +413,11 @@ export function parseFileAsJSONSchema(filename: string | null, contents: string) function isYaml(filename: string) { return filename.endsWith('.yaml') || filename.endsWith('.yml') } + +function color() { + let pc + try { + pc = require('picocolors') + } catch {} + return pc +}