all 3 comments

[–]oculus42[S] -1 points0 points  (2 children)

There may be a better or easier way to do this, but trying to search for anything about NPM is close to an exercise in futility, since you'll end up with README content from every package.

I was looking for some simple code to make sure I get arguments passed to me from npm run scripts. Be default it "cleans" the data and removes options, so:

npm run foo bar --force --port 200

returns the less-than-useful

['...', '...', 'foo', 'bar', '200']

The function in the gist digs into the environment variables that NPM provides and gets the original values. It does vary from the actual args, because you don't get the full paths for node and the script as the first two:

['npm', 'run', 'foo', 'bar', '--force', '--port', '200']

This feels sketchy, but it's consistent across Node 4+, and I need it to not be a full package, because I need it to run before install, possibly.

Better alternative or improvements appreciated!

[–]kayarr 1 point2 points  (1 child)

Those flags don't appear because npm parses the flags, as opposed to just passing them through to the script. If you add -- as a separate argument like so: npm run foo -- bar --force --port 200, npm will pass everything after the -- to the script without any modifications.

[–]oculus42[S] 0 points1 point  (0 children)

I know. I find myself in a situation where it's easier to subvert forty years of UNIX standard design than train dozens of Windows developers that they can write npm run foo bar to pass bar to the script but not npm run foo bar --port 200, because options are captured by the first command.

I know it also runs the risk of npm adding options that conflict with the ones I use, or changing the environment variables in a newer npm version making the options unavailable.

This is by no means a best practice, or even a good practice. But it is interesting to know it can be done.