-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
121 lines (108 loc) · 3.2 KB
/
util.js
File metadata and controls
121 lines (108 loc) · 3.2 KB
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
var fs = require('fs');
var util = module.exports = {
/** @module util */
/**
* Takes a destination object and any number of source objects, and
* shallow-copies each source's values into the destination object.
* Operates left-to-right, so if multiple sources provide the same keys,
* the rightmost's key will win.
*
* @param {Object} dest Object to receive properties from source(s)
* @param {...Object} source Object to be mixed into the destination object
* @return {Object} Destination object, now including properties from source(s)
**/
mixin: function (dest) {
var len = arguments.length;
var src;
function copyProperty(k) {
dest[k] = src[k];
}
for (var i = 1; i < len; i++) {
src = arguments[i];
Object.keys(src).forEach(copyProperty);
}
return dest;
},
/**
* Contains options and parameters discovered by parseArgv.
* @typedef {Object} ParsedArgv
* @property {Object} options Hash mapping any found option keys to
* their values (or true if the option was a key with no value)
* @property {string[]} parameters Array containing any other arguments
*/
/**
* Parses argv for options in the format --opt or --opt=value.
* Yes, there are robust node libraries for option parsing, but
* for this simple purpose, incurring a 500-LOC dependency is overkill.
*
* @return {ParsedArgv}
*/
parseArgv: function () {
var argv = process.argv;
var options = {};
var parameters = [];
var optionRx = /^--([^=]+)(?:=(.+))?$/;
var len = argv.length;
var i;
var arg;
var match;
for (i = 2; i < len; i++) {
arg = argv[i];
match = optionRx.exec(arg);
if (match) {
options[match[1]] = match[2] || true;
}
else {
parameters.push(arg);
}
}
return {
options: options,
parameters: parameters
};
},
/**
* Contains options and parameters discovered by parseArgvForConfig.
* @typedef {Object} ParsedArgvWithConfig
* @property {Object} options Hash based on parsed config, additionally
* mapping any other found option keys to their values (or true if
* the option was a key with no value)
* @property {string[]} parameters Array containing any other arguments
*/
/**
* Parses argv for options in the format --opt or --opt=value, then
* attempts to open a configuration file in JSON format based on a
* path provided via the given paramName; if found, the config will be
* used as a base upon which any other command-line arguments will be
* mixed atop.
*
* @param {string} [name="config"] Name of option to check for config filename
* @return {ParsedArgvWithConfig}
*/
parseArgvForConfig: function (name) {
name = name || 'config';
var argv = util.parseArgv();
var options = argv.options;
var filename = options[name];
var config;
if (filename) {
delete options[name];
// Add .json if filename didn't already include it
filename += filename.slice(-5) === '.json' ? '' : '.json';
try {
config = util.mixin(options, JSON.parse(fs.readFileSync(filename)));
}
catch (e) {
console.warn('Failed to parse JSON from ' + filename +
'; continuing without parsed config');
}
}
if (!config) {
config = {};
}
return {
options: config,
parameters: argv.parameters
};
}
};