Pass argument from script to gulp task

耗尽温柔 提交于 2019-12-20 04:37:22

问题


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"
}

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.

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).

Thanks in advance!


回答1:


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.

npm-script:

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 --"
  ...
}

Notes:

  • -- {@} 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

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

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.')
  }
});

Notes:

  1. The first three items in nodes process.argv are:

    • The path to the executable running the JavaScript file.
    • The path of the JavaScript file being executed.
    • The name of the gulp task, i.e. doSomething
  2. 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);
    

    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.


Running your start script:

You invoke your start script via your CLI as follows:

$ npm start -- --foo --quux

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


Output:

  • 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
    

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


Footnotes:

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.




来源:https://stackoverflow.com/questions/52660989/pass-argument-from-script-to-gulp-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!