Skip to content
Nate Fischer edited this page Mar 20, 2016 · 1 revision

A convenience script shelljs/make is also provided to mimic the behavior of a Unix Makefile. In this case all shell objects are global, and command line arguments will cause the script to execute only the corresponding function in the global target object. To avoid redundant calls, target functions are executed only once per script.

Example:

require('shelljs/make');

target.all = function() {
  target.bundle();
  target.docs();
};

target.bundle = function() {
  cd(__dirname);
  mkdir('-p', 'build');
  cd('src');
  cat('*.js').to('../build/output.js');
};

target.docs = function() {
  cd(__dirname);
  mkdir('-p', 'docs');
  var files = ls('src/*.js');
  for(var i = 0; i < files.length; i++) {
    var text = grep('//@', files[i]);     // extract special comments
    text = text.replace(/\/\/@/g, '');    // remove comment tags
    text.toEnd('docs/my_docs.md');
  }
};

To run the target all, call the above script without arguments: $ node make. To run the target docs: $ node make docs.

You can also pass arguments to your targets by using the -- separator. For example, to pass arg1 and arg2 to a target bundle, do $ node make bundle -- arg1 arg2:

require('shelljs/make');

target.bundle = function(argsArray) {
  // argsArray = ['arg1', 'arg2']
  /* ... */
}