Repository: fgnass/node-dev
Pull request #276 (2022-03-22 20:11:33)

Handle --no-warnings
Supercedes #275 

@lehni Thanks for trying, I know the code is not particularly easy to understand.



Patch:
diff --git a/lib/cli.js b/lib/cli.js
index 3efcaa0..4edc038 100644
--- a/lib/cli.js
+++ b/lib/cli.js
@@ -11,7 +11,7 @@ const resolvePath = p => resolve(process.cwd(), p);
 
 const nodeAlias = { require: 'r' };
 const nodeBoolean = ['expose_gc', 'preserve-symlinks'];
-const nodeOptional = ['inspect', 'inspect-brk'];
+const nodeCustom = ['inspect', 'inspect-brk', 'no-warnings'];
 const nodeString = ['require'];
 
 const nodeDevBoolean = ['clear', 'dedupe', 'fork', 'notify', 'poll', 'respawn', 'vm'];
@@ -44,10 +44,10 @@ const nodeArgsReducer =
     return out;
   };
 
-const nodeOptionalFactory = args => arg => {
-  const isNodeOptional = nodeOptional.includes(arg.substring(2));
-  if (isNodeOptional) args.push(arg);
-  return !isNodeOptional;
+const nodeCustomFactory = args => arg => {
+  const isNodeCustom = nodeCustom.includes(arg.substring(2));
+  if (isNodeCustom) args.push(arg);
+  return !isNodeCustom;
 };
 
 const unknownFactory = args => arg => {
@@ -56,8 +56,8 @@ const unknownFactory = args => arg => {
 };
 
 module.exports = argv => {
-  const nodeOptionalArgs = [];
-  const args = argv.slice(2).filter(nodeOptionalFactory(nodeOptionalArgs));
+  const nodeCustomArgs = [];
+  const args = argv.slice(2).filter(nodeCustomFactory(nodeCustomArgs));
 
   const unknownArgs = [];
   const unknown = unknownFactory(unknownArgs);
@@ -71,7 +71,7 @@ module.exports = argv => {
   const opts = minimist(args, { alias, boolean, default: getConfig(script) });
   const nodeArgs = [...nodeBoolean.map(argify), ...nodeString.map(argify), ...unknownArgs]
     .sort((a, b) => a.key - b.key)
-    .reduce(nodeArgsReducer(opts), [...nodeOptionalArgs]);
+    .reduce(nodeArgsReducer(opts), [...nodeCustomArgs]);
 
   opts.ignore = arrayify(opts.ignore).map(resolvePath);
 
diff --git a/lib/index.js b/lib/index.js
index b1383de..d5e37c7 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -93,21 +93,24 @@ module.exports = function (
    */
   function start() {
     isPaused = false;
-    const cmd = nodeArgs.concat(wrapper, script, scriptArgs);
+
+    const args = nodeArgs.slice();
 
     if (extname(script) === '.mjs' || getPackageType.sync(script) === 'module') {
       if (semver.satisfies(process.version, '>=10 <12.11.1')) {
         const resolveLoader = resolveMain(localPath('resolve-loader.mjs'));
-        cmd.unshift('--experimental-modules', `--loader=${resolveLoader}`);
+        args.push('--experimental-modules', `--loader=${resolveLoader}`);
       } else if (semver.satisfies(process.version, '>=12.11.1')) {
         const loaderPath = semver.satisfies(process.version, '>=16.12.0')
           ? 'load-loader.mjs'
           : 'get-source-loader.mjs';
         const experimentalLoader = resolveMain(localPath(loaderPath));
-        cmd.unshift(`--experimental-loader=${experimentalLoader}`);
+        args.push(`--experimental-loader=${experimentalLoader}`);
       }
     }
 
+    const cmd = args.concat(wrapper, script, scriptArgs);
+
     child = fork(cmd[0], cmd.slice(1), {
       cwd: process.cwd(),
       env: process.env


Test patch:
diff --git a/test/cli.js b/test/cli.js
index e882646..64bfa4d 100644
--- a/test/cli.js
+++ b/test/cli.js
@@ -200,6 +200,13 @@ tap.test('--inspect', t => {
   t.end();
 });
 
+tap.test('--no-warnings', t => {
+  const { nodeArgs } = cli(['node', 'bin/node-dev', '--no-warnings', 'test']);
+
+  t.same(nodeArgs, ['--no-warnings']);
+  t.end();
+});
+
 tap.test('--require source-map-support/register --require ts-node/register', t => {
   const { nodeArgs } = cli([
     'node',
diff --git a/test/spawn/esmodule.js b/test/spawn/esmodule.js
index 2e6e03a..d02734f 100644
--- a/test/spawn/esmodule.js
+++ b/test/spawn/esmodule.js
@@ -4,7 +4,8 @@ const tap = require('tap');
 const { spawn, touchFile } = require('../utils');
 
 tap.test('Supports ECMAScript modules with experimental-specifier-resolution', t => {
-  if (semver.satisfies(process.version, '<12.17')) return t.skip();
+  if (semver.satisfies(process.version, '<12.17'))
+    return t.skip('experimental-specifier-resolution requires node >= 12.17');
   if (process.platform === 'win32') return t.skip('ESM support on windows is broken');
 
   spawn('--experimental-specifier-resolution=node resolution.mjs', out => {
@@ -51,3 +52,21 @@ tap.test('Supports ECMAScript module packages', t => {
     }
   });
 });
+
+tap.test('We can hide the experimental warning by passing --no-warnings', t => {
+  if (process.platform === 'win32') return t.skip('ESM support on windows is broken');
+
+  spawn('--no-warnings ecma-script-modules.mjs', out => {
+    if (out.match(/ExperimentalWarning/)) return t.fail('Should not log an ExperimentalWarning');
+
+    if (out.match(/touch message.mjs/)) {
+      touchFile('message.mjs');
+      return out2 => {
+        if (out2.match(/Restarting/)) {
+          t.match(out2, /\[INFO\] \d{2}:\d{2}:\d{2} Restarting/);
+          return { exit: t.end.bind(t) };
+        }
+      };
+    }
+  });
+});