注意:它仅适用于 shell 环境,不适用于 Windows cmd。您应该像 Git Bash 一样在 Windows 上使用 bash。或者如果您使用的是 win10,请尝试 Linux 子系统。
将参数传递给脚本
将参数传递给npm 脚本 https://docs.npmjs.com/cli/run-script,你应该在之后提供它们--
为了安全。
就你而言,--
可以省略。他们的行为是一样的:
npm run test -- 8080 production
npm run test 8080 production
但是当参数包含选项时(例如-p
), --
是必要的,否则 npm 将解析它们并将它们视为 npm 的选项。
npm run test -- 8080 -p
使用位置参数
参数只是附加到要运行的脚本中。你的$1
$2
不会得到解决。 npm 实际运行的命令是:
node mytest.js $1 $2 | node_modules/tap-difflet/bin/tap-difflet "8080" "production"
为了使位置变量在 npm 脚本中工作,请将命令包装在 shell 函数中:
"scripts": {
"test": "run(){ node mytest.js $1 $2 | node_modules/tap-difflet/bin/tap-difflet; }; run"
}
或者使用工具scripty https://github.com/testdouble/scripty并将您的脚本放入单独的文件中。
包.json:
"scripts": {
"test": "scripty"
}
脚本/测试:
#!/usr/bin/env sh
node mytest.js $1 $2 | node_modules/tap-difflet/bin/tap-difflet