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

查看:33
本文介绍了将参数从脚本传递给 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 传递的参数,您需要使用节点 process.argv

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 传递的每个参数的数组,即我们使用数组省略了上面第 1 点中的前三项splice() 方法.

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 中定义的当前脚本,当然还有对您的开始 脚本.运行时:

  • 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 是通过 CLI 传递的.

--quux 是通过 CLI 传递的.

  • 运行:

  • Running:

    $ npm start -- --quux
    

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

    you will see just the following printed to the console:

    --quux 是通过 CLI 传递的.

  • 当然还有运行:

  • And of course, running:

    $ npm start
    

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

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

    脚注:

    1 -- {@} 可以替换为 -- {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天全站免登陆