-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.js
executable file
·80 lines (69 loc) · 1.65 KB
/
cli.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
#!/usr/bin/env node
'use strict';
const mdn = require('./');
const minimist = require('minimist');
const version = require('./package.json').version;
const defaults = {
boolean: [
'help',
'version',
'open',
'css'
],
alias: {
h: 'help',
v: 'version',
l: 'language',
c: 'css',
o: 'open',
lc: 'locale'
},
default: {
language: 'js',
locale: 'en-US',
open: false,
css: false
}
};
const options = minimist(process.argv.slice(2), defaults);
const help = `
Usage: mdn <KEYWORD>
Man pages for web APIs using MDN
Example:
$ mdn object.freeze
$ mdn background-image --language=css
$ mdn background --language=css --locale=pt-BR
Options:
-v --version Display current software version
-h --help Display software help and usage details
-l --language Specify a language to search for the keyword (defaults to "js")
-c --css Specify css as the language
-o --open Open MDN page in web browser
-lc --locale Specify a locale (defaults to "en-US")
`;
const run = options => {
if (options.help) {
process.stderr.write(help);
return;
}
if (options.version) {
process.stderr.write(`mdn v${version}\n`);
return;
}
const keyword = options._[0];
const language = (options.css) ? 'css' : options.language;
const shouldOpen = options.open;
const locale = options.locale;
if (keyword !== undefined && keyword.length) {
mdn({
keyword,
language,
shouldOpen,
locale
});
} else {
process.stderr.write('You must provide a valid keyword\n');
process.exit(1);
}
};
run(options);