UNPKG

1.16 kBJavaScriptView Raw
1'use strict';
2
3var utils = require('./utils');
4var invertedCache;
5var matchersCache;
6
7module.exports = function processArgs(app, argv) {
8 // temporary hack to ensure that shortcut keys are parsed correctly
9 // this should be fixed in the parser (minimist or parse-args)
10 var inverted = invertedCache || (invertedCache = utils.invert(utils.aliases));
11 var matchers = matchersCache || (matchersCache = createMatchers(inverted));
12
13 var len = argv.length;
14 var idx = -1;
15
16 while (++idx < len) {
17 argv[idx] = replaceFlag(argv[idx], matchers);
18 }
19
20 return app.argv(argv, {
21 esc: utils.fileKeys,
22 first: ['init', 'new', 'ask', 'emit', 'global', 'save', 'config', 'file'],
23 last: ['tasks']
24 });
25};
26
27function createMatchers(obj) {
28 var res = {};
29 for (var key in obj) {
30 var val = obj[key];
31 res[key] = {};
32 res[key].re = new RegExp('(?:^|)-' + key + '(?!\\w)', 'g');
33 res[key].val = '--' + val;
34 }
35 return res;
36}
37
38function replaceFlag(str, matchers) {
39 for (var key in matchers) {
40 var matcher = matchers[key];
41
42 if (matcher.re.test(str)) {
43 return str.replace(matcher.re, matcher.val);
44 }
45 }
46 return str;
47}