Repository: fgnass/node-dev (js)

Issue:
Using -r esm broke with 6.0 release
When running this command: `node_modules/.bin/node-dev -r esm --inspect=0.0.0.0:9229 test_inspect.js`

I get this error:
```
/Users/dlj/projects/test_inspect/node_modules/resolve/lib/sync.js:91
    throw err;
    ^

Error: Cannot find module '--inspect=0.0.0.0:9229' from '/Users/dlj/projects/test_inspect'
    at resolveSync (/Users/dlj/projects/test_inspect/node_modules/resolve/lib/sync.js:89:15)
    at module.exports (/Users/dlj/projects/test_inspect/node_modules/node-dev/lib/resolve-main.js:4:10)
    at getConfig (/Users/dlj/projects/test_inspect/node_modules/node-dev/lib/cfg.js:31:16)
    at Object.<anonymous> (/Users/dlj/projects/test_inspect/node_modules/node-dev/lib/wrap.js:14:34)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at Module._preloadModules (internal/modules/cjs/loader.js:1277:12) {
  code: 'MODULE_NOT_FOUND'
}
```

And unfortunately, using `NODE_OPTIONS` doesn't work (see #235)

Pull request description:
v6.1.0 / 2020-10-15
- Manually wrangle node args so that we can handle `--` args coming before `-` args (Fixes #236)

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7af9066..7f65afb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # node-dev
 
+## v6.1.0 / 2020-10-15
+
+- Manually wrangle node args so that we can handle `--` args coming before `-` args (Fixes #236)
+
 ## v6.0.0 / 2020-10-14
 
 - Support ESModules in node v12.11.1+ using `get-source-loader.mjs` and `resolve-loader.mjs` for earlier versions (Fixes #212)
diff --git a/lib/cli.js b/lib/cli.js
index 84cbd47..772c79c 100644
--- a/lib/cli.js
+++ b/lib/cli.js
@@ -9,18 +9,22 @@ function resolvePath(unresolvedPath) {
   return path.resolve(process.cwd(), unresolvedPath);
 }
 
+const doubleDash = s => /^--/.test(s);
+const dash = s => /^-[^-]*$/.test(s);
+
 function getFirstNonOptionArgIndex(args) {
   for (let i = 2; i < args.length; i += 1) {
-    if (args[i][0] != '-') return i;
+    if (!doubleDash(args[i]) && !dash(args[i]) && !dash(args[i - 1] || '')) return i;
   }
 
-  return args.length;
+  return args.length - 1;
 }
 
 module.exports = argv => {
-  const nodeArgs = [];
+  const unknownArgs = [];
 
   const scriptIndex = getFirstNonOptionArgIndex(argv);
+
   const script = argv[scriptIndex];
   const scriptArgs = argv.slice(scriptIndex + 1);
   const devArgs = argv.slice(2, scriptIndex);
@@ -29,15 +33,25 @@ module.exports = argv => {
     boolean: ['clear', 'dedupe', 'fork', 'notify', 'poll', 'respawn', 'vm'],
     string: ['graceful_ipc', 'ignore', 'timestamp'],
     default: getConfig(script),
-    unknown: function (arg) {
-      const argKeys = Object.keys(minimist([arg]));
+    unknown: arg => {
+      const key = Object.keys(minimist([arg]))[1];
 
-      if (configKeys.some(k => argKeys.includes(k))) {
-        return true;
+      if (!configKeys.includes(key)) {
+        unknownArgs.push({ arg, key });
       }
+    }
+  });
 
-      nodeArgs.push(arg);
+  const nodeArgs = unknownArgs.reduce((out, { arg, key }) => {
+    out.push(arg);
+    if (typeof opts[key] !== 'boolean' && !arg.includes('=')) {
+      out.push(opts[key]);
     }
+    return out;
+  }, []);
+
+  unknownArgs.forEach(({ key }) => {
+    delete opts[key];
   });
 
   opts.ignore = [...Array.isArray(opts.ignore) ? opts.ignore : [opts.ignore]].map(resolvePath);
diff --git a/package.json b/package.json
index 2fe310e..9d3e439 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "node-dev",
-  "version": "6.0.0",
+  "version": "6.1.0",
   "description": "Restarts your app when files are modified",
   "keywords": [
     "restart",


Test patch:
diff --git a/test/cli.js b/test/cli.js
index fbe319c..84189be 100644
--- a/test/cli.js
+++ b/test/cli.js
@@ -50,3 +50,33 @@ tap.test('cli overrides .node-dev.json from false to true', t => {
   t.is(notify, true);
   t.done();
 });
+
+tap.test('-r ts-node/register --inspect test/fixture/server.js', t => {
+  const argv = 'node bin/node-dev -r ts-node/register --inspect test/fixture/server.js'.split(' ');
+  const { nodeArgs } = cli(argv);
+  t.ok(nodeArgs.includes('--inspect'));
+  t.ok(nodeArgs.includes('-r'));
+  t.done();
+});
+
+tap.test('--inspect -r ts-node/register test/fixture/server.js', t => {
+  const argv = 'node bin/node-dev --inspect -r ts-node/register test/fixture/server.js'.split(' ');
+  const { nodeArgs } = cli(argv);
+  t.ok(nodeArgs.includes('--inspect'));
+  t.ok(nodeArgs.includes('-r'));
+  t.done();
+});
+
+tap.test('--expose_gc gc.js foo', t => {
+  const argv = 'node bin/node-dev --expose_gc test/fixture/gc.js foo'.split(' ');
+  const { nodeArgs } = cli(argv);
+  t.ok(nodeArgs.includes('--expose_gc'));
+  t.done();
+});
+
+tap.test('--require=coffeescript/register server.coffee', t => {
+  const argv = 'node bin/node-dev --require=coffeescript/register test/fixture/server.coffee'.split(' ');
+  const { nodeArgs } = cli(argv);
+  t.ok(nodeArgs.includes('--require=coffeescript/register'));
+  t.done();
+});