From 805ab4db9e8bd021921b08848b91e577ad8e3ebd Mon Sep 17 00:00:00 2001 From: Ashton Eby Date: Thu, 21 Nov 2024 10:59:22 -0800 Subject: [PATCH] prevent nodeJS warnings in production this change: - configures ESbuild to rewrite "process.env.NODE_ENV" as "production" during build. - configures the CLI to remove warning event listeners (which include the built-in event listener that logs to stderr on warning) when run in production env. --- package.json | 2 +- src/cli.mjs | 9 +++++++++ test/general-cli.mjs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index feb03fc7..eb5ded3d 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "pretest:ci": "npm run build:app", "test:ci": "mocha --recursive ./test --require ./test/mocha-root-hooks.mjs --reporter mocha-multi-reporters --reporter-options configFile=./test/config/reporter.json", "build": "npm run build:app && npm run build:sea", - "build:app": "esbuild --bundle ./src/user-entrypoint.mjs --platform=node --outfile=./dist/cli.cjs --format=cjs --inject:./sea/import-meta-url.js --define:import.meta.url=importMetaUrl", + "build:app": "esbuild --bundle ./src/user-entrypoint.mjs --platform=node --outfile=./dist/cli.cjs --format=cjs --inject:./sea/import-meta-url.js --define:import.meta.url=importMetaUrl --define:process.env.NODE_ENV=\\\"production\\\"", "build:sea": "node ./sea/build.cjs", "format": "prettier -w ." }, diff --git a/src/cli.mjs b/src/cli.mjs index 6e337172..8e7c669f 100644 --- a/src/cli.mjs +++ b/src/cli.mjs @@ -27,6 +27,9 @@ export async function run(argvInput, _container) { container = _container; const logger = container.resolve("logger"); const parseYargs = container.resolve("parseYargs"); + if (process.env.NODE_ENV === "production") { + process.removeAllListeners("warning"); + } try { builtYargs = buildYargs(argvInput); @@ -83,6 +86,12 @@ function buildYargs(argvInput) { }, builder: {}, }) + .command("warn", false, { + handler: async () => { + process.emitWarning("this is a warning emited on the node process"); + }, + builder: {}, + }) .demandCommand() .strict(true) .options({ diff --git a/test/general-cli.mjs b/test/general-cli.mjs index 8d7c3013..9fa4a0a5 100644 --- a/test/general-cli.mjs +++ b/test/general-cli.mjs @@ -1,5 +1,6 @@ //@ts-check +import { spawnSync } from "node:child_process"; import * as fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; @@ -13,6 +14,8 @@ import { builtYargs, run } from "../src/cli.mjs"; import { setupTestContainer as setupContainer } from "../src/config/setup-test-container.mjs"; import { f } from "./helpers.mjs"; +const __dirname = import.meta.dirname; + describe("cli operations", function () { let container; @@ -124,6 +127,40 @@ describe("cli operations", function () { expect(notify).to.have.been.called; }); + it("enables nodeJS warnings from the dev entrypoint", async function () { + const cliPath = path.resolve(__dirname, "../src/user-entrypoint.mjs"); + let cli = spawnSync(cliPath, ["warn"], { + encoding: "utf8", + // input: "", + timeout: 5000, + // stdio: ["inherit", "pipe", "pipe"], + // shell: true, + }); + if (cli.error) throw cli.error; + let stderr = cli.stderr; + + // the dev script should emit warnings + expect(stderr).to.include( + "Warning: this is a warning emited on the node process", + ); + }); + + it("suppresses nodeJS warnings from the prod entrypoint", async function () { + const cliPath = path.resolve(__dirname, "../dist/cli.cjs"); + let cli = spawnSync(cliPath, ["warn"], { + encoding: "utf8", + // input: "", + timeout: 5000, + // stdio: ["inherit", "pipe", "pipe"], + // shell: true, + }); + if (cli.error) throw cli.error; + + let stderr = cli.stderr; + // the prod one should not + expect(stderr).to.equal(""); + }); + it.skip("should detect color support if the user does not specify", async function () { // i can't find a way to mock this that doesn't involve setting a flag // and setting a flag defeats the purpose of testing if it's _detected_ automatically