From 795d1e3794de482ce41f1b3a32559d3c35f972c8 Mon Sep 17 00:00:00 2001 From: Anand Thakker Date: Sat, 5 Dec 2015 19:21:33 -0500 Subject: [PATCH] Refactor commands to separate modules --- bin/documentation.js | 79 ++----------------------------------------- lib/args.js | 16 ++++++--- lib/commands/build.js | 61 +++++++++++++++++++++++++++++++++ lib/commands/index.js | 6 ++++ lib/commands/lint.js | 20 +++++++++++ lib/commands/serve.js | 23 +++++++++++++ package.json | 2 +- 7 files changed, 125 insertions(+), 82 deletions(-) create mode 100644 lib/commands/build.js create mode 100644 lib/commands/index.js create mode 100644 lib/commands/lint.js create mode 100644 lib/commands/serve.js diff --git a/bin/documentation.js b/bin/documentation.js index fceab47..e4a6688 100755 --- a/bin/documentation.js +++ b/bin/documentation.js @@ -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); diff --git a/lib/args.js b/lib/args.js index f26af2e..80a94fa 100644 --- a/lib/args.js +++ b/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 [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') { diff --git a/lib/commands/build.js b/lib/commands/build.js new file mode 100644 index 0000000..9a0ea72 --- /dev/null +++ b/lib/commands/build.js @@ -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; + })); + } +} + diff --git a/lib/commands/index.js b/lib/commands/index.js new file mode 100644 index 0000000..844d6e9 --- /dev/null +++ b/lib/commands/index.js @@ -0,0 +1,6 @@ +module.exports = { + 'build': require('./build'), + 'serve': require('./serve'), + 'lint': require('./lint') +} + diff --git a/lib/commands/lint.js b/lib/commands/lint.js new file mode 100644 index 0000000..32fed8d --- /dev/null +++ b/lib/commands/lint.js @@ -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); + } + }); +} diff --git a/lib/commands/serve.js b/lib/commands/serve.js new file mode 100644 index 0000000..9bc5fc3 --- /dev/null +++ b/lib/commands/serve.js @@ -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(); + }) +} diff --git a/package.json b/package.json index 0013d80..e56ce4e 100644 --- a/package.json +++ b/package.json @@ -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",