This repository was archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnopt-grunt.js
63 lines (51 loc) · 1.74 KB
/
nopt-grunt.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
'use strict';
var nopt = require('nopt');
module.exports = function(grunt, options) {
/**
* Allows additional options to be registered and listed along with those
* natively supported by Grunt. Allows for smarter handling than the
* provided `grunt.option()` api.
*
* @method initOptions
* @param {Object} options
* @chainable
*/
grunt.initOptions = function(options) {
var parsed = null;
var cli = this.cli;
var optlist = cli.optlist;
var aliases = {};
var known = {};
// Append options
Object.keys(options).forEach(function(key) {
optlist[key] = options[key];
});
// Reparse options in exactly the same way as Grunt.
// https://github.com/gruntjs/grunt/blob/master/lib/grunt/cli.js
// Parse `optlist` into a form that nopt can handle.
Object.keys(optlist).forEach(function(key) {
var short = optlist[key].short;
if (short) {
aliases[short] = '--' + key;
}
known[key] = optlist[key].type;
});
parsed = nopt(known, aliases, process.argv, 2);
cli.tasks = parsed.argv.remain;
cli.options = parsed;
delete parsed.argv;
// Initialize any Array options that weren't initialized.
Object.keys(optlist).forEach(function(key) {
if (optlist[key].type === Array && !(key in cli.options)) {
cli.options[key] = [];
}
});
// Update internal value
this.option.init(cli.options);
return this;
};
// Handle `require('nopt-grunt')(grunt, { ... });`
if (arguments.length === 2) {
grunt.initOptions(options);
}
};