Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

npm script with regex glob #100

Closed
adamgruber opened this issue Apr 4, 2017 · 6 comments
Closed

npm script with regex glob #100

adamgruber opened this issue Apr 4, 2017 · 6 comments

Comments

@adamgruber
Copy link

This is my npm script:

"cross-env UV_THREADPOOL_SIZE=100 BABEL_DISABLE_CACHE=1 NODE_ENV=test nyc mocha --require test/helper.js \"test/spec/**/*.test.+(js|jsx)\""

After upgrading to 4.0.0, I get this error:

/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `nyc mocha --require test/helper.js test/spec/**/*.test.+(js|jsx)'

Is this a bug with v4 or do I need to change how I am passing my args to mocha?

@adamgruber adamgruber changed the title npm script with npm script with regex glob Apr 4, 2017
@kentcdodds
Copy link
Owner

This might be related to #99

@hgwood
Copy link
Collaborator

hgwood commented Apr 5, 2017

Definitely #99. @adamgruber could you try \\\"test/spec/**/*.test.+(js|jsx)\\\"?

@adamgruber
Copy link
Author

I tried different combinations of escaping but none have worked so far. Here's a simplified script:

"cross-env mocha \\\"test/spec/**/*.test.+(js|jsx)\\\""

and the error:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `cross-env mocha \"test/spec/**/*.test.+(js|jsx)\"'

I tried escaping the parenthesis:

"cross-env mocha \"test/spec/**/*.test.+\\(js|jsx\\)\""

but then I get this:

/bin/sh: jsx): command not found

@hgwood
Copy link
Collaborator

hgwood commented Apr 6, 2017

Applying the same analysis as there.

original script

  • The original text / what we can read in package.json: "cross-env mocha \"test/spec/**/*.test.+(js|jsx)\""
  • The actual JSON value / what the shell sees: cross-env mocha "test/spec/**/*.test.+(js|jsx)"
  • The shell-parsed value / what cross-env gets as process.argv:
[ 'mocha',
  'test/spec/**/*.test.+(js|jsx)' ]
  • Spawn call: spawn('/bin/sh', ['-c', 'mocha test/spec/**/*.test.+(js|jsx)'])
  • What the spawned shell sees: mocha test/spec/**/*.test.+(js|jsx)

Regexp is not quoted so it is interpreted by the shell. That why it says /bin/sh: -c: line 0: syntax error near unexpected token ('`.

suggested fix

  • The original text / what we can read in package.json: "cross-env mocha \\\"test/spec/**/*.test.+(js|jsx)\\\""
  • The actual JSON value / what the shell sees: cross-env mocha \"test/spec/**/*.test.+(js|jsx)\"
  • The shell-parsed value / what cross-env gets as process.argv:
[ 'mocha',
  '"test/spec/**/*.test.+(js|jsx)"' ]
  • Spawn call: spawn('/bin/sh', ['-c', 'mocha "test/spec/**/*.test.+(js|jsx)"'])
  • What the spawned shell sees: mocha "test/spec/**/*.test.+(js|jsx)"

So it should work. I suspect nyc might be adding an additional processing layer here.

@adamgruber did try without nyc?

@hgwood
Copy link
Collaborator

hgwood commented Apr 6, 2017

I guess you could also try test/spec/\\*\\*/\\*.test.+\\(js\\|jsx\\).

@adamgruber
Copy link
Author

@hgwood My example above was without nyc.

Taking your suggestion and escaping not just the parenthesis but also the pipe in my regex fixes it. So my working script is now:

"cross-env mocha --require test/helper.js \"test/spec/**/*.test.+\\(js\\|jsx\\)\""

Thanks for the help.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants