1 | #!/usr/bin/env node
|
2 |
|
3 | var Mdx = require('../index')
|
4 | var Extractor = Mdx.Extractor
|
5 |
|
6 | if (!process.parent) run()
|
7 |
|
8 | /**
|
9 | * Runs.
|
10 | */
|
11 |
|
12 | function run () {
|
13 | var read = require('read-input')
|
14 | var cli = getcli()
|
15 |
|
16 | var ex = new Extractor({
|
17 | excludeTags: cli.flags.excludeTag
|
18 | })
|
19 |
|
20 | read(cli.input).then(function (res) {
|
21 | // non-fatal errors
|
22 | if (res.error) console.warn(res.error.message)
|
23 |
|
24 | res.files.forEach(function (f) {
|
25 | ex.push(f.name, f.data)
|
26 | })
|
27 |
|
28 | var output = ex.toJson()
|
29 |
|
30 | if (cli.flags.format === 'json') {
|
31 | console.log(JSON.stringify(output, null, 2))
|
32 |
|
33 | } else if (cli.flags.format === 'markdown') {
|
34 | output = Mdx.renderTemplate(output)
|
35 | process.stdout.write(output)
|
36 |
|
37 | } else {
|
38 | console.error('unknown format: ' + cli.flags.format)
|
39 | }
|
40 |
|
41 | }).catch(function (err) {
|
42 | console.error(err.stack)
|
43 | process.exit(1)
|
44 | })
|
45 | }
|
46 |
|
47 | function getcli () {
|
48 | var cli = require('meow')(`
|
49 | Usage: mdx [options] [files...]
|
50 |
|
51 | Options:
|
52 | -f, --format FMT set output format {json, markdown}
|
53 | -m, --markdown short for --format=markdown
|
54 | --json short for --format=json
|
55 | -x, --exclude-tag TAG excludes certain tag (can be repeated)
|
56 |
|
57 | Other options:
|
58 | -h, --help show usage information
|
59 | -v, --version print version info and exit
|
60 |
|
61 | Examples:
|
62 | mdx lib/*.js
|
63 | cat lib/*.js | mdx -x internal -f markdown
|
64 | `, {
|
65 | alias: {
|
66 | h: 'help', v: 'version', f: 'format', x: 'exclude-tag',
|
67 | m: 'markdown'
|
68 | },
|
69 | boolean: ['markdown', 'json'],
|
70 | string: ['exclude-tag', 'format'],
|
71 | default: { format: 'json' },
|
72 | '--': true
|
73 | })
|
74 |
|
75 | if (cli.flags.markdown) cli.flags.format = 'markdown'
|
76 | if (cli.flags.json) cli.flags.format = 'json'
|
77 |
|
78 | if (!cli.flags.excludeTag) {
|
79 | cli.flags.excludeTag = []
|
80 | } else if (!Array.isArray(cli.flags.excludeTag)) {
|
81 | cli.flags.excludeTag = [cli.flags.excludeTag]
|
82 | }
|
83 |
|
84 | return cli
|
85 | }
|