forked from linterhub/usage-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
57 lines (50 loc) · 1.5 KB
/
bin.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
#!/usr/bin/env node
const parser = require('./src/parser');
const configDefault = require('./src/template/configDefault.json');
const {execSync} = require('child_process');
const version = require('./package.json').version;
const program = require('commander');
const fs = require('fs');
const runParser = (doc, config) => {
console.log(JSON.stringify(parser(doc, config), null, 4));
};
program
.version(version)
.option(
'-c, --config <config>',
'Custom config in json format',
configDefault
)
.option(
'-d, --docs <docs>',
'The help page content (pass without <binary> argument)',
undefined
)
.option(
'-f, --file <file>',
'Path to a file with CLI docs',
undefined
)
.arguments('<binary>')
.action(function(binary) {
program.docs = execSync(`${binary} --help`).toString();
})
.description(
'Parse help page specifying binary as argument or content as option')
.parse(process.argv);
if (program.docs || program.file) {
runParser(program.docs ? program.docs :
fs.readFileSync(program.file, 'utf8'), program.config);
} else if (process.stdin && !process.stdin.isTTY) {
let doc = '';
process.stdin.setEncoding('utf-8');
process.stdin.on('readable', () => {
let chunk;
while (chunk = process.stdin.read()) {
doc += chunk;
}
});
process.stdin.on('end', () => runParser(doc, program.config));
} else {
program.help();
}