将npm脚本命令行参数传递给其中的特定脚本 [英] Pass npm script command line arguments to a specific script inside it

查看:82
本文介绍了将npm脚本命令行参数传递给其中的特定脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我必须运行三个npm脚本才能在我的应用程序中获得某些结果.我将它们合并为一个npm脚本.这是我的 package.json :

I have a scenario where I have to run three npm script to achieve some result in my application. I combined them in one npm script. This is my package.json:

"scripts": {
    "setup": "npm install && npm run some-script && npm install",
    "some-script": "gulp some-other-script"
}

我想做的是从命令行将参数传递给 setup 脚本,该参数将进一步传递给 some-script 脚本.

what I would like to do is to pass arguments to setup script from command line which will be passed further to the some-script script.

如果我运行 npm运行脚本---abc = 123 ,则将参数添加到脚本的末尾,但是我想将其传递给特定的脚本(在这种情况下,传递给 npm运行一些脚本).我也尝试重写脚本定义,如下所示:"setup":"npm install&& npm运行某些脚本---sample = sample&& npm install" ,但是没有运气.

If I run npm run script -- --abc=123 the arguments are added at the end of the script but I would like to pass it to specific script (in this case to npm run some-script). I also tried to rewrite script definition like this: "setup": "npm install && npm run some-script -- --sample=sample&& npm install" but with no luck.

我知道shell函数(在此处进行描述:发送命令行参数到npm脚本,然后在此处 https://github.com/npm/npm/issues/9627 ),但我需要一个可以跨平台运行的解决方案.

I'm aware of shell functions (described here: Sending command line arguments to npm script and here https://github.com/npm/npm/issues/9627) but I need a solution which will work cross-platform.

有办法吗?

推荐答案

好,我找到了解决方法.

Ok I found a work around.

第一个目标是能够使用带有参数的脚本,并将其级联到其他脚本的调用:

1st goal was to be able to use some script with arguments, and cascade this to the calls of other scripts :

npm运行主---arg1"main":"npm run script1&& npm run script2"

npm run main -- --arg1 "main": "npm run script1 && npm run script2"

这种方法的问题是,级联只能通过在"npm run script1&& namp run script2"行的末尾添加传递给npm run main的arg来完成.我找不到将其传递给第一个元素的方法:npm run script1

Problem with this approach is that the cascading would only be done by adding the arg passed to npm run main at the END of the line "npm run script1 && npm run script2". I could find no way to pass it to the 1st element : npm run script1

这是我发现的解决方法:

Here is the work around I found :

首先您需要添加package.json:

1st you need to add in package.json:

"config": {
   "arg1": "ARG_VALUE"
},

然后在脚本中将其添加到呼叫中,如下所示:

Then in your script you can add it to the call like this :

"main": "npm run script_no_arg1 && npm run scrip1 -- --%npm_package_config_arg1% && npm run script2 -- --%npm_package_config_component% && npm run script_no_arg2"

最后,您不需要使用任何arg调用它:npm run main但是您需要在启动脚本之前修改ARG_VALUE:)

Finally, You don't need to call it with any arg : npm run main But you need to modify ARG_VALUE before launc the script :)

最后一件事:就我而言,我正在调用gulp任务:

Last thing : in my case I was calling gulp tasks :

"cleardown": "rimraf build lib",
"inline-build-templates": "gulp inline-build-templates",
"step1": "npm run cleardown && npm run inline-build-templates -- --%npm_package_config_arg1%",

它可以通过以下方式将参数传递到gulp任务中:

It works you can get the argument into the gulp task that way :

 gulp.task('inline-build-templates', function() {
   let component = process.argv[3];
   component = component.split('--')[1];
   console.log('RUNNING GULP TASK : inline-build-templates for component ' + component);
   // whatever task gulp
});

希望有帮助.

这篇关于将npm脚本命令行参数传递给其中的特定脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆