-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
153 lines (139 loc) · 4.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* The "grunt-promise" NPM module.
*
* The MIT License (MIT)
* Copyright (c) 2016 Mark Halliwell
*
* @module module:grunt-promise
*/
(function (exports, native) {
'use strict';
var grunt = require('grunt');
var util = require('./lib/util');
/**
* Determines if an object is "thenable" (aka. Promise object).
*
* An object must have a "then" function or method for it to be considered
* "thenable".
*
* @param {Object|Function} Obj
* An object or function.
* @param {Boolean} [create]
* Flag indicating whether this method should attempt to create a new
* Promise and check for a "then" method.
*
* @return {Boolean}
* TRUE or FALSE
*
* @see https://promisesaplus.com/#point-7
*/
exports.isThenable = function (Obj, create) {
return util.isThenable.apply(util, [Obj, create]);
};
/**
* Loads a Promise library/object.
*
* @deprecated since v1.1.0.
*
* @return {object<Promise>|function<Promise>|Boolean}
* A Promise object or FALSE if unable to load.
*
* @see exports.using
*/
exports.load = util.deprecate(function () {
return exports.using.apply(this, arguments);
}, 'The grunt-promise "load" method is deprecated and has been renamed to "using".');
/**
* Loads a Promise library/object.
*
* @param {String|Function|Object} [library]
* The name of the Promise API library/node module to load. It can
* also be a callback to execute. If left empty, it will attempt to find
* the first available NPM package node module installed.
*
* @return {object<Promise>|function<Promise>|Boolean}
* A Promise object or FALSE if unable to load.
*/
exports.using = function (library) {
var Promise;
// Allow a library to be specified as an option on the CLI.
if (!library) {
library = grunt.option('grunt-promise-library');
}
// Immediately return if there is already a Promise object loaded and no
// specific library was specified by the user.
if (!library && util.current) {
return util.current;
}
// Allow an explicit Promise library/module to be used.
if (library) {
Promise = util.getPromise(library);
if (!Promise) {
util.fail(library);
}
return Promise;
}
// Attempt to dynamically load first available supported promise object.
util.supportedModules.forEach(function (name) {
Promise = Promise || util.getPromise(name);
});
if (!Promise) {
if (library) {
util.fail(library);
}
else {
if (!util.loaded.native) {
util.fail();
}
Promise = util.loaded.native;
}
}
// Cache the currently loaded Promise object.
util.current = Promise;
return Promise;
};
/**
* Registers a new promise based Grunt task.
*
* @param {string} name
* The name of the Grunt task to register.
* @param {string|function} [info]
* (Optional) Descriptive text explaining what the task does. Shows up on
* `--help`. You may omit this argument and replace it with `fn` instead.
* @param {function} [fn]
* (Required) The task function. Remember not to pass in your Promise
* function directly. Promise resolvers are immediately invoked when they
* are created. You should wrap the Promise with an anonymous task function
* instead.
*
* @memberOf grunt
*
* @return {object}
* The task object.
*/
grunt.registerPromise = function (name, info, fn) {
return grunt.registerTask.apply(grunt.task, util.parseArgs(name, info, fn));
};
/**
* Registers a new promise based Grunt multi-task.
*
* @param {string} name
* The name of the Grunt task to register.
* @param {string|function} [info]
* (Optional) Descriptive text explaining what the task does. Shows up on
* `--help`. You may omit this argument and replace it with `fn` instead.
* @param {function} [fn]
* (Required) The task function. Remember not to pass in your Promise
* function directly. Promise resolvers are immediately invoked when they
* are created. You should wrap the Promise with an anonymous task function
* instead.
*
* @memberOf grunt
*
* @return {object}
* The task object.
*/
grunt.registerMultiPromise = function (name, info, fn) {
return grunt.registerMultiTask.apply(grunt.task, util.parseArgs(name, info, fn));
};
})(module.exports, Promise && Promise.toString() === 'function Promise() { [native code] }' && Promise);