@@ -4,35 +4,36 @@ var streamArray = require('stream-array'),
44 sharedOptions = require ( './shared_options' ) ,
55 fs = require ( 'fs' ) ,
66 vfs = require ( 'vinyl-fs' ) ,
7+ extend = require ( 'extend' ) ,
78 chokidar = require ( 'chokidar' ) ,
9+ documentation = require ( '../../' ) ,
810 debounce = require ( 'debounce' ) ;
911
10- module . exports = build ;
11- module . exports . description = 'build documentation' ;
12+ module . exports . command = ' build [input..]' ;
13+ module . exports . describe = 'build documentation' ;
1214
1315/**
1416 * Add yargs parsing for the build command
1517 * @param {Object } yargs module instance
1618 * @returns {Object } yargs with options
1719 * @private
1820 */
19- module . exports . parseArgs = function ( yargs ) {
20- return sharedOptions . sharedOutputOptions (
21- sharedOptions . sharedInputOptions ( yargs ) )
22- . option ( ' format' , {
21+ module . exports . builder = extend ( { } ,
22+ sharedOptions . sharedOutputOptions ,
23+ sharedOptions . sharedInputOptions , {
24+ format : {
2325 alias : 'f' ,
2426 default : 'json' ,
2527 choices : [ 'json' , 'md' , 'remark' , 'html' ]
26- } )
27- . option ( ' output' , {
28+ } ,
29+ output : {
2830 describe : 'output location. omit for stdout, otherwise is a filename ' +
29- 'for single-file outputs and a directory name for multi-file outputs like html' ,
31+ 'for single-file outputs and a directory name for multi-file outputs like html' ,
3032 default : 'stdout' ,
3133 alias : 'o'
32- } )
33- . example ( 'documentation build foo.js -f md > API.md' , 'parse documentation in a ' +
34- 'file and generate API documentation as Markdown' ) ;
35- } ;
34+ } ,
35+ example : 'documentation build foo.js -f md > API.md'
36+ } ) ;
3637
3738/*
3839 * The `build` command. Requires either `--output` or the `callback` argument.
@@ -42,22 +43,22 @@ module.exports.parseArgs = function (yargs) {
4243 * The former case, with the callback, is used by the `serve` command, which is
4344 * just a thin wrapper around this one.
4445 */
45- function build ( documentation , parsedArgs , callback ) {
46- var inputs = parsedArgs . inputs ;
47- var buildOptions = parsedArgs . commandOptions ;
48- var options = parsedArgs . options ;
49- if ( options . f === 'html' && options . o === 'stdout' ) {
46+ module . exports . handler = function build ( argv , callback ) {
47+ argv . _handled = true ;
48+ argv = sharedOptions . expandInputs ( argv ) ;
49+ if ( argv . f === 'html' && argv . o === 'stdout' ) {
5050 throw new Error ( 'The HTML output mode requires a destination directory set with -o' ) ;
5151 }
5252 var formatterOptions = {
53- name : buildOptions . name || ( options . package || { } ) . name ,
54- version : buildOptions [ 'project-version' ] || ( options . package || { } ) . version ,
55- theme : buildOptions . theme ,
56- paths : options . paths ,
57- hljs : options . hljs || { }
53+ name : argv . name || ( argv . package || { } ) . name ,
54+ version : argv [ 'project-version' ] || ( argv . package || { } ) . version ,
55+ theme : argv . theme ,
56+ paths : argv . paths ,
57+ hljs : argv . hljs || { }
5858 } ;
5959
60- var generator = documentation . build . bind ( null , inputs , options , onDocumented ) ;
60+ var generator = documentation . build
61+ . bind ( null , argv . input , argv , onDocumented ) ;
6162
6263 function onDocumented ( err , comments ) {
6364 if ( err ) {
@@ -67,38 +68,38 @@ function build(documentation, parsedArgs, callback) {
6768 throw err ;
6869 }
6970
70- documentation . formats [ buildOptions . format ] ( comments , formatterOptions , onFormatted ) ;
71+ documentation . formats [ argv . format ] ( comments , formatterOptions , onFormatted ) ;
7172 }
7273
7374 function onFormatted ( err , output ) {
74- if ( buildOptions . watch ) {
75+ if ( argv . watch ) {
7576 updateWatcher ( ) ;
7677 }
7778
7879 if ( typeof callback === 'function' ) {
7980 callback ( null , output ) ;
80- } else if ( buildOptions . output === 'stdout' ) {
81+ } else if ( argv . output === 'stdout' ) {
8182 process . stdout . write ( output ) ;
8283 } else if ( Array . isArray ( output ) ) {
83- streamArray ( output ) . pipe ( vfs . dest ( buildOptions . output ) ) ;
84+ streamArray ( output ) . pipe ( vfs . dest ( argv . output ) ) ;
8485 } else {
85- fs . writeFileSync ( buildOptions . output , output ) ;
86+ fs . writeFileSync ( argv . output , output ) ;
8687 }
8788 }
8889
89- if ( buildOptions . watch ) {
90- var watcher = chokidar . watch ( inputs ) ;
90+ if ( argv . watch ) {
91+ var watcher = chokidar . watch ( argv . input ) ;
9192 watcher . on ( 'all' , debounce ( generator , 300 ) ) ;
9293 }
9394 generator ( ) ;
9495
9596 function updateWatcher ( ) {
96- documentation . expandInputs ( inputs , options , addNewFiles ) ;
97+ documentation . expandInputs ( argv . input , argv , addNewFiles ) ;
9798 }
9899
99100 function addNewFiles ( err , files ) {
100101 watcher . add ( files . map ( function ( data ) {
101102 return typeof data === 'string' ? data : data . file ;
102103 } ) ) ;
103104 }
104- }
105+ } ;
0 commit comments