Skip to content

Commit

Permalink
@uppy/companion: remove chalk from dependencies (#5178)
Browse files Browse the repository at this point in the history
With this PR, colorful output won't be available on some Node.js versions.
  • Loading branch information
aduh95 authored May 22, 2024
1 parent 4298dd6 commit e48fdaf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 44 deletions.
4 changes: 2 additions & 2 deletions packages/@uppy/companion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@aws-sdk/s3-presigned-post": "^3.338.0",
"@aws-sdk/s3-request-presigner": "^3.338.0",
"body-parser": "1.20.2",
"chalk": "4.1.2",
"common-tags": "1.8.2",
"connect-redis": "7.1.1",
"content-disposition": "^0.5.4",
Expand Down Expand Up @@ -62,6 +61,7 @@
"prom-client": "15.1.2",
"serialize-error": "^11.0.0",
"serialize-javascript": "^6.0.0",
"supports-color": "8.x",
"tus-js-client": "^4.1.0",
"validator": "^13.0.0",
"ws": "8.17.0"
Expand All @@ -76,7 +76,7 @@
"@types/lodash": "4.14.191",
"@types/morgan": "1.7.37",
"@types/ms": "0.7.31",
"@types/node": "^18.0.3",
"@types/node": "^20.0.0",
"@types/react": "^18.0.0",
"@types/request": "2.48.8",
"@types/webpack": "^5.28.0",
Expand Down
20 changes: 13 additions & 7 deletions packages/@uppy/companion/src/server/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const chalk = require('chalk')
const escapeStringRegexp = require('escape-string-regexp')
const util = require('node:util')
const supportsColors = require('supports-color')

const valuesToMask = []
/**
Expand Down Expand Up @@ -38,6 +38,12 @@ exports.setProcessName = (newProcessName) => {
processName = newProcessName
}

const styleText =
typeof util.styleText === "function" && supportsColors.stderr ?
util.styleText
: (style, text) => text;


/**
* message log
*
Expand All @@ -46,9 +52,9 @@ exports.setProcessName = (newProcessName) => {
* @param {string} params.tag a unique tag to easily search for this message
* @param {string} params.level error | info | debug
* @param {string} [params.traceId] a unique id to easily trace logs tied to a request
* @param {Function} [params.color] function to display the log in appropriate color
* @param {string[]} [params.color] Format(s) that can be passed to `util.styleText`.
*/
const log = ({ arg, tag = '', level, traceId = '', color = (message) => message }) => {
const log = ({ arg, tag = '', level, traceId = '', color = [] }) => {
const time = new Date().toISOString()
const whitespace = tag && traceId ? ' ' : ''

Expand All @@ -66,7 +72,7 @@ const log = ({ arg, tag = '', level, traceId = '', color = (message) => message
const msgString = msgToString()
const masked = maskMessage(msgString)
// eslint-disable-next-line no-console
console.log(color(`${processName}: ${time} [${level}] ${traceId}${whitespace}${tag}`), color(masked))
console.log(styleText(color, `${processName}: ${time} [${level}] ${traceId}${whitespace}${tag}`), styleText(color, masked))
}

/**
Expand All @@ -88,7 +94,7 @@ exports.info = (msg, tag, traceId) => {
* @param {string} [traceId] a unique id to easily trace logs tied to a request
*/
exports.warn = (msg, tag, traceId) => {
log({ arg: msg, tag, level: 'warn', traceId, color: chalk.bold.yellow })
log({ arg: msg, tag, level: 'warn', traceId, color: ['bold', 'yellow'] })
}

/**
Expand All @@ -99,7 +105,7 @@ exports.warn = (msg, tag, traceId) => {
* @param {string} [traceId] a unique id to easily trace logs tied to a request
*/
exports.error = (msg, tag, traceId) => {
log({ arg: msg, tag, level: 'error', traceId, color: chalk.bold.red })
log({ arg: msg, tag, level: 'error', traceId, color: ['bold', 'red'] })
}

/**
Expand All @@ -111,6 +117,6 @@ exports.error = (msg, tag, traceId) => {
*/
exports.debug = (msg, tag, traceId) => {
if (process.env.NODE_ENV !== 'production') {
log({ arg: msg, tag, level: 'debug', traceId, color: chalk.bold.blue })
log({ arg: msg, tag, level: 'debug', traceId, color: ['bold', 'blue'] })
}
}
11 changes: 6 additions & 5 deletions packages/@uppy/companion/test/__tests__/logger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const chalk = require('chalk')
// We don't care about colors in our tests, so force `supports-color` to disable colors.
process.env.FORCE_COLOR = 'false'
const logger = require('../../src/server/logger')

const maskables = ['ToBeMasked1', 'toBeMasked2', 'toBeMasked(And)?Escaped']
Expand Down Expand Up @@ -43,7 +44,7 @@ describe('Test Logger secret mask', () => {
logger.warn('this warning has ToBeMasked1 and toBeMasked2 and case-insensitive TOBEMasKED2')
})

const exptectedMsg = chalk.bold.yellow('this warning has ****** and ****** and case-insensitive ******')
const exptectedMsg = 'this warning has ****** and ****** and case-insensitive ******'

expect(loggedMessage).toBeTruthy()
expect(loggedMessage).toBe(exptectedMsg)
Expand All @@ -54,7 +55,7 @@ describe('Test Logger secret mask', () => {
logger.error(new Error('this error has ToBeMasked1 and toBeMasked2 and case-insensitive TOBEMasKED2'))
})

const exptectedMsg = chalk.bold.red('Error: this error has ****** and ****** and case-insensitive ******')
const exptectedMsg = 'Error: this error has ****** and ****** and case-insensitive ******'

expect(loggedMessage.startsWith(exptectedMsg)).toBeTruthy()
})
Expand All @@ -65,7 +66,7 @@ describe('Test Logger secret mask', () => {
logger.error(err, '', '')
})

const exptectedMsg = chalk.bold.red('Error: this error has ****** and ****** and case-insensitive ******')
const exptectedMsg = 'Error: this error has ****** and ****** and case-insensitive ******'

expect(loggedMessage).toBeTruthy()
expect(loggedMessage.startsWith(exptectedMsg)).toBe(true)
Expand All @@ -79,7 +80,7 @@ describe('Test Logger secret mask', () => {
logger.warn('this warning has ToBeMasked(And)?Escaped but not toBeMaskedEscaped ')
})

const exptectedMsg = chalk.bold.yellow('this warning has ****** but not toBeMaskedEscaped ')
const exptectedMsg = 'this warning has ****** but not toBeMaskedEscaped '

expect(loggedMessage).toBeTruthy()
expect(loggedMessage).toBe(exptectedMsg)
Expand Down
51 changes: 21 additions & 30 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7814,15 +7814,6 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^18.0.3":
version: 18.19.33
resolution: "@types/node@npm:18.19.33"
dependencies:
undici-types: "npm:~5.26.4"
checksum: 10/e5816356e3bcf1af272587d6a95c172199532a86bdb379e4d314a10605463908b36316af51ff6d3c19d9f1965e14a6f62c6a5cbab876aafffe71e1211512084a
languageName: node
linkType: hard

"@types/normalize-package-data@npm:^2.4.0":
version: 2.4.4
resolution: "@types/normalize-package-data@npm:2.4.4"
Expand Down Expand Up @@ -8899,13 +8890,12 @@ __metadata:
"@types/lodash": "npm:4.14.191"
"@types/morgan": "npm:1.7.37"
"@types/ms": "npm:0.7.31"
"@types/node": "npm:^18.0.3"
"@types/node": "npm:^20.0.0"
"@types/react": "npm:^18.0.0"
"@types/request": "npm:2.48.8"
"@types/webpack": "npm:^5.28.0"
"@types/ws": "npm:8.5.3"
body-parser: "npm:1.20.2"
chalk: "npm:4.1.2"
common-tags: "npm:1.8.2"
connect-redis: "npm:7.1.1"
content-disposition: "npm:^0.5.4"
Expand Down Expand Up @@ -8936,6 +8926,7 @@ __metadata:
serialize-error: "npm:^11.0.0"
serialize-javascript: "npm:^6.0.0"
supertest: "npm:6.2.4"
supports-color: "npm:8.x"
tus-js-client: "npm:^4.1.0"
typescript: "npm:~5.4"
validator: "npm:^13.0.0"
Expand Down Expand Up @@ -11634,16 +11625,6 @@ __metadata:
languageName: node
linkType: hard

"chalk@npm:4.1.2, chalk@npm:^4.0.0, chalk@npm:^4.0.2, chalk@npm:^4.1.0, chalk@npm:^4.1.2":
version: 4.1.2
resolution: "chalk@npm:4.1.2"
dependencies:
ansi-styles: "npm:^4.1.0"
supports-color: "npm:^7.1.0"
checksum: 10/cb3f3e594913d63b1814d7ca7c9bafbf895f75fbf93b92991980610dfd7b48500af4e3a5d4e3a8f337990a96b168d7eb84ee55efdce965e2ee8efc20f8c8f139
languageName: node
linkType: hard

"chalk@npm:5.3.0, chalk@npm:^5.0.0, chalk@npm:^5.3.0":
version: 5.3.0
resolution: "chalk@npm:5.3.0"
Expand Down Expand Up @@ -11672,6 +11653,16 @@ __metadata:
languageName: node
linkType: hard

"chalk@npm:^4.0.0, chalk@npm:^4.0.2, chalk@npm:^4.1.0, chalk@npm:^4.1.2":
version: 4.1.2
resolution: "chalk@npm:4.1.2"
dependencies:
ansi-styles: "npm:^4.1.0"
supports-color: "npm:^7.1.0"
checksum: 10/cb3f3e594913d63b1814d7ca7c9bafbf895f75fbf93b92991980610dfd7b48500af4e3a5d4e3a8f337990a96b168d7eb84ee55efdce965e2ee8efc20f8c8f139
languageName: node
linkType: hard

"char-regex@npm:^1.0.2":
version: 1.0.2
resolution: "char-regex@npm:1.0.2"
Expand Down Expand Up @@ -28541,6 +28532,15 @@ __metadata:
languageName: node
linkType: hard

"supports-color@npm:8.x, supports-color@npm:^8.0.0, supports-color@npm:^8.1.1":
version: 8.1.1
resolution: "supports-color@npm:8.1.1"
dependencies:
has-flag: "npm:^4.0.0"
checksum: 10/157b534df88e39c5518c5e78c35580c1eca848d7dbaf31bbe06cdfc048e22c7ff1a9d046ae17b25691128f631a51d9ec373c1b740c12ae4f0de6e292037e4282
languageName: node
linkType: hard

"supports-color@npm:^5.3.0":
version: 5.5.0
resolution: "supports-color@npm:5.5.0"
Expand All @@ -28559,15 +28559,6 @@ __metadata:
languageName: node
linkType: hard

"supports-color@npm:^8.0.0, supports-color@npm:^8.1.1":
version: 8.1.1
resolution: "supports-color@npm:8.1.1"
dependencies:
has-flag: "npm:^4.0.0"
checksum: 10/157b534df88e39c5518c5e78c35580c1eca848d7dbaf31bbe06cdfc048e22c7ff1a9d046ae17b25691128f631a51d9ec373c1b740c12ae4f0de6e292037e4282
languageName: node
linkType: hard

"supports-color@npm:^9.0.0":
version: 9.4.0
resolution: "supports-color@npm:9.4.0"
Expand Down

0 comments on commit e48fdaf

Please sign in to comment.