@juuxstar/gulp-guzzle
Advanced tools
Comparing version
124
guzzle.js
@@ -10,2 +10,3 @@ /** | ||
const _ = require('lodash'); | ||
const clc = require('cli-color'); | ||
@@ -15,2 +16,9 @@ const guzzleTasks = []; | ||
const State = { | ||
NOT_STARTED: 'not_started', | ||
STARTED : 'started', | ||
DONE : 'done', | ||
ERROR : 'error' | ||
}; | ||
const oldStart = gulp.start; | ||
@@ -32,15 +40,14 @@ gulp.start = function() { | ||
* (see https://github.com/mdaines/viz.js/wiki/API for available extensions/formats) | ||
* @param {Function} [options.onFinish] - is called everytime there are no more tasks left to run (can occur multiple times due to watch) | ||
* @param {Function} [options.onFinish] - is called every time there are no more tasks left to run (can occur multiple times due to watch) | ||
* @param {Boolean} [options.prettyPrint] if true, uses a "different" display of tasks (experimental) | ||
*/ | ||
const guzzle = module.exports = function(plugins, options) { | ||
options = options || {}; | ||
if (options.taskGraph) { | ||
graphFilename = options.taskGraph; | ||
const guzzle = module.exports = function(plugins, { taskGraph, onFinish, prettyPrint = false } = {}) { | ||
if (taskGraph) { | ||
graphFilename = taskGraph; | ||
} | ||
if (options.onFinish) { | ||
if (onFinish) { | ||
gulp.on('task_stop', _.debounce(() => { | ||
if (!_.some(gulp.tasks, t => t.running)) { | ||
options.onFinish(); | ||
onFinish(); | ||
} | ||
@@ -50,2 +57,22 @@ }, 100)); | ||
gulp.on('task_start', event => { | ||
const task = findGuzzleTask(event.task); | ||
task._state = State.STARTED; | ||
task._started = new Date(); | ||
if (prettyPrint) { printTasks(); } | ||
}); | ||
gulp.on('task_stop', event => { | ||
const task = findGuzzleTask(event.task); | ||
if (task._state === State.DONE) return; | ||
task._state = State.DONE; | ||
task._ended = new Date(); | ||
if (prettyPrint) { printTasks(); } | ||
}); | ||
gulp.on('task_err', event => { | ||
findGuzzleTask(event.task)._state = State.ERROR; | ||
if (prettyPrint) { printTasks(); } | ||
}); | ||
for (const name in plugins) { | ||
@@ -63,16 +90,20 @@ GuzzleTask.prototype[name] = makeGuzzlePlugin(plugins[name]); | ||
guzzle.task = function(name, depends, fn) { | ||
// check if called without any dependencies | ||
if (arguments.length === 2 && typeof depends == 'function') { | ||
fn = depends; | ||
depends = []; | ||
} | ||
/** | ||
* Defines a guzzle task. | ||
* @param {String} name | ||
* @param {String[]} [depends] array of dependencies for this task | ||
* @param {Function} [doneCallback] | ||
*/ | ||
guzzle.task = function() { | ||
const { name, depends, doneCallback } = parseTaskArguments(...arguments) | ||
return new GuzzleTask(name, depends, doneCallback); | ||
}; | ||
if (!Array.isArray(depends)) { | ||
depends = [ depends ]; | ||
} | ||
const guzzleTask = new GuzzleTask(name, _.compact(depends), fn); | ||
guzzleTasks.push(guzzleTask); | ||
return guzzleTask; | ||
/** | ||
* A variant of guzzle.task that only runs once. | ||
* @see guzzle.task for params | ||
*/ | ||
guzzle.taskOnce = function() { | ||
const { name, depends, doneCallback } = parseTaskArguments(...arguments); | ||
return new GuzzleTask(name, depends, doneCallback, { runOnce : true }); | ||
}; | ||
@@ -84,5 +115,8 @@ | ||
class GuzzleTask { | ||
constructor(name, dependencies, onDoneFunction) { | ||
constructor(name, dependencies, onDoneFunction, { runOnce = false }={}) { | ||
this._name = name; | ||
this._stream = null; | ||
this._started = null; | ||
this._state = State.NOT_STARTED; | ||
this._runOnce = runOnce; | ||
this._dependencies = dependencies.map(dependantTask => { | ||
@@ -97,4 +131,10 @@ // for any task dependencies not specified by name, assume they're sub-tasks and prepend parent task name | ||
guzzleTasks.push(this); | ||
if (onDoneFunction) { | ||
this._onDoneFunction = callback => { | ||
if (this._runOnce && this._state !== State.NOT_STARTED) { | ||
return callback(); | ||
} | ||
const result = onDoneFunction.call(this, callback); | ||
@@ -196,1 +236,41 @@ | ||
function findGuzzleTask(taskName) { | ||
return guzzleTasks.find(task => task._name === taskName); | ||
} | ||
function printTasks() { | ||
const now = new Date(); | ||
const stateSymbols = { | ||
[State.NOT_STARTED]: '-', | ||
[State.STARTED] : clc.yellow('?'), | ||
[State.DONE] : clc.green.bold('✓'), | ||
[State.ERROR] : clc.redBright('X') | ||
} | ||
// clear the screen and print out updated list of tasks | ||
process.stdout.write( | ||
'\033c\n' // clears terminal | ||
+ guzzleTasks.map(task => { | ||
let elapsed = task._started ? (task._ended || now) - task._started : 0; | ||
elapsed = elapsed > 1000 ? (Math.round(elapsed / 100) / 10) + 's' : elapsed + 'ms'; | ||
return `${stateSymbols[task._state]} ${task._name} ${elapsed ? clc.blackBright(`(${elapsed})`) : ''}`; | ||
}).join('\n') | ||
+ '\n---\n' | ||
); | ||
} | ||
function parseTaskArguments(name, depends, doneCallback) { | ||
// check if called without any dependencies | ||
if (arguments.length === 2 && typeof depends === 'function') { | ||
doneCallback = depends; | ||
depends = []; | ||
} | ||
if (!Array.isArray(depends)) { | ||
depends = [ depends ]; | ||
} | ||
depends = _.compact(depends); | ||
return { name, depends, doneCallback }; | ||
} |
{ | ||
"name" : "@juuxstar/gulp-guzzle", | ||
"version": "1.0.0", | ||
"main" : "guzzle.js", | ||
"name": "@juuxstar/gulp-guzzle", | ||
"version": "1.0.4", | ||
"main": "guzzle.js", | ||
"dependencies": { | ||
"viz.js": "^2.1.2", | ||
"lodash": "^4.17.11", | ||
"gulp" : "^3.9.1" | ||
"gulp": "^3.9.1", | ||
"clc" : "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"mocha" : "^6.2.0", | ||
"chai" : "^4.2.0" | ||
} | ||
} | ||
} |
9056
42.46%5
25%232
49.68%4
33.33%2
Infinity%+ Added
+ Added
+ Added