Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/companion: remove chalk from dependencies #5178

Merged
merged 9 commits into from
May 22, 2024
Merged

Conversation

aduh95
Copy link
Member

@aduh95 aduh95 commented May 16, 2024

We can use the built-in https://nodejs.org/api/util.html#utilstyletextformat-text utility – it's not (yet?) available on Node.js 18.x, so I implemented a fallback (which consists in outputing the plain text, without the colors).

This comment was marked as off-topic.

Copy link
Contributor

Diff output files
diff --git a/packages/@uppy/companion/lib/server/logger.js b/packages/@uppy/companion/lib/server/logger.js
index 3abec84..7407ff0 100644
--- a/packages/@uppy/companion/lib/server/logger.js
+++ b/packages/@uppy/companion/lib/server/logger.js
@@ -1,8 +1,8 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
-const chalk = require("chalk");
 const escapeStringRegexp = require("escape-string-regexp");
 const util = require("node:util");
+const supportsColors = require("supports-color");
 const valuesToMask = [];
 /**
  * Adds a list of strings that should be masked by the logger.
@@ -34,6 +34,9 @@ let processName = "companion";
 exports.setProcessName = (newProcessName) => {
   processName = newProcessName;
 };
+const styleText = typeof util.styleText === "function" && supportsColors.stderr
+  ? util.styleText
+  : (style, text) => text;
 /**
  * message log
  *
@@ -42,9 +45,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 ? " " : "";
   function msgToString() {
@@ -62,7 +65,10 @@ 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),
+  );
 };
 /**
  * INFO level log
@@ -82,7 +88,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"] });
 };
 /**
  * ERROR level log
@@ -92,7 +98,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"] });
 };
 /**
  * DEBUG level log
@@ -103,6 +109,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"] });
   }
 };

packages/@uppy/companion/package.json Outdated Show resolved Hide resolved
.github/workflows/companion.yml Outdated Show resolved Hide resolved
@aduh95 aduh95 merged commit e48fdaf into 4.x May 22, 2024
21 checks passed
@aduh95 aduh95 deleted the companion-remove-chalk branch May 22, 2024 11:58
github-actions bot added a commit that referenced this pull request May 23, 2024
| Package           |      Version | Package           |      Version |
| ----------------- | ------------ | ----------------- | ------------ |
| @uppy/companion   | 5.0.0-beta.8 | @uppy/xhr-upload  | 4.0.0-beta.5 |
| @uppy/transloadit | 4.0.0-beta.6 | uppy              | 4.0.0-beta.9 |

- @uppy/companion: remove `chalk` from dependencies (Antoine du Hamel / #5178)
- @uppy/transloadit: do not cancel assembly when removing all files (Merlijn Vos / #5191)
- @uppy/xhr-upload: fix regression for lowercase HTTP methods (Antoine du Hamel / #5179)
- meta: improve changelog generator (Antoine du Hamel / #5190)
Murderlon added a commit that referenced this pull request May 28, 2024
* 4.x: (38 commits)
  docs: assume tree-shaking bundler is the most common case (#5160)
  @uppy/core: remove `reason` (#5159)
  Release: uppy@4.0.0-beta.9 (#5194)
  Release: uppy@3.25.5 (#5193)
  @uppy/companion: remove `chalk` from dependencies (#5178)
  @uppy/transloadit: do not cancel assembly when removing all files (#5191)
  @uppy/xhr-upload: fix regression for lowercase HTTP methods (#5179)
  meta: improve changelog generator (#5190)
  Release: uppy@4.0.0-beta.8 (#5189)
  examples: add SvelteKit example (#5181)
  @uppy/companion: fix missing `await`
  Release: uppy@3.25.4 (#5188)
  @uppy/svelte: do not attempt removing plugin before it's created (#5186)
  Update facebook.mdx
  fixup! @uppy/tus: fix no headers passed to companion if argument is a function (#5182)
  @uppy/core: resolve some (breaking) TODOs (#4824)
  @uppy/tus: fix no headers passed to companion if argument is a function (#5182)
  @uppy/companion: fix google drive gsuite export large size (#5144)
  @uppy/companion: encode `uploadId` (#5168)
  @uppy/companion: bump `express-session` (#5177)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants