Comparing version 0.0.14 to 0.0.15
var async = require('async'), | ||
Registry = require('./registry').Registry; | ||
Registry = require('./registry').Registry, | ||
util = require('util'), | ||
isArray = util.isArray || function(ar) { | ||
return Array.isArray(ar) || | ||
(typeof ar === 'object' && Object.prototype.toString.call(ar) === '[object Array]'); | ||
}; | ||
@@ -45,8 +50,13 @@ function freeze(o) { | ||
switch (task.type) { | ||
case 'broadcast': | ||
async.map( | ||
options.length ? options : [], | ||
function(option, callback) { | ||
// FIXME: incoming message empty | ||
task.fn.call(self, option, {}, callback); | ||
case 'append': | ||
if (options === undefined) { | ||
options = []; | ||
} else { | ||
if (!isArray(options)) { | ||
options = [options]; | ||
} | ||
} | ||
async.map(options, function(option, callback) { | ||
task.call(self, option, undefined, callback); | ||
}, | ||
@@ -57,3 +67,4 @@ function(err, results) { | ||
} | ||
done(err, results); | ||
done(err, messages.concat(results)); | ||
} | ||
@@ -64,3 +75,3 @@ ); | ||
case 'join': | ||
task.fn.call(self, options, messages, function(err, results) { | ||
task.call(self, options, messages, function(err, results) { | ||
if (notify) { | ||
@@ -73,5 +84,19 @@ notify(err, [results]); | ||
case 'take': | ||
messages = messages.slice(0, isArray(options) ? options.length : 1); | ||
async.map(messages, function(message, callback) { | ||
task.call(self, options, message, callback); | ||
}, | ||
function(err, results) { | ||
if (notify) { | ||
notify(err, results); | ||
} | ||
done(err, results); | ||
} | ||
); | ||
break; | ||
default: | ||
async.map(messages, function(message, callback) { | ||
task.fn.call(self, options, message, callback); | ||
task.call(self, options, message, callback); | ||
}, | ||
@@ -78,0 +103,0 @@ function(err, results) { |
@@ -9,20 +9,18 @@ /** | ||
*/ | ||
exports.concat = { | ||
type: 'join', | ||
fn: function(options, messages, done) { | ||
options = options || {}; | ||
var callback = options.callback || function(message) { | ||
return message.body; | ||
}, | ||
result = messages.reduce(function(prev, cur) { | ||
return prev + callback(cur); | ||
}, ''); | ||
var concat = exports.concat = function concat(options, messages, done) { | ||
options = options || {}; | ||
var callback = options.callback || function(message) { | ||
return message.body; | ||
}, | ||
result = messages.reduce(function(prev, cur) { | ||
return prev + callback(cur); | ||
}, ''); | ||
done(null, { | ||
meta: { | ||
total: messages.length | ||
}, | ||
body: result | ||
}); | ||
} | ||
}; | ||
done(null, { | ||
meta: { | ||
total: messages.length | ||
}, | ||
body: result | ||
}); | ||
}; | ||
concat.type = 'join'; |
@@ -8,7 +8,5 @@ /** | ||
*/ | ||
exports.inspect = { | ||
fn: function(options, message, done) { | ||
this._log(message); | ||
done(null, message); | ||
} | ||
exports.inspect = function(options, message, done) { | ||
this._log(message); | ||
done(null, message); | ||
}; |
var fs = require('fs'); | ||
/** | ||
* Loads messages from different sources. | ||
* Loads message from different sources. | ||
* TODO: Add more sources i.e. (url). | ||
@@ -12,18 +12,16 @@ * | ||
*/ | ||
exports.load = { | ||
type: 'broadcast', | ||
fn: function(options, message, done) { | ||
options = (typeof options == 'string') ? {file: options} : options; | ||
var load = exports.load = function load(options, message, done) { | ||
options = (typeof options === 'string') ? {file: options} : options; | ||
if (options.file) { | ||
fs.readFile(options.file, function(err, data) { | ||
done(err, { | ||
meta: { | ||
name: options.file | ||
}, | ||
body: data | ||
}); | ||
if (options.file) { | ||
fs.readFile(options.file, function(err, data) { | ||
done(err, { | ||
meta: { | ||
name: options.file | ||
}, | ||
body: data | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
}; | ||
load.type = 'append'; |
@@ -8,7 +8,5 @@ /** | ||
*/ | ||
exports.log = { | ||
fn: function(string, message, done) { | ||
this._log(string); | ||
done(null, message); | ||
} | ||
exports.log = function(string, message, done) { | ||
this._log(string); | ||
done(null, message); | ||
}; |
@@ -6,39 +6,37 @@ var async = require('async'); | ||
* | ||
* @param options {String} TODO. | ||
* @param workflow {String} TODO. | ||
* @param message {Object} Incoming message. | ||
* @param done {Function} Callback on task completion. | ||
*/ | ||
exports.tasks = { | ||
type: 'join', | ||
fn: function(options, messages, done) { | ||
var task, | ||
requires, | ||
notify, | ||
fn, | ||
tasks = {}, | ||
self = this; | ||
var tasks = exports.tasks = function tasks(workflow, messages, done) { | ||
var item, | ||
requires, | ||
notify, | ||
fn, | ||
auto = {}, | ||
self = this; | ||
function runTask(name, taskOptions, requires, notify) { | ||
return function(callback, result) { | ||
var new_messages = requires.length ? [] : messages; | ||
result = result || []; | ||
function task(name, options, requires, notify) { | ||
return function(callback, result) { | ||
var new_messages = requires.length ? [] : messages; | ||
result = result || []; | ||
// Concat dependency messages in order of requires array | ||
requires.forEach(function(task) { | ||
new_messages = new_messages.concat(result[task]); | ||
}); | ||
// Concat dependency messages in order of requires array | ||
requires.forEach(function(item) { | ||
new_messages = new_messages.concat(result[item]); | ||
}); | ||
self._dispatch(name, taskOptions, new_messages, callback, notify); | ||
}; | ||
} | ||
self._dispatch(name, options, new_messages, callback, notify); | ||
}; | ||
} | ||
for (task in options) { | ||
requires = options[task].requires; | ||
notify = options[task].callback; | ||
fn = runTask(options[task].task, options[task].options, requires || [], notify); | ||
tasks[task] = requires ? requires.concat(fn) : fn; | ||
} | ||
for (item in workflow) { | ||
requires = workflow[item].requires; | ||
notify = workflow[item].callback; | ||
fn = task(workflow[item].task, workflow[item].options, requires || [], notify); | ||
auto[item] = requires ? requires.concat(fn) : fn; | ||
} | ||
async.auto(tasks, done); | ||
} | ||
}; | ||
async.auto(auto, done); | ||
}; | ||
tasks.type = 'join'; |
@@ -9,3 +9,3 @@ var fs = require('fs'), | ||
* | ||
* @param options {Object} Write task options. | ||
* @param options {Object} Write options or filename. | ||
* @param options.file {String} Filename to write. | ||
@@ -15,40 +15,40 @@ * @param message {Object} Incoming message. | ||
*/ | ||
exports.write = { | ||
fn: function(options, message, done) { | ||
var name = options.file, | ||
dirname = path.resolve(path.dirname(options.file)), | ||
checksum; | ||
var write = exports.write = function write(options, message, done) { | ||
options = (typeof options === 'string') ? {file: options} : options; | ||
function writeFile(name, message) { | ||
fs.writeFile(name, message.body, function(err) { | ||
done(err, { | ||
meta: { | ||
name: name | ||
}, | ||
body: message.body | ||
}); | ||
var dirname = path.resolve(path.dirname(options.file)), | ||
checksum; | ||
function writeFile(name, message) { | ||
fs.writeFile(name, message.body, function(err) { | ||
done(err, { | ||
meta: { | ||
name: name | ||
}, | ||
body: message.body | ||
}); | ||
} | ||
}); | ||
} | ||
if (name.indexOf('{checksum}') > -1) { // Replace {checksum} with md5 string | ||
checksum = Crypto.createHash('md5'); | ||
checksum.update(message.body); | ||
name = name.replace('{checksum}', checksum.digest('hex')); | ||
if (options.file.indexOf('{checksum}') > -1) { // Replace {checksum} with md5 string | ||
checksum = Crypto.createHash('md5'); | ||
checksum.update(message.body); | ||
options.file = options.file.replace('{checksum}', checksum.digest('hex')); | ||
} | ||
path.exists(dirname, function(exists) { | ||
if (!exists) { | ||
mkdirp(dirname, '0755', function(err) { | ||
if (err) { | ||
done(err); | ||
} else { | ||
writeFile(options.file, message); | ||
} | ||
}); | ||
} | ||
path.exists(dirname, function(exists) { | ||
if (!exists) { | ||
mkdirp(dirname, '0755', function(err) { | ||
if (err) { | ||
done(err); | ||
} else { | ||
writeFile(name, message); | ||
} | ||
}); | ||
} | ||
else { | ||
writeFile(name, message); | ||
} | ||
}); | ||
} | ||
}; | ||
else { | ||
writeFile(options.file, message); | ||
} | ||
}); | ||
}; | ||
write.type = 'take'; |
{ | ||
"name": "gear", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Task-based build system.", | ||
@@ -5,0 +5,0 @@ "author": "Stephen Murphy <stephen@hypernaut.com>", |
@@ -29,8 +29,20 @@ # gear | ||
gear.queue() | ||
.load([{file: 'foo.js'}, {file: 'bar.js'}, {file: 'baz.js'}] | ||
.concat() | ||
.write({file: 'foobarbaz.js'}) | ||
.load('foo.js') | ||
.log('read foo.js') | ||
.inspect() | ||
.write('foobarbaz.js') | ||
.run(); | ||
``` | ||
### Execute Tasks Using Array Style | ||
``` | ||
gear.queue() | ||
.load(['foo.js', {file: 'bar.js'}, 'baz.js']) | ||
.log('read foo.js') | ||
.inspect() | ||
.write(['newfoo.js', 'newbar.js']) // Not writing 'baz.js' | ||
.run(); | ||
``` | ||
### Complex Task Execution | ||
@@ -40,6 +52,6 @@ | ||
gear.queue() | ||
.load([{file: 'foo.js'}]) | ||
.load('foo.js') | ||
.log('Complex Task') | ||
.tasks({ | ||
read: {task: 'load', options: [{file: 'foo.js'}, {file: 'bar.js'}, {file: 'baz.js'}]} | ||
read: {task: 'load', options: ['foo.js', 'bar.js', 'baz.js'], callback: function(err, results) {}} | ||
combine: {task: 'concat', requires: ['read']} | ||
@@ -178,4 +190,5 @@ minify: {task: 'jsminify', requires: ['combine']} | ||
``` | ||
// source - What resource to load. | ||
// source - Filename or object to load. | ||
// source.file - Filename of resource. | ||
.load(['foo', 'baz']) | ||
.load([{file: 'foo'}, {file: 'bar'}, {file: 'baz'}]) | ||
@@ -182,0 +195,0 @@ ``` |
var should = require('should'), | ||
concat = require('../lib/tasks/concat').concat.fn, | ||
concat = require('../lib/tasks/concat').concat, | ||
fixtures = { | ||
@@ -4,0 +4,0 @@ messages: [{body: 'abc'}, {body: 'def'}], |
var should = require('should'), | ||
load = require('../lib/tasks/load').load.fn, | ||
load = require('../lib/tasks/load').load, | ||
fixtures = { | ||
filename: 'test/fixtures/test1.js', | ||
file: {file: 'test/fixtures/test1.js'}, | ||
@@ -9,2 +10,8 @@ missing_file: {file: 'test/fixtures/missing_file.js'} | ||
describe('load()', function() { | ||
it('should handle filenames instead of objects', function(done) { | ||
load(fixtures.filename, [], function(err, message) { | ||
done(err); | ||
}); | ||
}); | ||
it('should read files', function(done) { | ||
@@ -11,0 +18,0 @@ load(fixtures.file, [], function(err, message) { |
@@ -5,4 +5,5 @@ var should = require('should'), | ||
fixtures = { | ||
file: {file: 'test/fixtures/test1.js'}, | ||
files: [{file: 'test/fixtures/test1.js'}, {file: 'test/fixtures/test2.js'}], | ||
missing_files: [{file: 'test/fixtures/missing_file.js'}] | ||
missing_file: {file: 'test/fixtures/missing_file.js'} | ||
}; | ||
@@ -12,8 +13,16 @@ | ||
describe('run()', function() { | ||
it('should execute chained tasks', function(done) { | ||
it('should handle append tasks', function(done) { | ||
gear.queue() | ||
.load(fixtures.files) | ||
.load(fixtures.file) | ||
.run(function(err, results) { | ||
results.should.have.length(3); | ||
done(err); | ||
}); | ||
}); | ||
it('should handle tasks called with array options', function(done) { | ||
gear.queue() | ||
.load(fixtures.files) | ||
.concat() | ||
.inspect() | ||
.log('Finished') | ||
.run(function(err, results) { | ||
@@ -24,7 +33,15 @@ done(err); | ||
it('should execute chained tasks', function(done) { | ||
gear.queue() | ||
.load(fixtures.file) | ||
.concat() | ||
.run(function(err, results) { | ||
done(err); | ||
}); | ||
}); | ||
it('should handle errors', function(done) { | ||
gear.queue() | ||
.load(fixtures.missing_files) | ||
.load(fixtures.missing_file) | ||
.concat() | ||
.inspect() | ||
.run(function(err, results) { | ||
@@ -31,0 +48,0 @@ should.exist(err); |
var should = require('should'), | ||
path = require('path'), | ||
gear = require(path.join(process.cwd(), './index')), | ||
tasks = require('../lib/tasks/tasks').tasks.fn, | ||
tasks = require('../lib/tasks/tasks').tasks, | ||
fixtures = { | ||
@@ -6,0 +6,0 @@ files: [{file: 'test/fixtures/test1.js'}], |
var path = require('path'), | ||
write = require('../lib/tasks/write').write.fn, | ||
fs = require('fs'), | ||
write = require('../lib/tasks/write').write, | ||
fixtures = { | ||
filename: 'testing/write.txt', | ||
filename2: 'testing/write2.txt', | ||
checksum: 'testing/write_{checksum}.txt', | ||
checksum_replaced: 'testing/write_900150983cd24fb0d6963f7d28e17f72.txt', | ||
message: {body: 'abc'} | ||
}; | ||
function remove(filename) { | ||
if (path.existsSync(filename)) { | ||
fs.unlinkSync(filename); | ||
} | ||
} | ||
describe('write()', function() { | ||
it('should write last message', function(done) { | ||
write({file: fixtures.filename}, fixtures.message, function(err, message) { | ||
path.existsSync(fixtures.filename); | ||
it('should write file', function(done) { | ||
remove(fixtures.filename); | ||
write(fixtures.filename, fixtures.message, function(err, message) { | ||
path.existsSync(fixtures.filename).should.equal(true); | ||
done(err); | ||
@@ -16,3 +27,17 @@ }); | ||
it('should replace {checksum} in filename'); | ||
it('should write file with options', function(done) { | ||
remove(fixtures.filename); | ||
write({file: fixtures.filename2}, fixtures.message, function(err, message) { | ||
path.existsSync(fixtures.filename2).should.equal(true); | ||
done(err); | ||
}); | ||
}); | ||
it('should replace {checksum} in filename', function(done) { | ||
remove(fixtures.checksum_replaced); | ||
write({file: fixtures.checksum}, fixtures.message, function(err, message) { | ||
path.existsSync(fixtures.checksum_replaced).should.equal(true); | ||
done(err); | ||
}); | ||
}); | ||
}); |
25083
24
521
328
8