123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- let { uuid } = require('./utils');
- let api = new (function () {
-
- this.task = function (name, prereqs, action, opts) {
- let args = Array.prototype.slice.call(arguments);
- let createdTask;
- args.unshift('task');
- createdTask = jake.createTask.apply(global, args);
- jake.currentTaskDescription = null;
- return createdTask;
- };
-
- this.rule = function () {
- let args = Array.prototype.slice.call(arguments);
- let arg;
- let pattern = args.shift();
- let source = args.shift();
- let prereqs = [];
- let action = function () {};
- let opts = {};
- let key = pattern.toString();
- while ((arg = args.shift())) {
- if (typeof arg == 'function') {
- action = arg;
- }
- else if (Array.isArray(arg)) {
- prereqs = arg;
- }
- else {
- opts = arg;
- }
- }
- jake.currentNamespace.rules[key] = new jake.Rule({
- pattern: pattern,
- source: source,
- prereqs: prereqs,
- action: action,
- opts: opts,
- desc: jake.currentTaskDescription,
- ns: jake.currentNamespace
- });
- jake.currentTaskDescription = null;
- };
-
- this.directory = function (name) {
- let args = Array.prototype.slice.call(arguments);
- let createdTask;
- args.unshift('directory');
- createdTask = jake.createTask.apply(global, args);
- jake.currentTaskDescription = null;
- return createdTask;
- };
-
- this.file = function (name, prereqs, action, opts) {
- let args = Array.prototype.slice.call(arguments);
- let createdTask;
- args.unshift('file');
- createdTask = jake.createTask.apply(global, args);
- jake.currentTaskDescription = null;
- return createdTask;
- };
-
- this.desc = function (description) {
- jake.currentTaskDescription = description;
- };
-
- this.namespace = function (name, closure) {
- let curr = jake.currentNamespace;
- let ns = curr.childNamespaces[name] || new jake.Namespace(name, curr);
- let fn = closure || function () {};
- curr.childNamespaces[name] = ns;
- jake.currentNamespace = ns;
- fn();
- jake.currentNamespace = curr;
- jake.currentTaskDescription = null;
- return ns;
- };
-
- this.complete = function (task, val) {
-
- if(task && task. _currentPrereqIndex >=0 ) {
- task.complete(val);
- }
- else {
- val = task;
- if(jake._invocationChain.length > 0) {
- jake._invocationChain[jake._invocationChain.length-1].complete(val);
- }
- }
- };
-
- this.fail = function (err, code) {
- let msg;
- let errObj;
- if (code) {
- jake.errorCode = code;
- }
- if (err) {
- if (typeof err == 'string') {
-
-
- msg = err.split('\n');
- errObj = new Error(msg.shift());
- if (msg.length) {
- errObj.stack = msg.join('\n');
- }
- throw errObj;
- }
- else if (err instanceof Error) {
- throw err;
- }
- else {
- throw new Error(err.toString());
- }
- }
- else {
- throw new Error();
- }
- };
- this.packageTask = function (name, version, prereqs, definition) {
- return new jake.PackageTask(name, version, prereqs, definition);
- };
- this.publishTask = function (name, prereqs, opts, definition) {
- return new jake.PublishTask(name, prereqs, opts, definition);
- };
-
- this.npmPublishTask = function (name, prereqs, opts, definition) {
- return new jake.PublishTask(name, prereqs, opts, definition);
- };
- this.testTask = function () {
- let ctor = function () {};
- let t;
- ctor.prototype = jake.TestTask.prototype;
- t = new ctor();
- jake.TestTask.apply(t, arguments);
- return t;
- };
- this.setTaskTimeout = function (t) {
- this._taskTimeout = t;
- };
- this.setSeriesAutoPrefix = function (prefix) {
- this._seriesAutoPrefix = prefix;
- };
- this.series = function (...args) {
- let prereqs = args.map((arg) => {
- let name = (this._seriesAutoPrefix || '') + arg.name;
- jake.task(name, arg);
- return name;
- });
- let seriesName = uuid();
- let seriesTask = jake.task(seriesName, prereqs);
- seriesTask._internal = true;
- let res = function () {
- return new Promise((resolve) => {
- seriesTask.invoke();
- seriesTask.on('complete', (val) => {
- resolve(val);
- });
- });
- };
- Object.defineProperty(res, 'name', {value: uuid(),
- writable: false});
- return res;
- };
- })();
- module.exports = api;
|