tunnckoCore / blankr

:heart: tasks, todos, ideas, streaks, WIP, etc
https://i.am.charlike.online
4 stars 1 forks source link

Erasm - task runner with dependencies, like gulp3 #29

Closed tunnckoCore closed 9 years ago

tunnckoCore commented 9 years ago
var auto = require('run-auto');

module.exports = Erasm;

function Erasm(tasks) {
  if (!(this instanceof Erasm)) {
    return new Erasm(tasks);
  }
  if (Array.isArray(tasks)) {
    tasks = Object.create(null);
  }
  if (typeof tasks !== 'object') {
    tasks = Object.create(null);
  }
  this.tasks = tasks;
};

Erasm.prototype.task = function _task(name, deps, fn) {
  if (!name && !deps) {
    throw new Error('task should have at least two arguments');
  }
  if (typeof deps === 'function') {
    this.tasks[name] = deps;
    return this;
  }
  if (!Array.isArray(deps)) {
    throw new TypeError('task 2nd argument should be array or function');
  }
  this.tasks[name] = deps.concat(fn);
  return this;
}

Erasm.prototype.run = function _run(done) {
  done = typeof done === 'function' ? done : function _done(err, res) {
    console.log('internal handling `done`');
  }
  auto(this.tasks, done);
  return this;
}
tunnckoCore commented 9 years ago

tests

var erasm = require('./erasm.js');
var test = require('tape');

test('functions that return errors', function (t) {
  t.plan(2)

  var tasks = {
    a: function (cb) {
      t.pass('cb 1')
      cb(new Error('oops'))
    },
    b: ['a', function (cb) {
      t.fail('cb 2 should not get called')
    }]
  }

  erasm(tasks)
  .run(function (err) {
    t.ok(err instanceof Error, 'should be true')
  })
});

test('empty tasks object', function (t) {
  t.plan(2)

  erasm({})
  .run(function (err) {
    t.equals(err, undefined)
    t.pass('did not throw')
    t.end()
  })
});

test('tasks property count should be zero', function (t) {
  t.plan(1)

  t.deepEqual(erasm({}).tasks, {})
  t.end()
});

test('empty tasks object and no callback', function (t) {
  erasm([])
  t.pass('did not throw')
  t.end()
})

test('auto error should pass partial results', function (t) {
  t.plan(3)
  var tasks = {
    task1: function (cb) {
      cb(false, 'result1')
    },
    task2: ['task1', function (cb) {
      cb(new Error('testerror'), 'result2')
    }],
    task3: ['task2', function (cb) {
      t.fail('task3 should not be called')
    }]
  }

  erasm(tasks)
  .run(function (err, results) {
    t.ok(err instanceof Error, 'should be true')
    t.equals(results.task1, 'result1')
    t.equals(results.task2, 'result2')
    t.end()
  })
});

test('should have .task method', function(t) {
  t.plan(3)

  erasm()
  .task('task1', function(cb) {
    cb(false, 'result1')
  })
  .task('task2', ['task1'], function(cb) {
    cb(new Error('testerror'), 'result2')
  })
  .task('task3', ['task2'], function(cb) {
    t.fail('task3 should not be called')
  })
  .run(function(err, results) {
    t.ok(err instanceof Error, 'should be true')
    t.equals(results.task1, 'result1')
    t.equals(results.task2, 'result2')
    t.end()
  })
})

tests 2

var callOrder = [];
test('functions run in parallel', function (t) {
  erasm()
  .task('task1', function (cb) {
    callOrder.push('task1')
    cb()
  })
  .task('task2', function (cb) {
    callOrder.push('task2')
    cb()
  })
  .task('task3', function (cb) {
    setTimeout(function () {
      callOrder.push('task3')
      cb()
    }, 50)
  })
  .task('task4', ['task3'], function (cb) {
    setTimeout(function () {
      callOrder.push('task4')
      cb()
    }, 30)
  })
  .task('task5', ['task4'], function (cb) {
    setTimeout(function () {
      callOrder.push('task5')
      cb()
    }, 150)
  })
  .task('task6', function (cb) {
    callOrder.push('task6')
    cb()
  })
  .run(function (err) {
    t.deepEqual(callOrder, ['task1','task2','task6','task3','task4','task5'])
    t.end()
  })
});
tunnckoCore commented 9 years ago

closing, follow https://github.com/tunnckoCore/ideas/issues/11