-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (55 loc) · 1.61 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
/**
* Reparse the Grunt command line options flags.
*
* Using the arguments parsing logic from Grunt:
* https://github.com/gruntjs/grunt/blob/master/lib/grunt/cli.js
*/
module.exports = function(grunt){
// Get the current Grunt CLI instance.
var nopt = require('nopt'),
parsedOptions = parseOptions(nopt, grunt.cli.optlist);
grunt.log.debug('(nopt-grunt-fix) old flags: ', grunt.option.flags());
// Reassign the options.
resetOptions(grunt, parsedOptions);
grunt.log.debug('(nopt-grunt-fix) new flags: ', grunt.option.flags());
return grunt;
};
// Normalise the parameters and then parse them.
function parseOptions(nopt, optlist){
var params = getParams(optlist);
var parsedOptions = nopt(params.known, params.aliases, process.argv, 2);
initArrays(optlist, parsedOptions);
return parsedOptions;
}
// Reassign the options on the Grunt instance.
function resetOptions(grunt, parsedOptions){
for (var i in parsedOptions){
if (parsedOptions.hasOwnProperty(i) && i != 'argv'){
grunt.option(i, parsedOptions[i]);
}
}
}
// Parse `optlist` into a form that nopt can handle.
function getParams(optlist){
var aliases = {};
var known = {};
Object.keys(optlist).forEach(function(key) {
var short = optlist[key].short;
if (short) {
aliases[short] = '--' + key;
}
known[key] = optlist[key].type;
});
return {
known: known,
aliases: aliases
}
}
// Initialize any Array options that weren't initialized.
function initArrays(optlist, parsedOptions){
Object.keys(optlist).forEach(function(key) {
if (optlist[key].type === Array && !(key in parsedOptions)) {
parsedOptions[key] = [];
}
});
}