Comparing version 1.0.0 to 1.1.0
235
index.js
/** | ||
* @author Stanislav Kalashnik <darkpark.main@gmail.com> | ||
* @license GNU GENERAL PUBLIC LICENSE Version 3 | ||
* @license The MIT License (MIT) | ||
* @copyright Stanislav Kalashnik <darkpark.main@gmail.com> | ||
*/ | ||
@@ -8,3 +8,232 @@ | ||
var Emitter = require('cjs-emitter'), | ||
parallel = require('cjs-async/parallel'), | ||
serial = require('cjs-async/serial'); | ||
/** | ||
* | ||
* | ||
* @constructor | ||
* @extends Emitter | ||
*/ | ||
function Runner () { | ||
console.assert(typeof this === 'object', 'must be constructed via new'); | ||
// parent constructor call | ||
Emitter.call(this); | ||
this.tasks = {}; | ||
this.tree = {}; | ||
//this.running = {}; | ||
} | ||
// inheritance | ||
Runner.prototype = Object.create(Emitter.prototype); | ||
Runner.prototype.constructor = Runner; | ||
/** | ||
* Remove all attributes from the model. | ||
* | ||
* @param {string} id task name | ||
* @param {function} body task method | ||
* | ||
* @return {function} task | ||
* | ||
* @fires Runner#clear | ||
*/ | ||
Runner.prototype.task = function ( id, body ) { | ||
if ( id && typeof id === 'string' && body && typeof body === 'function' ) { | ||
this.tasks[id] = body; | ||
body.id = id; | ||
//console.log(body); | ||
} | ||
return body; | ||
}; | ||
//function done ( instance, fn ) { | ||
// // mark finished | ||
// fn.running = false; | ||
// | ||
// // there are some listeners | ||
// if ( instance.events['finish'] ) { | ||
// // notify listeners | ||
// instance.emit('finish', {id: fn.id}); | ||
// //console.log('finish', fn.id); | ||
// } | ||
//} | ||
// | ||
//function wrap ( instance, fn ) { | ||
// // is there a callback for task? | ||
// if ( fn.length === 0 ) { | ||
// // start task sync | ||
// fn(); | ||
// // finish | ||
// done(); | ||
// } else { | ||
// // start task async | ||
// fn(done); | ||
// } | ||
//} | ||
Runner.prototype.wrap = function ( task ) { | ||
var self = this, | ||
time; | ||
return function ( cb ) { | ||
var done = function () { | ||
// mark finished | ||
task.running = false; | ||
time = Date.now() - time; | ||
/*console.log('finish', task.id, time);/**/ | ||
// there are some listeners | ||
if ( self.events['finish'] ) { | ||
// notify listeners | ||
self.emit('finish', {id: task.id, time: time}); | ||
} | ||
// callback from user or | ||
if ( cb && typeof cb === 'function' ) { | ||
cb.apply(null, arguments); | ||
} | ||
}, | ||
result; | ||
// exist and not already executing | ||
if ( task && !task.running ) { | ||
// mark to prevent multiple starts | ||
task.running = true; | ||
time = Date.now(); | ||
/*console.log('start', task.id);/**/ | ||
// there are some listeners | ||
if ( self.events['start'] ) { | ||
// notify listeners | ||
self.emit('start', {id: task.id}); | ||
} | ||
// is there a callback for task? | ||
if ( task.length === 0 ) { | ||
// start task sync | ||
result = task(); | ||
// finish | ||
done(); | ||
} else { | ||
//console.log(task.id); | ||
// start task async | ||
result = task(done); | ||
} | ||
} | ||
return result; | ||
}; | ||
}; | ||
Runner.prototype.parallel = function () { | ||
var self = this, | ||
tasks = Array.prototype.slice.call(arguments), | ||
func; | ||
if ( tasks.length === 0 ) { | ||
return null; | ||
} | ||
func = function ( done ) { | ||
var readyTasks = []; | ||
// wrap not running tasks only | ||
tasks.forEach(function ( task ) { | ||
task = self.tasks[task] || task; | ||
if ( task && !task.running ) { | ||
readyTasks.push( | ||
function ( done ) { | ||
return self.run(task, done); | ||
} | ||
); | ||
} | ||
}); | ||
parallel(readyTasks, done); | ||
}; | ||
func.group = true; | ||
func.type = 'parallel'; | ||
func.children = tasks; | ||
return func; | ||
}; | ||
Runner.prototype.serial = function () { | ||
var self = this, | ||
tasks = Array.prototype.slice.call(arguments), | ||
func; | ||
if ( tasks.length === 0 ) { | ||
return null; | ||
} | ||
func = function ( done ) { | ||
var readyTasks = []; | ||
// wrap not running tasks only | ||
tasks.forEach(function ( task ) { | ||
task = self.tasks[task] || task; | ||
if ( task && !task.running ) { | ||
readyTasks.push( | ||
function ( done ) { | ||
return self.run(task, done); | ||
} | ||
); | ||
} | ||
}); | ||
serial(readyTasks, done); | ||
}; | ||
func.group = true; | ||
func.type = 'serial'; | ||
func.children = tasks; | ||
return func; | ||
}; | ||
/** | ||
* Start task execution. | ||
* | ||
* @param {function|string} task task to run | ||
* @param {function} [done] callback on task finish | ||
* | ||
* @return {*} task execution result | ||
*/ | ||
Runner.prototype.run = function ( task, done ) { | ||
var result; | ||
task = this.tasks[task] || task; | ||
if ( typeof task === 'function' && !task.running ) { | ||
result = this.wrap(task)(done); | ||
} | ||
return result; | ||
}; | ||
Runner.prototype.start = function ( done ) { | ||
this.run(this.tasks.default, done); | ||
}; | ||
// public | ||
module.exports = {}; | ||
module.exports = Runner; |
{ | ||
"name": "cjs-runner", | ||
"version": "1.0.0", | ||
"description": "Task runner.", | ||
"version": "1.1.0", | ||
"description": "Simple task runner.", | ||
"author": { | ||
@@ -9,8 +9,22 @@ "name": "Stanislav Kalashnik", | ||
}, | ||
"repository": "cjssdk/runner", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/cjssdk/runner.git" | ||
}, | ||
"scripts": { | ||
"lint": "eslint ./*.js", | ||
"test": "node ./tests/main.js", | ||
"lint": "eslint .", | ||
"mocha": "node ./tests/main.js", | ||
"test": "npm run mocha && npm run lint", | ||
"jsdoc": "jsdoc --destination doc *.js readme.md" | ||
}, | ||
"dependencies": { | ||
"cjs-async": "1.*.*", | ||
"cjs-emitter": "1.*.*" | ||
}, | ||
"devDependencies": { | ||
"cjs-eslint-config": "1.*.*", | ||
"eslint": "3.*.*", | ||
"mocha": "3.*.*", | ||
"should": "11.*.*" | ||
}, | ||
"keywords": [ | ||
@@ -24,3 +38,3 @@ "commonjs", | ||
], | ||
"license": "GPL-3.0" | ||
"license": "MIT" | ||
} |
Task runner | ||
=========== | ||
[![Build Status](https://img.shields.io/travis/cjssdk/runner.svg?style=flat-square)](https://travis-ci.org/cjssdk/runner) | ||
[![NPM version](https://img.shields.io/npm/v/cjs-runner.svg?style=flat-square)](https://www.npmjs.com/package/cjs-runner) | ||
[![Dependencies Status](https://img.shields.io/david/cjssdk/runner.svg?style=flat-square)](https://david-dm.org/cjssdk/runner) | ||
[![build status](https://img.shields.io/travis/cjssdk/runner.svg?style=flat-square)](https://travis-ci.org/cjssdk/runner) | ||
[![npm version](https://img.shields.io/npm/v/cjs-runner.svg?style=flat-square)](https://www.npmjs.com/package/cjs-runner) | ||
[![dependencies status](https://img.shields.io/david/cjssdk/runner.svg?style=flat-square)](https://david-dm.org/cjssdk/runner) | ||
[![devDependencies status](https://img.shields.io/david/dev/cjssdk/runner.svg?style=flat-square)](https://david-dm.org/cjssdk/runner?type=dev) | ||
[![Gitter](https://img.shields.io/badge/gitter-join%20chat-blue.svg?style=flat-square)](https://gitter.im/DarkPark/cjssdk) | ||
[![RunKit](https://img.shields.io/badge/RunKit-try-yellow.svg?style=flat-square)](https://runkit.com/npm/cjs-runner) | ||
@@ -25,6 +27,26 @@ | ||
```js | ||
var runner = require('cjs-runner'); | ||
var Runner = require('cjs-runner'), | ||
runner = new Runner(); | ||
``` | ||
Create a simple task: | ||
```js | ||
runner.task('make', function () { | ||
// some actions | ||
}); | ||
``` | ||
```js | ||
t1 = runner.task('static', runner.serial('jade:build', 'sass:build')); | ||
t1 = runner.task({ | ||
name: 'static', | ||
dependency: ['jade:build', 'sass:build'] | ||
}); | ||
runner.start(['build']); | ||
``` | ||
## Contribution ## | ||
@@ -38,2 +60,2 @@ | ||
`cjs-runner` is released under the [GPL-3.0 License](http://opensource.org/licenses/GPL-3.0). | ||
`cjs-runner` is released under the [MIT License](license.md). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
8920
5
0
100
188
60
0
2
4
+ Addedcjs-async@1.*.*
+ Addedcjs-emitter@1.*.*
+ Addedcjs-async@1.6.0(transitive)
+ Addedcjs-emitter@1.5.3(transitive)