Repository: fgnass/node-dev (js)

Issue:
[v6.1.0] Fails if you have two required modules
> Sorry I'm a little late to the game, but if you have two required modules:
> 
> ```
> node-dev -r dotenv-safe/config -r ts-node/register/transpile-only
> ```
> 
> Then it fails with 
> ```
> Error: Cannot find module 'dotenv-safe/config,ts-node/register/transpile-only'
> ```
> 
> I suspect it is a consequence of this "wrangling"


_Originally posted by @jraoult in https://github.com/fgnass/node-dev/pull/237#issuecomment-709146208_

Pull request description:
v6.2.0 / 2020-10-15
- Handle multiple values of arguments in command line (Fixes #238)

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7f65afb..a20f9d0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # node-dev
 
+## v6.2.0 / 2020-10-15
+
+- Handle multiple values of arguments in command line (Fixes #238)
+
 ## v6.1.0 / 2020-10-15
 
 - Manually wrangle node args so that we can handle `--` args coming before `-` args (Fixes #236)
diff --git a/lib/cli.js b/lib/cli.js
index 772c79c..6a7c122 100644
--- a/lib/cli.js
+++ b/lib/cli.js
@@ -20,6 +20,17 @@ function getFirstNonOptionArgIndex(args) {
   return args.length - 1;
 }
 
+function unique(k) {
+  const seen = [];
+  return o => {
+    if (!seen.includes(o[k])) {
+      seen.push(o[k]);
+      return true;
+    }
+    return false;
+  };
+}
+
 module.exports = argv => {
   const unknownArgs = [];
 
@@ -42,11 +53,19 @@ module.exports = argv => {
     }
   });
 
-  const nodeArgs = unknownArgs.reduce((out, { arg, key }) => {
-    out.push(arg);
-    if (typeof opts[key] !== 'boolean' && !arg.includes('=')) {
-      out.push(opts[key]);
+  const nodeArgs = unknownArgs.filter(unique('key')).reduce((out, { arg, key }) => {
+    const value = opts[key];
+
+    if (typeof value !== 'boolean' && !arg.includes('=')) {
+      if (Array.isArray(value)) {
+        value.forEach(v => out.push(arg, v));
+      } else {
+        out.push(arg, value);
+      }
+    } else {
+      out.push(arg);
     }
+
     return out;
   }, []);
 
diff --git a/package.json b/package.json
index 9d3e439..b2a62c3 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "node-dev",
-  "version": "6.1.0",
+  "version": "6.2.0",
   "description": "Restarts your app when files are modified",
   "keywords": [
     "restart",


Test patch:
diff --git a/test/cli.js b/test/cli.js
index 84189be..eb8b34d 100644
--- a/test/cli.js
+++ b/test/cli.js
@@ -54,29 +54,34 @@ tap.test('cli overrides .node-dev.json from false to true', t => {
 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.deepEqual(nodeArgs, ['-r', 'ts-node/register', '--inspect']);
   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.deepEqual(nodeArgs, ['--inspect', '-r', 'ts-node/register']);
   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 argv = 'node bin/node-dev --expose_gc test/fixture/gc.js test/fixture/foo'.split(' ');
   const { nodeArgs } = cli(argv);
-  t.ok(nodeArgs.includes('--expose_gc'));
+  t.deepEqual(nodeArgs, ['--expose_gc']);
   t.done();
 });
 
-tap.test('--require=coffeescript/register server.coffee', t => {
+tap.test('--require=coffeescript/register test/fixture/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.deepEqual(nodeArgs, ['--require=coffeescript/register']);
+  t.done();
+});
+
+tap.test('-r coffeescript/register -r ts-node/register test/fixture/server.coffee', t => {
+  const argv = 'node bin/node-dev -r coffeescript/register -r ts-node/register test/fixture/server.coffee'.split(' ');
+  const { nodeArgs } = cli(argv);
+  t.deepEqual(nodeArgs, ['-r', 'coffeescript/register', '-r', 'ts-node/register']);
   t.done();
 });