UNPKG

3.34 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3require('lazy-ass');
4var xplain = require('./src/xplain');
5
6if (module.parent) {
7 module.exports = xplain;
8 return;
9}
10
11var updateNotifier = require('update-notifier');
12var notifier = updateNotifier();
13if (notifier.update) {
14 notifier.notify();
15}
16
17function hasOption(option) {
18 return process.argv.some(function (str) {
19 return str === option;
20 });
21}
22
23var bunyan = require('bunyan');
24global.log = bunyan.createLogger({ name: 'xplain' });
25log.level(hasOption('--debug') ? 'debug' : 'info');
26
27var path = require('path');
28var check = require('check-types');
29var verify = check.verify;
30var package = require('./package.json');
31
32var info = 'xplain - JavaScript API documentation generator\n' +
33' version: ' + package.version + '\n' +
34' author: ' + JSON.stringify(package.author);
35
36if (hasOption('--version') || hasOption('-v')) {
37 console.log(info);
38 process.exit(0);
39}
40
41var 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
84var inputFiles = program.input;
85if (typeof inputFiles === 'string') {
86 inputFiles = [inputFiles];
87}
88lazyAss(check.array(inputFiles), 'missing input pattern array', inputFiles);
89lazyAss(check.string(program.output), 'missing output folder', program);
90var fullFolder = path.resolve(process.cwd(), program.output);
91console.log('generating docs from', inputFiles, 'target', fullFolder);
92
93if (!program.framework) {
94 program.framework = xplain.detectFramework(inputFiles);
95 lazyAss(check.unemptyString(program.framework), 'could not guess framework from source filenames',
96 inputFiles);
97}
98log.debug('framework', { name: program.framework });
99if (!xplain.isSupported(program.framework)) {
100 console.error('Invalid framework ' + program.framework);
101 console.error('Available', xplain.supportedFrameworks());
102 process.exit(-1);
103}
104
105if (program.version) {
106 program.version = '' + program.version;
107 if (program.version) {
108 verify.string(program.version, 'invalid API version ' + program.version);
109 }
110}
111if (program.title) {
112 program.title = '' + program.title;
113 if (program.title) {
114 verify.string(program.title, 'invalid API title ' + program.title);
115 }
116}
117if (program.title) {
118 console.log('title', program.title);
119}
120if (program.version) {
121 console.log('version', program.version);
122}
123if (program.header) {
124 console.log('header', program.header);
125}
126
127verify.fn(xplain.document, 'xplain have document function');
128xplain.document({
129 patterns: inputFiles,
130 outputFolder: fullFolder,
131 title: program.title,
132 apiVersion: program.version,
133 framework: program.framework,
134 header: program.header
135});