UNPKG

gulp-task

Version:

A wrapper of gulp.task that enables promise-based dependency management

182 lines (156 loc) 5.35 kB
(function() { var Promise, executeTask, foreach, gulp, humanizeTime, isPromise, isStream, next, streamToPromise, tag, _gulp, _task, _tasks; Promise = require('bluebird'); foreach = require('gulp-foreach'); gulp = require('gulp'); next = require('gulp-next'); require('colors'); _tasks = {}; _gulp = null; isPromise = function(input) { return input && typeof input.then === 'function'; }; isStream = function(input) { return input && typeof input.pipe === 'function'; }; humanizeTime = function(timeArray) { var f, limit, m, ndx, numDecimals, s, suffix, time; f = function(n) { return Math.floor(n) % 1000; }; s = timeArray[0], m = timeArray[1]; suffix = [' s', ' ms', ' μs', ' ns']; time = s + (m / 1000000000); limit = 1; ndx = 0; while (time < limit) { time *= 1000; ndx++; } numDecimals = Math.max(0, 4 - (parseInt(time) + '').length); return time.toFixed(numDecimals) + suffix[ndx]; }; streamToPromise = function(stream) { return new Promise(function(resolve) { return stream.pipe(next(function() { return resolve(stream); })); }); }; _task = function(name, cb) { var end, newRegStack, oldRegStack; if (_tasks[name] != null) { end = _task.stackLength + 1; newRegStack = new Error().stack.split('\n').slice(2, +end + 1 || 9e9).join('\n'); oldRegStack = _tasks[name].registrationStack.split('\n').slice(2, +end + 1 || 9e9).join('\n'); console.error("The 1st registration was at:\n".yellow.bold + oldRegStack.green); console.error("The 2nd registration was at:\n".yellow.bold + newRegStack.green); console.error("For longer stack traces, add 'task.stackLength = <number>' before declaring tasks.".gray); console.error("Error: Task ".red + name.green + " has been declared twice.".red); process.exit(1); } _tasks[name] = { callback: cb, registrationStack: new Error().stack }; if (_gulp != null) { return _gulp.task(name, function() { return _task.run(name).then(_task.gulpTaskCallback); }); } }; executeTask = function(task) { var promise, returnVal; returnVal = task.apply(null); if (isPromise(returnVal)) { promise = returnVal; } else if (isStream(returnVal)) { promise = streamToPromise(returnVal); } else { promise = Promise.resolve(returnVal); } return promise; }; tag = "[" + "task".yellow + "]"; _task.run = function(name) { var end, startTime, task, _ref; if (typeof name === "string") { task = (_ref = _tasks[name]) != null ? _ref.callback : void 0; if (task == null) { throw new Error("Task Not Found: '" + name + "'"); } } else if (typeof name === "function") { task = name; name = null; } else { console.error('Unexpected parameter'.red); end = _task.stackLength + 1; console.error(new Error().stack.split('\n').slice(2, +end + 1 || 9e9).join('\n').green); throw new Error('task.run expects either the name of a task registered with task or an anonymous function'); } startTime = process.hrtime(); if (name) { console.log("" + tag + " Running '" + name.green.bold + "'"); } return executeTask(task).tap(function() { var timeDiff, timeTaken; if (name) { timeDiff = process.hrtime(startTime); timeTaken = humanizeTime(timeDiff); return console.log(("" + tag + " Finished '" + name.magenta.bold + "' in ") + timeTaken.green.bold); } })["catch"](function(error) { if (name) { if (error.reported) { console.log(("" + tag + " ") + "Failed to complete '".red + name.red.bold + "'".red); } else { console.log(("" + tag + " ") + "Failed to complete '".red + name.red.bold + ("': " + error.message).red); console.log(error.stack); error.reported = true; } } return Promise.reject(error); }); }; _task.configure = function(gulp) { var name, task; if (_gulp == null) { for (name in _tasks) { task = _tasks[name]; _gulp.task(name, function() { return _task.run(name).then(gulpTaskCallback); }); } } return _gulp = gulp; }; _task.watch = function(glob, options, cb) { if (options == null) { options = {}; } if (typeof options === 'function') { cb = options; options = {}; } gulp.watch(glob, options, function(event) { cb(gulp.src(event.path), event.path, event); return null; }); return { pipe: function() { throw new Error('task.watch no longer returns a stream. Functionality has moved from the gulp-watch package to the gulp.watch function in order to avoid segmentation faults for large projects. If your project is small and you wish the old functionality, lock your version number down to 1.0.0'); } }; }; _task.watchedSrc = _task.watch; _task.getTaskNames = function() { return Object.keys(_tasks).sort(); }; _task.stackLength = 1; _task.toString = function() { return 'gulp-task'; }; _task.gulpTaskCallback = function() {}; _task.promisifyStream = streamToPromise; module.exports = _task; }).call(this);