From 570e293c039f215d135370dff1dfba2d15128856 Mon Sep 17 00:00:00 2001 From: Patrick Taylor Date: Sat, 26 Oct 2024 23:26:22 +0100 Subject: [PATCH] =?UTF-8?q?Created=20our=20own=20log-symbols=20=E2=9D=87?= =?UTF-8?q?=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - This means we don't have two modules for setting colours. - Ability to customise symbols. --- jest-config/setup.ts | 5 ---- jest.config.ts | 2 +- package.json | 2 +- src/utils/__tests__/log-symbols.spec.ts | 38 +++++++++++++++++++++++++ src/utils/log-symbols.ts | 17 +++++++++++ yarn.lock | 18 ++++-------- 6 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 src/utils/__tests__/log-symbols.spec.ts create mode 100644 src/utils/log-symbols.ts diff --git a/jest-config/setup.ts b/jest-config/setup.ts index c5d16f6..d681567 100644 --- a/jest-config/setup.ts +++ b/jest-config/setup.ts @@ -2,11 +2,6 @@ * MOCKS AND SPIES */ -jest.mock('log-symbols', () => ({ - warning: '!', - error: 'X', -})) - jest.spyOn(process, 'exit').mockImplementation(code => { throw new Error(`process.exit(${code})`) }) diff --git a/jest.config.ts b/jest.config.ts index 04ede1a..ff603eb 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -33,7 +33,7 @@ const config: JestConfigWithTsJest = { '^.+\\.(js|ts)$': 'babel-jest', }, transformIgnorePatterns: [ - './node_modules/(?!(chalk)/)', + './node_modules/(?!(chalk|is-unicode-supported)/)', ], } diff --git a/package.json b/package.json index 9d117ec..c6fb8a2 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "eslint-plugin-sort-destructure-keys": "2.0.0", "eslint-plugin-sort-exports": "0.9.1", "glob": "10.4.5", - "log-symbols": "6.0.0", + "is-unicode-supported": "2.1.0", "markdownlint": "0.35.0", "markdownlint-rule-helpers": "0.26.0", "node-notifier": "10.0.1", diff --git a/src/utils/__tests__/log-symbols.spec.ts b/src/utils/__tests__/log-symbols.spec.ts new file mode 100644 index 0000000..d46072a --- /dev/null +++ b/src/utils/__tests__/log-symbols.spec.ts @@ -0,0 +1,38 @@ +import isUnicodeSupported from 'is-unicode-supported' + +jest.mock('is-unicode-supported') + +describe('logSymbols', () => { + + afterEach(() => { + jest.resetModules() + }) + + it('returns Unicode symbols when supported', () => { + (isUnicodeSupported as jest.Mock).mockReturnValue(true) + + const logSymbols = require('../log-symbols').default + + expect(logSymbols).toEqual({ + error: '✗', + info: 'ℹ', + success: '✓', + tipEmoji: '💡', + warning: '⚠', + }) + }) + + it('returns fallback symbols when Unicode is not supported', () => { + (isUnicodeSupported as jest.Mock).mockReturnValue(false) + + const logSymbols = require('../log-symbols').default + + expect(logSymbols).toEqual({ + error: '×', + info: '[i]', + success: '√', + tipEmoji: 'TIP:', + warning: '‼', + }) + }) +}) diff --git a/src/utils/log-symbols.ts b/src/utils/log-symbols.ts new file mode 100644 index 0000000..98ed4c5 --- /dev/null +++ b/src/utils/log-symbols.ts @@ -0,0 +1,17 @@ +import isUnicodeSupported from 'is-unicode-supported' + +const _isUnicodeSupported = isUnicodeSupported() + +const error = _isUnicodeSupported ? '✗' : '×' +const info = _isUnicodeSupported ? 'ℹ' : '[i]' +const success = _isUnicodeSupported ? '✓' : '√' +const tipEmoji = _isUnicodeSupported ? '💡' : 'TIP:' +const warning = _isUnicodeSupported ? '⚠' : '‼' + +export default { + error, + info, + success, + tipEmoji, + warning, +} diff --git a/yarn.lock b/yarn.lock index 8066fbe..ed0a136 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2431,7 +2431,7 @@ chalk@4.1.2, chalk@^4.0.0, chalk@^4.0.2: ansi-styles "^4.1.0" supports-color "^7.1.0" -chalk@5.3.0, chalk@^5.3.0: +chalk@5.3.0: version "5.3.0" resolved "https://registry.yarnpkg.com/chalk/-/chalk-5.3.0.tgz#67c20a7ebef70e7f3970a01f90fa210cb6860385" integrity sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w== @@ -3461,10 +3461,10 @@ is-stream@^2.0.0: resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.1.tgz#fac1e3d53b97ad5a9d0ae9cef2389f5810a5c077" integrity sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg== -is-unicode-supported@^1.3.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-1.3.0.tgz#d824984b616c292a2e198207d4a609983842f714" - integrity sha512-43r2mRvz+8JRIKnWJ+3j8JtjRKZ6GmjzfaE/qiBJnikNnYv/6bagRJ1kUhNk8R5EX/GkobD+r+sfxCPJsiKBLQ== +is-unicode-supported@2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-2.1.0.tgz#09f0ab0de6d3744d48d265ebb98f65d11f2a9b3a" + integrity sha512-mE00Gnza5EEB3Ds0HfMyllZzbBrmLOX3vfWoj9A9PEnTfratQ/BcaJOuMhnkhjXvb2+FkY3VuHqtAGpTPmglFQ== is-wsl@^2.2.0: version "2.2.0" @@ -4072,14 +4072,6 @@ lodash.truncate@^4.4.2: resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193" integrity sha512-jttmRe7bRse52OsWIMDLaXxWqRAmtIUccAQ3garviCqJjafXOfNMO0yMfNpdD6zbGaTU0P5Nz7e7gAT6cKmJRw== -log-symbols@6.0.0: - version "6.0.0" - resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-6.0.0.tgz#bb95e5f05322651cac30c0feb6404f9f2a8a9439" - integrity sha512-i24m8rpwhmPIS4zscNzK6MSEhk0DUWa/8iYQWxhffV8jkI4Phvs3F+quL5xvS0gdQR0FyTCMMH33Y78dDTzzIw== - dependencies: - chalk "^5.3.0" - is-unicode-supported "^1.3.0" - lru-cache@^10.2.0: version "10.2.2" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.2.2.tgz#48206bc114c1252940c41b25b41af5b545aca878"