1 | #!/usr/bin/env node
|
2 |
|
3 | require('lazy-ass');
|
4 | var xplain = require('./src/xplain');
|
5 |
|
6 | if (module.parent) {
|
7 | module.exports = xplain;
|
8 | return;
|
9 | }
|
10 |
|
11 | var updateNotifier = require('update-notifier');
|
12 | var notifier = updateNotifier();
|
13 | if (notifier.update) {
|
14 | notifier.notify();
|
15 | }
|
16 |
|
17 | function hasOption(option) {
|
18 | return process.argv.some(function (str) {
|
19 | return str === option;
|
20 | });
|
21 | }
|
22 |
|
23 | var bunyan = require('bunyan');
|
24 | global.log = bunyan.createLogger({ name: 'xplain' });
|
25 | log.level(hasOption('--debug') ? 'debug' : 'info');
|
26 |
|
27 | var path = require('path');
|
28 | var check = require('check-types');
|
29 | var verify = check.verify;
|
30 | var package = require('./package.json');
|
31 |
|
32 | var info = 'xplain - JavaScript API documentation generator\n' +
|
33 | ' version: ' + package.version + '\n' +
|
34 | ' author: ' + JSON.stringify(package.author);
|
35 |
|
36 | if (hasOption('--version') || hasOption('-v')) {
|
37 | console.log(info);
|
38 | process.exit(0);
|
39 | }
|
40 |
|
41 | var program = require('optimist')
|
42 | .usage(info)
|
43 | .options('input', {
|
44 | alias: 'i',
|
45 | string: true,
|
46 | description: 'input file(s), you can use wildcards'
|
47 | })
|
48 | .options('output', {
|
49 | alias: 'o',
|
50 | string: true,
|
51 | description: 'output folder name / Markdown filename',
|
52 | default: 'docs'
|
53 | })
|
54 | .options('title', {
|
55 | alias: 't',
|
56 | string: true,
|
57 | description: 'API title to use',
|
58 | default: ''
|
59 | })
|
60 | .options('version', {
|
61 | alias: 'v',
|
62 | string: true,
|
63 | description: 'API version to add to title',
|
64 | default: ''
|
65 | })
|
66 | .options('framework', {
|
67 | alias: 'f',
|
68 | string: true,
|
69 | description: 'unit testing framework name',
|
70 | default: ''
|
71 | })
|
72 | .options('header', {
|
73 | alias: 'e',
|
74 | string: true,
|
75 | description: 'optional Markdown doc to use as header',
|
76 | default: '',
|
77 | check: function (value) {
|
78 | return (/\.md$/).test(value);
|
79 | }
|
80 | })
|
81 | .demand(['input'])
|
82 | .argv;
|
83 |
|
84 | var inputFiles = program.input;
|
85 | if (typeof inputFiles === 'string') {
|
86 | inputFiles = [inputFiles];
|
87 | }
|
88 | lazyAss(check.array(inputFiles), 'missing input pattern array', inputFiles);
|
89 | lazyAss(check.string(program.output), 'missing output folder', program);
|
90 | var fullFolder = path.resolve(process.cwd(), program.output);
|
91 | console.log('generating docs from', inputFiles, 'target', fullFolder);
|
92 |
|
93 | if (!program.framework) {
|
94 | program.framework = xplain.detectFramework(inputFiles);
|
95 | lazyAss(check.unemptyString(program.framework), 'could not guess framework from source filenames',
|
96 | inputFiles);
|
97 | }
|
98 | log.debug('framework', { name: program.framework });
|
99 | if (!xplain.isSupported(program.framework)) {
|
100 | console.error('Invalid framework ' + program.framework);
|
101 | console.error('Available', xplain.supportedFrameworks());
|
102 | process.exit(-1);
|
103 | }
|
104 |
|
105 | if (program.version) {
|
106 | program.version = '' + program.version;
|
107 | if (program.version) {
|
108 | verify.string(program.version, 'invalid API version ' + program.version);
|
109 | }
|
110 | }
|
111 | if (program.title) {
|
112 | program.title = '' + program.title;
|
113 | if (program.title) {
|
114 | verify.string(program.title, 'invalid API title ' + program.title);
|
115 | }
|
116 | }
|
117 | if (program.title) {
|
118 | console.log('title', program.title);
|
119 | }
|
120 | if (program.version) {
|
121 | console.log('version', program.version);
|
122 | }
|
123 | if (program.header) {
|
124 | console.log('header', program.header);
|
125 | }
|
126 |
|
127 | verify.fn(xplain.document, 'xplain have document function');
|
128 | xplain.document({
|
129 | patterns: inputFiles,
|
130 | outputFolder: fullFolder,
|
131 | title: program.title,
|
132 | apiVersion: program.version,
|
133 | framework: program.framework,
|
134 | header: program.header
|
135 | });
|