Skip to content

Commit

Permalink
prevent nodeJS warnings in production
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
echo-bravo-yahoo committed Nov 21, 2024
1 parent d4a07b2 commit 805ab4d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ."
},
Expand Down
9 changes: 9 additions & 0 deletions src/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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({
Expand Down
37 changes: 37 additions & 0 deletions test/general-cli.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 805ab4d

Please sign in to comment.