@@ -8,6 +8,8 @@ const cliOptions = require('./cli-options')
88const jsdoc = require ( 'jsdoc-api' )
99const transform = require ( './transform' )
1010const collectJson = require ( 'collect-json' )
11+ const assert = require ( 'assert' )
12+ const connect = require ( 'stream-connect' )
1113
1214/**
1315 * Exports a single function to parse jsdoc data.
@@ -32,105 +34,37 @@ jsdocParse.cliOptions = cliOptions.definitions
3234 */
3335function jsdocParse ( options ) {
3436 options = new ParseOptions ( options )
35-
36- if ( options . invalidMessage ) {
37+ try {
38+ options . validate ( )
39+ } catch ( err ) {
3740 const output = new Transform ( )
3841 process . nextTick ( function ( ) {
39- output . emit ( 'error' , new Error ( options . invalidMessage ) )
42+ output . emit ( 'error' , err )
4043 } )
4144 return output
42- } else {
43- if ( options . src ) {
44- const jsdocOptions = { }
45- if ( options . html ) jsdocOptions . configure = path . resolve ( __dirname , 'html-conf.json' )
46- const output = jsdoc . createExplainStream ( options . fileSet . files , jsdocOptions )
47- . on ( 'error' , ( err => output . emit ( 'error' , err ) ) )
48- . pipe ( transform ( ) )
49- . on ( 'error' , ( err => output . emit ( 'error' , err ) ) )
50- . pipe ( collectJson ( function ( data ) {
51- return applyOptions ( data , options )
52- } ) )
53- return output
54- }
5545 }
5646
57- return
58- // if (src) {
59- // var inputFiles = fileSet(src)
60- // src = inputFiles.files
61- //
62- // var output = new OutputTransform(options)
63- //
64- // if (!src.length) {
65- // var msg = util.format('[jsdoc-parse] please specify valid input files. ')
66- // if (inputFiles.notExisting) {
67- // msg += 'These files do not exist: ' + inputFiles.notExisting.join(', ')
68- // }
69- // /* defer, to give client chance to attach listener */
70- // process.nextTick(function () {
71- // output.emit('error', new Error(msg))
72- // })
73- // } else {
74- // getJsdocOutput(src, options, function (err, data) {
75- // if (err) {
76- // output.emit('error', err)
77- // } else {
78- // output.end(data)
79- // }
80- // })
81- // }
82- // return output
83- // } else {
84- // var inputStream = new Transform()
85- // var inputFilePath = tempPath()
86- //
87- // var buf = new Buffer(0)
88- // inputStream._transform = function (chunk, enc, done) {
89- // if (chunk) buf = Buffer.concat([buf, chunk])
90- // done()
91- // }
92- // inputStream._flush = function (done) {
93- // var self = this
94- // fs.writeFileSync(inputFilePath, buf)
95- // getJsdocOutput([ inputFilePath ], options, function (err, data) {
96- // if (err) {
97- // done(err)
98- // } else {
99- // try {
100- // data = applyOptions(data, options)
101- // self.push(data)
102- // self.push(null)
103- // done()
104- // } catch (err) {
105- // done(err)
106- // }
107- // }
108- // fs.unlinkSync(inputFilePath)
109- // })
110- // }
111- // return inputStream
112- // }
113- }
47+ const jsdocOptions = { }
48+ if ( options . html ) jsdocOptions . configure = path . resolve ( __dirname , 'html-conf.json' )
49+ jsdocOptions . files = options . files
11450
115- function OutputTransform ( options ) {
116- Transform . call ( this )
117- this . json = new Buffer ( 0 )
118- this . _transform = function ( chunk , enc , done ) {
119- if ( chunk ) this . json = Buffer . concat ( [ this . json , chunk ] )
120- done ( )
121- }
122- this . _flush = function ( ) {
123- try {
124- this . json = applyOptions ( this . json , options )
125- } catch ( err ) {
126- err . message += ' [Problem parsing the JSON data output by jsdoc, input data: ' + this . json . toString ( ) . substr ( 0 , 100 ) + ']'
127- return this . emit ( 'error' , err )
128- }
129- this . push ( this . json )
130- this . push ( null )
51+ const explainStream = jsdoc
52+ . createExplainStream ( jsdocOptions )
53+ . once ( 'error' , emitError )
54+
55+ const transformedStream = transform ( )
56+ . once ( 'error' , emitError )
57+
58+ const outputStream = collectJson ( function ( data ) {
59+ return applyOptions ( data , options )
60+ } ) . once ( 'error' , emitError )
61+
62+ function emitError ( err ) {
63+ outputStream . emit ( 'error' , err )
13164 }
65+
66+ return connect ( explainStream , connect ( transformedStream , outputStream ) )
13267}
133- util . inherits ( OutputTransform , Transform )
13468
13569/**
13670 * @param {string } - input json string
@@ -246,16 +180,22 @@ class ParseOptions {
246180 this [ 'sort-by' ] = [ 'scope' , 'category' , 'kind' , 'order' ]
247181
248182 Object . assign ( this , options )
249- if ( this . src ) this . fileSet = fileSet ( this . src )
183+ if ( this . src ) {
184+ this . fileSet = fileSet ( this . src )
185+ this . files = this . fileSet . files
186+ }
250187 }
251188
252- get invalidMessage ( ) {
189+ validate ( ) {
253190 if ( this . src ) {
254- if ( ! this . fileSet . files . length ) {
255- return '[jsdoc-parse] please specify valid input files.'
256- } else if ( this . fileSet . notExisting && this . fileSet . notExisting . length ) {
257- return 'These files do not exist: ' + this . fileSet . notExisting . join ( ', ' )
258- }
191+ assert . ok (
192+ this . fileSet . files . length ,
193+ 'Please specify valid input files.'
194+ )
195+ assert . ok (
196+ ! ( this . fileSet . notExisting && this . fileSet . notExisting . length ) ,
197+ 'These files do not exist: ' + this . fileSet . notExisting . join ( ', ' )
198+ )
259199 }
260200 }
261201}
0 commit comments