将参数传递给npm脚本的中间

Rya*_*lor 4 npm

标题说明了一切.我希望能够将参数传递到npm脚本的中间,以便我可以执行以下操作.

$ npm run deploy -- <destination-path>
Run Code Online (Sandbox Code Playgroud)

在package.json中

"scripts": {
    "deploy": "robocopy dist <destination-path> /E /NP"
 }
Run Code Online (Sandbox Code Playgroud)

这可能不使用环境变量或npm的配置变量吗?

Rya*_*lor 7

每次将args传递给运行脚本#5518,似乎不可能将参数传递到脚本的中间.

抱歉,我们不会支持将args传递到脚本中间.如果您真的需要这个,请使用任何人使用的任何命令行解析器编写测试命令.(Minimist,dashdash,nopt和指挥官都支持这个很好.)

但是,此处已经记录了使用npm配置块的替代方法.我的实现看起来像这样:

"name": "foo"
"config": { "destination" : "deploy" },
"scripts": { "deploy": "robocopy dist %npm_package_config_destination% /E /NP" }
Run Code Online (Sandbox Code Playgroud)

然后我可以在命令行和我的构建服务器上覆盖它:

npm run deploy --foo:destination=C:\path\to\deploy\dir
Run Code Online (Sandbox Code Playgroud)