mirror of
https://github.com/documentationjs/documentation.git
synced 2025-12-08 18:23:43 +00:00
Refactor commands to separate modules
This commit is contained in:
parent
6ab9b2f13e
commit
795d1e3794
@ -1,84 +1,11 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/* eslint no-console: 0 */
|
||||
|
||||
'use strict';
|
||||
|
||||
var documentation = require('../'),
|
||||
chokidar = require('chokidar'),
|
||||
debounce = require('debounce'),
|
||||
streamArray = require('stream-array'),
|
||||
fs = require('fs'),
|
||||
vfs = require('vinyl-fs'),
|
||||
errorPage = require('../lib/error_page'),
|
||||
Server = require('../lib/server'),
|
||||
args = require('../lib/args');
|
||||
args = require('../lib/args'),
|
||||
commands = require('../lib/commands');
|
||||
|
||||
var parsedArgs = args(process.argv.slice(2));
|
||||
|
||||
var generator = documentation.bind(null,
|
||||
parsedArgs.inputs, parsedArgs.options, onDocumented.bind(null, parsedArgs));
|
||||
|
||||
var server = new Server();
|
||||
server.on('listening', function () {
|
||||
process.stdout.write('documentation.js serving on port 4001\n');
|
||||
});
|
||||
|
||||
function onDocumented(parsedArgs, err, comments) {
|
||||
if (err) {
|
||||
if (parsedArgs.command === 'serve') {
|
||||
return server.setFiles([errorPage(err)]).start();
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
documentation.formats[parsedArgs.formatter](
|
||||
comments, parsedArgs.formatterOptions,
|
||||
onFormatted.bind(null, parsedArgs));
|
||||
}
|
||||
|
||||
function onFormatted(parsedArgs, err, output) {
|
||||
if (parsedArgs.watch) {
|
||||
updateWatcher();
|
||||
}
|
||||
|
||||
if (parsedArgs.command === 'serve') {
|
||||
server.setFiles(output).start();
|
||||
} else if (parsedArgs.output === 'stdout') {
|
||||
process.stdout.write(output);
|
||||
} else if (Array.isArray(output)) {
|
||||
streamArray(output).pipe(vfs.dest(parsedArgs.output));
|
||||
} else {
|
||||
fs.writeFileSync(parsedArgs.output, output);
|
||||
}
|
||||
}
|
||||
|
||||
if (parsedArgs.command === 'lint') {
|
||||
documentation.lint(parsedArgs.inputs, parsedArgs.options, function (err, lintOutput) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
if (lintOutput) {
|
||||
console.log(lintOutput);
|
||||
process.exit(1);
|
||||
} else {
|
||||
process.exit(0);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
generator();
|
||||
if (parsedArgs.watch) {
|
||||
var watcher = chokidar.watch(parsedArgs.inputs);
|
||||
watcher.on('all', debounce(generator, 300));
|
||||
}
|
||||
}
|
||||
|
||||
function updateWatcher() {
|
||||
documentation.expandInputs(parsedArgs.inputs, parsedArgs.options, addNewFiles);
|
||||
}
|
||||
|
||||
function addNewFiles(err, files) {
|
||||
watcher.add(files.map(function (data) {
|
||||
return data.file;
|
||||
}));
|
||||
}
|
||||
commands[parsedArgs.command](documentation, parsedArgs);
|
||||
|
||||
16
lib/args.js
16
lib/args.js
@ -2,6 +2,8 @@ var path = require('path'),
|
||||
yargs = require('yargs'),
|
||||
loadConfig = require('../lib/load_config.js');
|
||||
|
||||
var commands = require('./commands');
|
||||
|
||||
function commonOptions(parser) {
|
||||
return parser.option('shallow', {
|
||||
describe: 'shallow mode turns off dependency resolution, ' +
|
||||
@ -60,18 +62,22 @@ function outputOptions(parser) {
|
||||
});
|
||||
}
|
||||
|
||||
function addCommands(parser) {
|
||||
for (var cmd in commands) {
|
||||
parser = parser.command(cmd, commands[cmd].description)
|
||||
}
|
||||
return parser
|
||||
}
|
||||
|
||||
function parse(args) {
|
||||
// reset() needs to be called at parse time because the yargs module uses an
|
||||
// internal global variable to hold option state
|
||||
var command = yargs.reset()
|
||||
var command = addCommands(yargs.reset()
|
||||
.usage('Usage: $0 <command> [options]')
|
||||
.demand(1)
|
||||
.command('build', 'build documentation')
|
||||
.command('lint', 'check for common style and uniformity mistakes')
|
||||
.command('serve', 'generate, update, and display HTML documentation')
|
||||
.version(function () {
|
||||
return require('../package').version;
|
||||
})
|
||||
}))
|
||||
.parse(args)._[0];
|
||||
|
||||
if (command === 'build') {
|
||||
|
||||
61
lib/commands/build.js
Normal file
61
lib/commands/build.js
Normal file
@ -0,0 +1,61 @@
|
||||
'use strict';
|
||||
|
||||
var streamArray = require('stream-array'),
|
||||
fs = require('fs'),
|
||||
vfs = require('vinyl-fs'),
|
||||
chokidar = require('chokidar'),
|
||||
debounce = require('debounce');
|
||||
|
||||
module.exports = build
|
||||
module.exports.description = 'build documentation'
|
||||
|
||||
function build(documentation, parsedArgs, next) {
|
||||
var generator = documentation.bind(null,
|
||||
parsedArgs.inputs, parsedArgs.options, onDocumented.bind(null, parsedArgs));
|
||||
|
||||
function onDocumented(parsedArgs, err, comments) {
|
||||
if (err) {
|
||||
if (typeof next === 'function') {
|
||||
return next(err);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
documentation.formats[parsedArgs.formatter](
|
||||
comments, parsedArgs.formatterOptions,
|
||||
onFormatted.bind(null, parsedArgs));
|
||||
}
|
||||
|
||||
function onFormatted(parsedArgs, err, output) {
|
||||
if (parsedArgs.watch) {
|
||||
updateWatcher();
|
||||
}
|
||||
|
||||
if (typeof next === 'function') {
|
||||
next(null, output)
|
||||
} else if (parsedArgs.output === 'stdout') {
|
||||
process.stdout.write(output);
|
||||
} else if (Array.isArray(output)) {
|
||||
streamArray(output).pipe(vfs.dest(parsedArgs.output));
|
||||
} else {
|
||||
fs.writeFileSync(parsedArgs.output, output);
|
||||
}
|
||||
}
|
||||
|
||||
generator();
|
||||
if (parsedArgs.watch) {
|
||||
var watcher = chokidar.watch(parsedArgs.inputs);
|
||||
watcher.on('all', debounce(generator, 300));
|
||||
}
|
||||
|
||||
function updateWatcher() {
|
||||
documentation.expandInputs(parsedArgs.inputs, parsedArgs.options, addNewFiles);
|
||||
}
|
||||
|
||||
function addNewFiles(err, files) {
|
||||
watcher.add(files.map(function (data) {
|
||||
return data.file;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
6
lib/commands/index.js
Normal file
6
lib/commands/index.js
Normal file
@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
'build': require('./build'),
|
||||
'serve': require('./serve'),
|
||||
'lint': require('./lint')
|
||||
}
|
||||
|
||||
20
lib/commands/lint.js
Normal file
20
lib/commands/lint.js
Normal file
@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
/* eslint no-console: 0 */
|
||||
|
||||
module.exports = lint;
|
||||
module.exports.description = 'check for common style and uniformity mistakes'
|
||||
|
||||
function lint(documentation, parsedArgs) {
|
||||
documentation.lint(parsedArgs.inputs, parsedArgs.options, function (err, lintOutput) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
if (lintOutput) {
|
||||
console.log(lintOutput);
|
||||
process.exit(1);
|
||||
} else {
|
||||
process.exit(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
23
lib/commands/serve.js
Normal file
23
lib/commands/serve.js
Normal file
@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
var errorPage = require('../../lib/error_page'),
|
||||
Server = require('../../lib/server')
|
||||
|
||||
var build = require('./build')
|
||||
|
||||
module.exports = serve
|
||||
module.exports.description = 'generate, update, and display HTML documentation'
|
||||
|
||||
var server = new Server();
|
||||
server.on('listening', function () {
|
||||
process.stdout.write('documentation.js serving on port 4001\n');
|
||||
});
|
||||
|
||||
function serve(documentation, parsedArgs) {
|
||||
build(documentation, parsedArgs, function (err, output) {
|
||||
if (err) {
|
||||
return server.setFiles([errorPage(err)]).start();
|
||||
}
|
||||
server.setFiles(output).start();
|
||||
})
|
||||
}
|
||||
@ -44,7 +44,7 @@
|
||||
"vfile-sort": "^1.0.0",
|
||||
"vinyl": "^0.5.0",
|
||||
"vinyl-fs": "^1.0.0",
|
||||
"yargs": "^3.5.4"
|
||||
"yargs": "^3.31.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chdir": "0.0.0",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user