Skip to content

Commit 571137a

Browse files
committed
Merge pull request #277 from documentationjs/refactor-commands-new
Refactor commands into modules
2 parents a6f0435 + b609664 commit 571137a

File tree

7 files changed

+265
-238
lines changed

7 files changed

+265
-238
lines changed

Diff for: bin/documentation.js

+78-66
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,96 @@
11
#!/usr/bin/env node
22

3-
/* eslint no-console: 0 */
4-
53
'use strict';
64

75
var documentation = require('../'),
8-
chokidar = require('chokidar'),
9-
debounce = require('debounce'),
10-
streamArray = require('stream-array'),
11-
fs = require('fs'),
12-
vfs = require('vinyl-fs'),
13-
errorPage = require('../lib/error_page'),
14-
Server = require('../lib/server'),
15-
args = require('../lib/args');
16-
17-
var parsedArgs = args(process.argv.slice(2));
6+
path = require('path'),
7+
yargs = require('yargs'),
8+
extend = require('extend'),
9+
loadConfig = require('../lib/load_config.js'),
10+
commands = require('../lib/commands');
1811

19-
var generator = documentation.bind(null,
20-
parsedArgs.inputs, parsedArgs.options, onDocumented.bind(null, parsedArgs));
12+
var parsedArgs = parseArgs();
13+
commands[parsedArgs.command](documentation, parsedArgs);
2114

22-
var server = new Server();
23-
server.on('listening', function () {
24-
process.stdout.write('documentation.js serving on port 4001\n');
25-
});
15+
function parseArgs() {
16+
// reset() needs to be called at parse time because the yargs module uses an
17+
// internal global variable to hold option state
18+
var argv = yargs
19+
.usage('Usage: $0 <command> [options]')
20+
.version(function () {
21+
return require('../package').version;
22+
})
23+
.option('shallow', {
24+
describe: 'shallow mode turns off dependency resolution, ' +
25+
'only processing the specified files (or the main script specified in package.json)',
26+
default: false,
27+
type: 'boolean'
28+
})
29+
.option('config', {
30+
describe: 'configuration file. an array defining explicit sort order',
31+
alias: 'c'
32+
})
33+
.option('external', {
34+
describe: 'a string / glob match pattern that defines which external ' +
35+
'modules will be whitelisted and included in the generated documentation.',
36+
default: null
37+
})
38+
.option('extension', {
39+
describe: 'only input source files matching this extension will be parsed, ' +
40+
'this option can be used multiple times.',
41+
alias: 'e'
42+
})
43+
.option('polyglot', {
44+
type: 'boolean',
45+
describe: 'polyglot mode turns off dependency resolution and ' +
46+
'enables multi-language support. use this to document c++'
47+
})
48+
.option('private', {
49+
describe: 'generate documentation tagged as private',
50+
type: 'boolean',
51+
default: false,
52+
alias: 'p'
53+
})
54+
.option('github', {
55+
type: 'boolean',
56+
describe: 'infer links to github in documentation',
57+
alias: 'g'
58+
})
59+
.argv;
2660

27-
function onDocumented(parsedArgs, err, comments) {
28-
if (err) {
29-
if (parsedArgs.command === 'serve') {
30-
return server.setFiles([errorPage(err)]).start();
31-
}
32-
throw err;
61+
var options = {};
62+
if (argv.config) {
63+
options = loadConfig(argv.config);
3364
}
65+
options = extend(options, argv);
3466

35-
documentation.formats[parsedArgs.formatter](
36-
comments, parsedArgs.formatterOptions,
37-
onFormatted.bind(null, parsedArgs));
38-
}
67+
var command = argv._[0],
68+
inputs = argv._.slice(1);
3969

40-
function onFormatted(parsedArgs, err, output) {
41-
if (parsedArgs.watch) {
42-
updateWatcher();
70+
if (inputs.length == 0) {
71+
try {
72+
var p = require(path.resolve('package.json'));
73+
options.package = p;
74+
inputs = [p.main || 'index.js'];
75+
} catch (e) {
76+
yargs.showHelp();
77+
throw new Error('documentation was given no files and was not run in a module directory');
78+
}
4379
}
4480

45-
if (parsedArgs.command === 'serve') {
46-
server.setFiles(output).start();
47-
} else if (parsedArgs.output === 'stdout') {
48-
process.stdout.write(output);
49-
} else if (Array.isArray(output)) {
50-
streamArray(output).pipe(vfs.dest(parsedArgs.output));
51-
} else {
52-
fs.writeFileSync(parsedArgs.output, output);
53-
}
81+
return {
82+
inputs: inputs,
83+
command: command,
84+
commandOptions: addCommands(yargs).argv,
85+
options: options
86+
};
5487
}
5588

56-
if (parsedArgs.command === 'lint') {
57-
documentation.lint(parsedArgs.inputs, parsedArgs.options, function (err, lintOutput) {
58-
if (err) {
59-
throw err;
60-
}
61-
if (lintOutput) {
62-
console.log(lintOutput);
63-
process.exit(1);
64-
} else {
65-
process.exit(0);
66-
}
67-
});
68-
} else {
69-
generator();
70-
if (parsedArgs.watch) {
71-
var watcher = chokidar.watch(parsedArgs.inputs);
72-
watcher.on('all', debounce(generator, 300));
89+
function addCommands(parser) {
90+
parser = parser.demand(1);
91+
for (var cmd in commands) {
92+
parser = parser.command(cmd, commands[cmd].description, commands[cmd].parseArgs);
7393
}
94+
return parser.help('help');
7495
}
7596

76-
function updateWatcher() {
77-
documentation.expandInputs(parsedArgs.inputs, parsedArgs.options, addNewFiles);
78-
}
79-
80-
function addNewFiles(err, files) {
81-
watcher.add(files.map(function (data) {
82-
return data.file;
83-
}));
84-
}

Diff for: lib/args.js

-171
This file was deleted.

0 commit comments

Comments
 (0)