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 argv = yargs().argv
|
15 | var ex = new Extractor({
|
16 | excludeTags: argv.excludeTag || []
|
17 | })
|
18 |
|
19 | read(argv._).then(function (res) {
|
20 | // non-fatal errors
|
21 | if (res.error) console.warn(res.error.message)
|
22 |
|
23 | res.files.forEach(function (f) {
|
24 | ex.push(f.name, f.data)
|
25 | })
|
26 |
|
27 | var output = ex.toJson()
|
28 |
|
29 | if (argv.format === 'json') {
|
30 | console.log(JSON.stringify(output, null, 2))
|
31 |
|
32 | } else if (argv.format === 'markdown') {
|
33 | output = Mdx.renderTemplate(output)
|
34 | process.stdout.write(output)
|
35 |
|
36 | } else {
|
37 | console.error('unknown format: ' + argv.format)
|
38 | }
|
39 |
|
40 | }).catch(function (err) {
|
41 | console.error(err.stack)
|
42 | process.exit(1)
|
43 | })
|
44 | }
|
45 |
|
46 | function yargs () {
|
47 | return require('yargs')
|
48 | .version(require('../package.json').version)
|
49 | .alias('h', 'help')
|
50 | .help('help')
|
51 | .option('v', {
|
52 | alias: 'version',
|
53 | describe: 'Show version info'
|
54 | })
|
55 |
|
56 | .usage('Usage: $0 <files>')
|
57 |
|
58 | .option('format', {
|
59 | alias: 'f',
|
60 | type: 'string',
|
61 | describe: 'set output format {json,markdown}',
|
62 | default: 'json'
|
63 | })
|
64 |
|
65 | .option('exclude-tag', {
|
66 | type: 'array',
|
67 | alias: 'x',
|
68 | describe: 'tags to exclude'
|
69 | })
|
70 |
|
71 | .example('$0 lib/*.js')
|
72 | .showHelpOnFail(false, 'Specify --help for available options')
|
73 | .strict()
|
74 | }
|