index.js
1.11 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
var parse = require('./parse');
/**
* Converts a string to command line args, in particular
* groups together quoted values.
* This is a utility function to allow calling nodemon as a required
* library, but with the CLI args passed in (instead of an object).
*
* @param {String} string
* @return {Array}
*/
function stringToArgs(string) {
var args = [];
var parts = string.split(' ');
var length = parts.length;
var i = 0;
var open = false;
var grouped = '';
var lead = '';
for (; i < length; i++) {
lead = parts[i].substring(0, 1);
if (lead === '"' || lead === '\'') {
open = lead;
grouped = parts[i].substring(1);
} else if (open && parts[i].slice(-1) === open) {
open = false;
grouped += ' ' + parts[i].slice(0, -1);
args.push(grouped);
} else if (open) {
grouped += ' ' + parts[i];
} else {
args.push(parts[i]);
}
}
return args;
}
module.exports = {
parse: function (argv) {
if (typeof argv === 'string') {
argv = stringToArgs(argv);
}
return parse(argv);
},
};