From d5c1e03517c2c8790004f81a51664447c49d06e1 Mon Sep 17 00:00:00 2001 From: tkqubo Date: Mon, 28 Sep 2015 22:53:23 +0900 Subject: [PATCH] Add undertaker --- undertaker/undertaker-tests.ts | 46 +++++++++++++ undertaker/undertaker.d.ts | 117 +++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 undertaker/undertaker-tests.ts create mode 100644 undertaker/undertaker.d.ts diff --git a/undertaker/undertaker-tests.ts b/undertaker/undertaker-tests.ts new file mode 100644 index 000000000..b40e19af1 --- /dev/null +++ b/undertaker/undertaker-tests.ts @@ -0,0 +1,46 @@ +/// +/// +/// + +var fs = require('fs'); +var Undertaker = require('undertaker'); +import { Registry } from 'undertaker'; +require('es6-promise'); + +var taker = new Undertaker(); + +taker.task('task1', function(cb: () => void){ + // do things + + cb(); // when everything is done +}); + +taker.task('task2', function(){ + return fs.createReadStream('./myFile.js') + .pipe(fs.createWriteStream('./myFile.copy.js')); +}); + +taker.task('task3', function(){ + return new Promise(function(resolve, reject){ + // do things + + resolve(); // when everything is done + }); +}); + +taker.task('combined', taker.series('task1', 'task2')); + +taker.task('all', taker.parallel('combined', 'task3')); + +var registry: Registry; +function CommonRegistry(options: { buildDir: string }): Registry { + return registry; +} + +var taker = new Undertaker(CommonRegistry({ buildDir: '/dist' })); + +taker.task('build', taker.series('clean', function build(cb: () => void) { + // do things + cb(); +})); + diff --git a/undertaker/undertaker.d.ts b/undertaker/undertaker.d.ts new file mode 100644 index 000000000..b31b6646d --- /dev/null +++ b/undertaker/undertaker.d.ts @@ -0,0 +1,117 @@ +// Type definitions for undertaker 0.12.0 +// Project: https://github.com/phated/undertaker +// Definitions by: Qubo +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "undertaker" { + + export interface UndertakerStatic { + new(registry?: Registry): Undertaker; + } + + export interface Undertaker { + task: TaskMethod; + /** + * Takes a variable amount of strings (taskName) and/or functions (fn) + * and returns a function of the composed tasks or functions. + * Any taskNames are retrieved from the registry using the get method. + * + * When the returned function is executed, the tasks or functions will be executed in series, + * each waiting for the prior to finish. If an error occurs, execution will stop. + * @param task + */ + series(...tasks: (string|Task)[]): Task; + /** + * Takes a variable amount of strings (taskName) and/or functions (fn) + * and returns a function of the composed tasks or functions. + * Any taskNames are retrieved from the registry using the get method. + * + * When the returned function is executed, the tasks or functions will be executed in parallel, + * all being executed at the same time. If an error occurs, all execution will complete. + * @param tasks + */ + parallel(...tasks: (string|Task)[]): Task; + /** + * Returns the current registry object. + */ + registry(): Registry; + /** + * The tasks from the current registry will be transferred to it + * and the current registry will be replaced with the new registry. + * @param registry + */ + registry(registry: Registry): void; + /** + * Optionally takes an object (options) and returns an object representing the tree of registered tasks. + * @param options + */ + tree(options?: { deep?: boolean }): Node[]|string[]; + /** + * Takes a string or function (task) and returns a timestamp of the last time the task was run successfully. + * The time will be the time the task started. Returns undefined if the task has not been run. + * @param task + * @param timeResolution + */ + lastRun(task: string, timeResolution?: number): number; + } + + export interface Task { + (cb?: Function): any; + } + + export interface TaskMethod { + /** + * Returns the registered function. + * @param taskName + */ + (taskName: string): Task; + /** + * Register the task by the taskName. + * @param taskName + * @param fn + */ + (taskName: string, fn: Task): void; + /** + * Register the task by the name property of the function. + * @param fn + */ + (fn: Task): void; + /** + * Register the task by the displayName property of the function. + * @param fn + */ + (fn: Task & { displayName: string }): void; + } + + export interface Registry { + /** + * receives the undertaker instance to set pre-defined tasks using the task(taskName, fn) method. + * @param taker + */ + init(taker: Undertaker): void; + /** + * returns the task with that name or undefined if no task is registered with that name. + * @param taskName + */ + get(taskName: string): Task; + /** + * add task to the registry. If set modifies a task, it should return the new task. + * @param taskName + * @param fn + */ + set(taskName: string, fn: Task): void; + /** + * returns an object listing all tasks in the registry. + */ + tasks(): { [taskName: string]: Task }; + } + + export interface Node { + label: string; + type: string; + nodes: Node[]; + } + + export default UndertakerStatic; +} +