user3270899
user3270899

Reputation: 43

Pass argument from script to gulp task

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!

Upvotes: 2

Views: 2548

Answers (1)

RobC
RobC

Reputation: 24982

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.


Upvotes: 2

Related Questions