-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Properly escape extra args #31
Changes from 3 commits
19443b8
2004ecc
d1c4c26
74df3ab
8a5639c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@ECHO off | ||
node %* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
const pathModule = require('path') | ||
const makeSpawnArgs = require('./make-spawn-args.js') | ||
const promiseSpawn = require('@npmcli/promise-spawn') | ||
const packageEnvs = require('./package-envs.js') | ||
const { isNodeGypPackage, defaultGypInstallScript } = require('@npmcli/node-gyp') | ||
const signalManager = require('./signal-manager.js') | ||
const isServerPackage = require('./is-server-package.js') | ||
const isWindows = require('./is-windows.js') | ||
const { ShellString, unquoted } = require('puka'); | ||
|
||
const sh = (...args) => | ||
ShellString.sh(...args).toString(process.env.__FAKE_TESTING_PLATFORM__ || process.platform) | ||
|
||
// you wouldn't like me when I'm angry... | ||
const bruce = (id, event, cmd) => | ||
|
@@ -31,7 +37,7 @@ const runScriptPkg = async options => { | |
if (options.cmd) | ||
cmd = options.cmd | ||
else if (pkg.scripts && pkg.scripts[event]) | ||
cmd = pkg.scripts[event] + args.map(a => ` ${JSON.stringify(a)}`).join('') | ||
cmd = sh`${unquoted(pkg.scripts[event])} ${args}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is how Yarn does it: And how the original PR by the author of |
||
else if ( // If there is no preinstall or install script, default to rebuilding node-gyp packages. | ||
event === 'install' && | ||
!scripts.install && | ||
|
@@ -41,7 +47,15 @@ const runScriptPkg = async options => { | |
) | ||
cmd = defaultGypInstallScript | ||
else if (event === 'start' && await isServerPackage(path)) | ||
cmd = 'node server.js' + args.map(a => ` ${JSON.stringify(a)}`).join('') | ||
// `node` probably means `C:\Program Files\nodejs\node.exe`. | ||
// But it can also point to `.\node_modules\.bin\node.cmd` if you install a | ||
// Node.js version using `npm i node`. | ||
// So it might point to an executable, or a batch file. They require | ||
// different escaping. Also, `puka` only supports batch-style escaping. | ||
// The solution is to always use a batch file on Windows. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this comment mean that if a user has a script that runs a command that is not a batch file, we will be escaping it wrong? if i have {
"scripts": {
"go": "node server.js"
}
} and i run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You’re right, this won’t be using the batch file wrapper. Running Ideas:
In summary: This is super easy to do right on macOS and Linux, and a minefield on Windows. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The more I think about this, the more I realize something:
|
||
cmd = isWindows | ||
? sh`${pathModule.join(__dirname, 'node.cmd')} server.js ${args}` | ||
: sh`node server.js ${args}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: This means that
instead of:
Let me know if you’d rather want to print |
||
|
||
if (!cmd) | ||
return { code: 0, signal: null } | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
puka
comes with ash
export (defined asconst sh = (...args) => ShellString.sh(...args).toString()
basically), but we’re making our own so we can test the Windows version more easily.https://gitlab.com/rhendric/puka#shellstring
process.env.__FAKE_TESTING_PLATFORM__ || process.platform
is copied from is-windows.js – let me know if you’d like to share that, and if so, how.