Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

Found something wrong or missing? Open an issue!

documentation

Table of contents

Introduction

The module has 2 static methods

Gulp.create - Gulp.createClass

the same gulp API methods we know and love

gulp.src - gulp.dest - gulp.task - gulp.watch

and 3 more to bundle/run tasks

gulp.series - gulp.parallel - gulp.stack

Setup

  1. Install npm install --save-dev gulp-runtime

  2. Open a gulpfile, or create one, and

change this line

var gulp = require('gulp');

with

var gulp = require('gulp-runtime').create();

After that run the gulpfile with node directly from the command line

node gulpfile.js default watch serve

Thats it! When no arguments are given the default task will run instead.

  1. What about the CLI? Can I just run gulp-runtime from the terminal?

Yes. For this add an alias to your .bashrc/.zshrc

alias gulp-runtime='node $(find . -name "gulpfile.js" -not -path "./node_modules/*" | head -n1)'

which will use the first gulpfile.js found in the current working directory excluding node_modules.

Right after this open a new terminal tab and write

gulp-runtime --tasks default watch serve

API

Static methods

The module exports a constructor function

var Gulp = require('gulp-runtime');

which has two static methods: Gulp.create and Gulp.createClass.

Gulp.create

function create([Object props])

Gulp.create returns a new instance with the given props.

Defaults are:

  • props.log = true task logging is enabled, pass false to disable it

  • props.repl = false the REPL is disabled, pass true to enable it

  • props.wait = false tasks will run in parallel by default. Pass wait: true to make series the default when running tasks

  • props.onStackEnd called when a stack has ended, defaults to empty function

  • props.onHandleEnd called after a task has ended, defaults to empty function

  • props.onHandleStart called before a task starts, defaults to empty function

  • props.onHandleError called when a task throws, defaults to empty function

These callbacks can be overridden gulp.series, gulp.parallel and gulp.stack.

Gulp.createClass

function createClass([Object mixin])

Gulp.createClass returns a new constructor function that inherits from its parent prototype.

  • When mixin is given it overrides its parent prototype.
  • When mixin.create is given it will be used as the instance constructor.

Example:

Say we always want to make instances that log and have a REPL.

var Gulp = require('gulp-runtime').createClass({
  create: function Gulp (props) {
    props = props || {};
    props.log = props.repl = true;
    Gulp.super_.call(this, props);
  }
});

exports = module.exports = Gulp;

Instance methods

gulp.task

gulp.src, gulp.dest, gulp.watch and gulp.task behave the same as described in the gulp API documentation.

In addition task names can use :parameters (like expressjs routes) and have arguments passed from other task or task runner.

:parameters example:

var gulp = require('gulp-runtime').create();

gulp.task('build:mode', function (done) {
  console.log(this.params.mode);
  done(); // or do async things
});

done will be always passed as first argument to the task. It should be used if the task does not return a stream, promise or RxJS observable.

Tasks parameters can also use regular expressions using parens right after the parameter

var gulp = require('gulp-runtime').create();

gulp.task('build:mode(-dev|-prod)', function (done){
  done(); // or do async things
});

To know more about how this tasks names can be set see parth.

arguments example:

var gulp = require('gulp-runtime').create();

gulp.task('build', function (done, sources, dest) {
  var stream = gulp.src(sources)
    // some build steps here

  stream.on('end', function () {
    // pass stream to next task
    done(stream);
  });
});

gulp.task('minify', function (done, stream) {
  return stream
    // minify
    .pipe(gulp.dest(dest));
});

gulp.task('build and min', function (done) {
  gulp.series('build', 'minify')('src/**/*.js', 'dest/source.min.js', done);
});

gulp.series

function series(tasks...[, Object options])

series bundles the given tasks... into one async function and returns it. This function will always run the tasks... in series.

Its sugar on top of [gulp.stack][#gulpstack]. See [gulp.stack][#gulpstack] for more information about options.

gulp.parallel

function parallel(tasks...[, Object options])

parallel bundles the given tasks... into one async function and returns it. This function will always run the tasks... in parallel.

Its sugar on top of [gulp.stack][#gulpstack]. See [gulp.stack][#gulpstack] for more information about options.

gulp.stack

function stack(tasks...[, Object options])

stack bundles the given tasks... into one async function and returns it.

Each tasks... can be either a string or a function.

If given, options will override the instance props for this stack.

CLI

The initial aim of this project was to be able to run gulp tasks directly from a REPL. But when that was then possible, one also wants to be able to run the CLI while using the REPL, right?

For this reason the gulp cli commands are set as tasks for each instance. Which lets one use them as tasks.

Example:

var gulp = require('gulp-runtime').create();

gulp.task('info', ['--tasks', '--version']);
// other tasks...
gulp.task('default', ['info']);

REPL

var gulp = require('gulp-runtime').create({repl: true});

When an instance passes repl: true the process running does not exit when all tasks are done. What happens then its that the process will have a REPL listening on stdin.

$ node gulpfile.js

press enter and you will see a prompt > that will run the tasks defined in the gulpfile

>
> build less compress png

This way you can run tasks in the same way you run commands on the terminal.

How will tasks run with the REPL?

  • If those tasks are defined they will run in parallel
  • If there is more than one instance with repl: true the REPL will go through them and run the first task that matched one of those tasks
  • If one or more of those tasks is not defined there will be a warning and none of the tasks will run for that instance of any of the other ones.

For more information see gulp-repl.