将参数从脚本传递到gulp任务 [英] Pass argument from script to gulp task

查看:216
本文介绍了将参数从脚本传递到gulp任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有package.json脚本,其结构如下:

I have package.json scripts with the following structure:

"scripts": {
    "watch:build": "tsc --watch",
    "watch:server": "nodemon ./src/app.js --watch 'app'",
    "build": "tsc && gulp do_something",
    "start": "npm-run-all clean build --parallel watch:build", 
    "watch:server --print-label"
}

我想以npm run start with_argument的身份启动该应用程序 并将其传递给build脚本,以根据该参数在gulp任务中执行操作.

I would like to start the application as npm run start with_argument and pass it to build script, to perform actions in the gulp task based on that argument.

我阅读了很多教程以及如何撰写文章,但没有结果.可能会以某种方式将参数从一个脚本传递到另一个脚本(这会启动一项gulp任务).

I read a lot of tutorial and how to articles, but without result. It is possible somehow to pass argument from one script to another(which starts a gulp task).

提前谢谢!

推荐答案

npm-run-all提供了自己的自定义机制,可以通过使用npm-scripts中的占位符来处理自变量,如 Argument Placeholders 中所述.在其文档部分找到这里.

npm-run-all provides a its own custom mechanism for handling arguments by utilizing placeholders in npm-scripts, as stated in the Argument Placeholders section of its documentation found here.

鉴于您当前的名为start的npm脚本,您需要按以下方式重新定义它:

Given your current npm-script named start you'll need to redefine it as follows:

"scripts": {
  ...
  "start": "npm-run-all clean \"build -- {@}\" --parallel watch:build --"
  ...
}

注释:

  • -- {@}必须在build之后添加. 1
  • build -- {@}必须用转义的双引号引起来\"...\"
  • 在最后一次脚本调用之后还必须添加
  • --,即:watch:build
  • -- {@} must be added after build.1
  • build -- {@} must be wrapped in escaped double quotes \"...\"
  • -- must also be added after last script invocation, namely: watch:build

要获取在 gulpfile.js 中通过CLI传递的参数,您将需要使用节点

To obtain the arguments passed via the CLI inside your gulpfile.js you''ll need to utilize nodes process.argv

为了演示起见,我们的 gulpfile.js 如下:

For the purpose of demonstration lets say our gulpfile.js is as follows:

var gulp = require('gulp');
var args = process.argv.splice(3, process.argv.length - 3);

gulp.task('doSomething', function() {

  // For testing purposes...
  if (args.indexOf('--foo') > -1) {
    console.log('--foo was passed via the CLI.')
  }

  if (args.indexOf('--quux') > -1) {
    console.log('--quux was passed via the CLI.')
  }
});

注释:

  1. 节点process.argv中的前三项是:

  • 运行JavaScript文件的可执行文件的路径.
  • 正在执行的JavaScript文件的路径.
  • gulp任务的名称,即doSomething

但是,我们只对数组中第四项以后的元素感兴趣-因为这些将是通过CLI传递的参数.该行显示为:

However, we're only interested in the elements from the fourth item in the array onwards - as these will be the arguments passed via the CLI. The line which reads:

var args = process.argv.splice(3, process.argv.length - 3);

创建一个args变量并分配一个包含通过CLI传递的每个参数的数组,即,我们使用数组

creates an args variable and assigns an Array containing each argument passed via the CLI, i.e. we omit the first three aforementioned items in point 1 above using the Arrays splice() method.


运行您的start脚本:

您可以通过CLI调用启动脚本,如下所示:


Running your start script:

You invoke your start script via your CLI as follows:

$ npm start -- --foo --quux

注意:在提供自己的参数之前,您必须提供--npm start之前.

Note You must provide the -- which precedes npm start before providing your own arguments.

  • 使用上述人为设计的gulpfile.js,结合您在 package.json 中定义的当前脚本,当然还要对start脚本进行必要的更改.运行时:

  • Using the contrived gulpfile.js above, in combination with your current scripts defined in your package.json, and of course the necessary changes made to your start script. When you run:

$ npm start -- --foo --quux

您将看到以下内容打印到控制台:

you will see the following printed to the console:

--foo was passed via the CLI.

--quux was passed via the CLI.

  • 运行:

  • Running:

    $ npm start -- --quux
    

    您将只看到以下内容打印到控制台:

    you will see just the following printed to the console:

    --quux was passed via the CLI.

  • 当然,正在运行:

  • And of course, running:

    $ npm start
    

    不打印 gulpfile.js 中定义的任何一条消息.

    Does not print either of the messages defined in gulpfile.js.

    脚注:

    Footnotes:

    1 -- {@}.但是,-- {@}处理多个参数,因此也可以将其用于一个参数.

    1 -- {@} can be replaced with -- {1} if you only intend to pass one argument. However, -- {@} handles multiple arguments, so it's fine to use it for one argument too.

    这篇关于将参数从脚本传递到gulp任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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