Fork me on GitHub

Gear.js

Task-Based Build System

Gear.js is a scriptable build system using simple tasks that act like a sequence of piped commands.

Features include:

Installation

Node

To get the most out of Gear.js, install gear-lib which contains tasks for linting, minifying, and deploying JS/CSS assets.

$ npm install gear
$ npm install gear-lib

Browser

<script type="text/javascript" src="https://github.com/yahoo/gear-lib/raw/master/build/gear-lib.js"></script>
<script type="text/javascript" src="https://github.com/yahoo/gear/raw/master/build/gear.js"></script>

Quick Examples

Chaining Tasks

new Queue()
    .read('foo.js')
    .log('read foo.js')
    .inspect()
    .write('foo2.js')
    .run();

Execute Tasks Using Array Style

new Queue()
    .read(['foo.js', {name: 'bar.js'}, 'baz.js'])
    .log('read files')
    .inspect()
    .write(['foo2.js', 'bar2.js']) // Not writing 'baz.js'
    .run();

Parallel Task Execution

new Queue()
    .read('foo.js')
    .log('Parallel Tasks')
    .tasks({
        read:     {task: ['read', ['bar.js', 'baz.js']]},
        combine:  {requires: 'read', task: 'concat'},
        minify:   {requires: 'combine', task: 'jsminify'},
        print:    {requires: 'minify', task: 'inspect'},
        parallel: {task: ['log', "Hello Gear.js world!"]}, // Run parallel to read
        join:     {requires: ['parallel', 'print']} // Joined data is passed to next task in queue
     })
     .inspect()
     .run();

Documentation

Queue

Registry

Tasks

Library Tasks

Custom Tasks

Queue()

Queue(options)

Queue constructor.

Arguments

  • options - (Object) Options for queue.
  • options.logger - (Object) Logger instance, usually console.
  • options.registry - (Registry) Registry loaded with available tasks.
new Queue()
    .log('New queue')
    .run();

Queue.task(name)

Queue.task(name, options)

Helper method to run the specified task. Preferred task execution style is to call the task directly i.e. inspect() instead of task('inspect').

Arguments

  • name - (String) Name of task in registry.
  • options - Task specific options.
new Queue()
    .task('log', 'New queue')
    .run();

Queue.run()

Queue.run(callback)

Runs the queue.

Arguments

  • callback - (Function(err, results)) Called on queue completion.
new Queue()
    .log('test')
    .run();

Registry()

Registry(options)

Creates a new Registry instance. Registries contain available tasks.

Arguments

  • options - See Registry.load.
new Registry();

Registry.load(options)

Load tasks from NPM module, directory, or file.

Arguments

  • options.module - Module to load tasks from.
  • options.dirname - Directory to load tasks from.
  • options.filename - File to load tasks from.
  • options.tasks - Object to load tasks from.
new Registry().load({dirname: 'foo'});

read(name)

read(options)

Appends file contents onto queue.

Arguments

  • name - (String) Filename to read.
new Queue()
    .read('foo.js')
    .read(['foo.js', 'bar.js'])
    .read([{name: 'foo.js'}, {name: 'bar.js'}, {name: 'baz.js'}])
    .run(function(err, results) {
        console.log(('' + results[0]).length + ' characters');
    });

write(name)

write(options)

Write the blob to disk.

Arguments

  • name - (String) File to write, will replace {checksum} with hash of blob content.
new Queue()
    .read('foo.js')
    .write('foo2.js')
    .write({name: 'foo3.js'})
    .run();

concat()

concat(callback)

Concatenates blobs.

Arguments

  • callback - (Function) N/A.
new Queue()
    .read(['foo.js', 'bar.js'])
    .concat()
    .run();

inspect()

Inspects blobs.

new Queue()
    .read(['foo.js', 'bar.js'])
    .inspect()
    .run();

log(message)

Log a message.

Arguments

  • message - (String) Message to log.
new Queue()
    .log('Hi')
    .run();

tasks(workflow)

Arguments

  • workflow - (Object) Task workflow described below.
// label - Task instance name.
// label.task - Task name and optionally options.
// label.requires - List of labels that must be executed before this task runs.
new Queue()
    .read('foo.js')
    .tasks({
        dev:     {task: ['write', 'output.js']},
        prodmin: {task: 'jsminify'},
        prod:    {requires: 'prodmin', task: ['write', 'output.min.js']},
        join:    {requires: ['dev', 'prod']}
    })
    .run(function() {
        console.log('output.js - ' + localStorage['output.js'].length);
        console.log('output.min.js - ' + localStorage['output.min.js'].length);
    });

Library Tasks

Install gear-lib which contains tasks such as:

  • jslint
  • jsminify
  • csslint
  • cssminify
  • s3
$ npm install gear-lib

Custom Tasks

Writing a task is especially easy compared to other Node build systems. Tasks operate on simple immutable blobs. The task returns a transformed blob via its callback.

Arguments

  • options - Options for the task.
  • blob - Immutable blob.
  • done(err, result) - Callback executed when task is complete.
// example.js
// Example task creates new blob containing `string`
exports.example = function(string, blob, done) {
    done(null, new blob.constructor(string)); // blob.constructor() is equivalent to Blob()
};

Running Example Task

new Queue({registry: new Registry({filename: 'example.js'})})
 .example('EXAMPLE')
 .run();

Who's Using Gear.js

Mojito Shaker by Yahoo.

Special Thanks

Gear.js takes inspiration from a few sources: