UNPKG

antsjs

Version:

Task Runner

61 lines (47 loc) 1.5 kB
class Task{ constructor(public name: string, fun: Function){ this.fun = fun } logs = Array<string>() log(str: string, print = false){ str = '[' + this.name + '] - ' + Date.now() + ': ' + str this.logs.push(str) if(print) console.log(str) } before(...args){ //... } fun: Function = null after(...args){ //... } } const ant = { tasks: Array<Task>(), add(id: string, fun: Function): Task{ const _task = new Task(id, fun) ant.tasks.push(_task) _task.log('Task [' + id + ']' + ' will created') return _task }, remove(id: string): Array<Task>{ return ant.tasks.splice(ant.tasks.findIndex(x=> x.name == id), 1) }, run(id: string, ...args): any{ const _task = ant.find(id) if(!_task) throw new Error('Undefined Task - ' + id) args.push(_task) _task.log('Task [' + id + ']' + ' [before] calling...') const _args = _task.before.apply(this, args) if(Array.isArray(_args)) args = _args _task.log('Task [' + id + ']' + ' calling...') const _result = _task.fun.apply(this, args) _task.log('Task [' + id + ']' + ' [after] calling...') _task.after.apply(this, args) return _result; }, find(id: string): Task{ return ant.tasks.find(x=> x.name == id) } } module.exports = ant