-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·133 lines (114 loc) · 2.8 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
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
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
const argv = require('minimist')(process.argv.slice(2));
const path = require('path');
const glob = require('glob');
const merge = require('deepmerge');
const configfiles = argv._;
// Our own options
let opts = {
join: {
long: "split",
short: "split",
},
dash: {
long: "--",
short: "-",
},
};
// Show help/usage
if (argv.help || argv.h) {
process.stdout.write(`\n`);
process.stdout.write(`Usage: ${process.argv[1]} [options] configfile [configfile...]\n`);
process.stdout.write(`\n`);
process.exit(0);
}
// Ensure we have config files
if (!configfiles.length) {
process.stdout.write(`No config files were given.\n`);
process.stdout.write(`Run '${process.argv[1]} --help' to see usage.\n`);
process.exit(1);
}
// Catch for multi-config
if (configfiles.length > 1) {
process.stdout.write(`Multiple config files are not supported.\n`);
process.stdout.write(`Run '${process.argv[1]} --help' to see usage.\n`);
process.exit(1);
}
// Load
let cfg = {};
for(const filename of configfiles) {
cfg = merge(cfg, require(path.resolve(filename)));
}
// Load own options
if (cfg['$']) {
opts = merge(opts, cfg['$']);
delete cfg['$'];
}
// Escape helper function
function shell_escape(str) {
if (!str) return '';
return str
.split('\\').join('\\\\')
.split(' ').join('\\ ')
;
}
// Build arguments
const args = [];
async function parseArgument(key, value) {
// Handle globs
if ('string' === typeof value && ~value.indexOf('*')) {
const entries = await new Promise((s,f) => {
glob(value, function(err, files) {
if (err) return f(err);
s(files);
});
});
if (entries.length > 1) {
for(const entry of entries) {
await parseArgument(key, entry)
}
return;
}
value = entries[0];
}
// Minimist-like direct arguments
if ('_' == key) {
args.push(value);
return;
}
// Fetch how to render args
const isLong = key.length > 1 ? opts.join.long : opts.join.short;
const join = isLong ? opts.join.long : opts.join.short;
const dash = isLong ? opts.dash.long : opts.dash.short;
// Handle boolean like a flag
if (value === false) return;
if (value === true) {
args.push(dash + key);
return;
}
// Custom split join
if ('split' == join) {
args.push(dash + key);
args.push(value);
return;
}
// Merge
args.push(dash + key + join + value);
}
(async () => {
for (const key in cfg) {
let opt = cfg[key];
if (!Array.isArray(opt)) opt = [opt];
for(const arg of opt) {
await parseArgument(key, arg);
}
}
const output = args
.filter(arg => arg)
.map(shell_escape)
.join(' ');
process.stderr.write(output);
process.stderr.write('\n');
process.stdout.write(output);
process.stdout.write('\n');
})();