@@ -4,35 +4,36 @@ var streamArray = require('stream-array'),
4
4
sharedOptions = require ( './shared_options' ) ,
5
5
fs = require ( 'fs' ) ,
6
6
vfs = require ( 'vinyl-fs' ) ,
7
+ extend = require ( 'extend' ) ,
7
8
chokidar = require ( 'chokidar' ) ,
9
+ documentation = require ( '../../' ) ,
8
10
debounce = require ( 'debounce' ) ;
9
11
10
- module . exports = build ;
11
- module . exports . description = 'build documentation' ;
12
+ module . exports . command = ' build [input..]' ;
13
+ module . exports . describe = 'build documentation' ;
12
14
13
15
/**
14
16
* Add yargs parsing for the build command
15
17
* @param {Object } yargs module instance
16
18
* @returns {Object } yargs with options
17
19
* @private
18
20
*/
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 : {
23
25
alias : 'f' ,
24
26
default : 'json' ,
25
27
choices : [ 'json' , 'md' , 'remark' , 'html' ]
26
- } )
27
- . option ( ' output' , {
28
+ } ,
29
+ output : {
28
30
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' ,
30
32
default : 'stdout' ,
31
33
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
+ } ) ;
36
37
37
38
/*
38
39
* The `build` command. Requires either `--output` or the `callback` argument.
@@ -42,22 +43,22 @@ module.exports.parseArgs = function (yargs) {
42
43
* The former case, with the callback, is used by the `serve` command, which is
43
44
* just a thin wrapper around this one.
44
45
*/
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' ) {
50
50
throw new Error ( 'The HTML output mode requires a destination directory set with -o' ) ;
51
51
}
52
52
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 || { }
58
58
} ;
59
59
60
- var generator = documentation . build . bind ( null , inputs , options , onDocumented ) ;
60
+ var generator = documentation . build
61
+ . bind ( null , argv . input , argv , onDocumented ) ;
61
62
62
63
function onDocumented ( err , comments ) {
63
64
if ( err ) {
@@ -67,38 +68,38 @@ function build(documentation, parsedArgs, callback) {
67
68
throw err ;
68
69
}
69
70
70
- documentation . formats [ buildOptions . format ] ( comments , formatterOptions , onFormatted ) ;
71
+ documentation . formats [ argv . format ] ( comments , formatterOptions , onFormatted ) ;
71
72
}
72
73
73
74
function onFormatted ( err , output ) {
74
- if ( buildOptions . watch ) {
75
+ if ( argv . watch ) {
75
76
updateWatcher ( ) ;
76
77
}
77
78
78
79
if ( typeof callback === 'function' ) {
79
80
callback ( null , output ) ;
80
- } else if ( buildOptions . output === 'stdout' ) {
81
+ } else if ( argv . output === 'stdout' ) {
81
82
process . stdout . write ( output ) ;
82
83
} else if ( Array . isArray ( output ) ) {
83
- streamArray ( output ) . pipe ( vfs . dest ( buildOptions . output ) ) ;
84
+ streamArray ( output ) . pipe ( vfs . dest ( argv . output ) ) ;
84
85
} else {
85
- fs . writeFileSync ( buildOptions . output , output ) ;
86
+ fs . writeFileSync ( argv . output , output ) ;
86
87
}
87
88
}
88
89
89
- if ( buildOptions . watch ) {
90
- var watcher = chokidar . watch ( inputs ) ;
90
+ if ( argv . watch ) {
91
+ var watcher = chokidar . watch ( argv . input ) ;
91
92
watcher . on ( 'all' , debounce ( generator , 300 ) ) ;
92
93
}
93
94
generator ( ) ;
94
95
95
96
function updateWatcher ( ) {
96
- documentation . expandInputs ( inputs , options , addNewFiles ) ;
97
+ documentation . expandInputs ( argv . input , argv , addNewFiles ) ;
97
98
}
98
99
99
100
function addNewFiles ( err , files ) {
100
101
watcher . add ( files . map ( function ( data ) {
101
102
return typeof data === 'string' ? data : data . file ;
102
103
} ) ) ;
103
104
}
104
- }
105
+ } ;
0 commit comments