11'use strict' ;
22
3+ var _createClass = ( function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ) ( ) ;
4+
35function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
46
5- var cp = require ( 'child_process' ) ;
7+ function _typeof ( obj ) { return obj && typeof Symbol !== "undefined" && obj . constructor === Symbol ? "symbol" : typeof obj ; }
8+
69var path = require ( 'path' ) ;
7- var util = require ( 'util' ) ;
810var a = require ( 'array-tools' ) ;
9- var o = require ( 'object-tools' ) ;
10- var fs = require ( 'fs' ) ;
1111var fileSet = require ( 'file-set' ) ;
1212var Transform = require ( 'stream' ) . Transform ;
1313var cliOptions = require ( './cli-options' ) ;
14- var getTempPath = require ( 'temp-path' ) ;
15- var walkBack = require ( 'walk-back' ) ;
16-
17- function tempPath ( ) {
18- return getTempPath ( ) + 'jsdoc-parse.js' ;
19- }
14+ var jsdoc = require ( 'jsdoc-api' ) ;
15+ var transform = require ( './transform' ) ;
16+ var collectJson = require ( 'collect-json' ) ;
17+ var assert = require ( 'assert' ) ;
18+ var connect = require ( 'stream-connect' ) ;
2019
2120module . exports = jsdocParse ;
2221jsdocParse . cliOptions = cliOptions . definitions ;
2322
24- var ParseOptions = function ParseOptions ( ) {
25- _classCallCheck ( this , ParseOptions ) ;
26-
27- this . src = null ;
28-
29- this . private = false ;
30-
31- this . stats ;
32-
33- this . html = false ;
34-
35- this . conf = null ;
36-
37- this [ 'sort-by' ] = [ 'scope' , 'category' , 'kind' , 'order' ] ;
38- } ;
39-
4023function jsdocParse ( options ) {
41- options = o . extend ( new ParseOptions ( ) , options ) ;
42- var src = options . src ;
43-
44- if ( src ) {
45- var inputFiles = fileSet ( src ) ;
46- src = inputFiles . files ;
47-
48- var output = new OutputTransform ( options ) ;
49-
50- if ( ! src . length ) {
51- var msg = util . format ( '[jsdoc-parse] please specify valid input files. ' ) ;
52- if ( inputFiles . notExisting ) {
53- msg += 'These files do not exist: ' + inputFiles . notExisting . join ( ', ' ) ;
54- }
55-
24+ options = new ParseOptions ( options ) ;
25+ try {
26+ options . validate ( ) ;
27+ } catch ( err ) {
28+ var _ret = ( function ( ) {
29+ var output = new Transform ( ) ;
5630 process . nextTick ( function ( ) {
57- output . emit ( 'error' , new Error ( msg ) ) ;
58- } ) ;
59- } else {
60- getJsdocOutput ( src , options , function ( err , data ) {
61- if ( err ) {
62- output . emit ( 'error' , err ) ;
63- } else {
64- output . end ( data ) ;
65- }
66- } ) ;
67- }
68- return output ;
69- } else {
70- var inputStream = new Transform ( ) ;
71- var inputFilePath = tempPath ( ) ;
72-
73- var buf = new Buffer ( 0 ) ;
74- inputStream . _transform = function ( chunk , enc , done ) {
75- if ( chunk ) buf = Buffer . concat ( [ buf , chunk ] ) ;
76- done ( ) ;
77- } ;
78- inputStream . _flush = function ( done ) {
79- var self = this ;
80- fs . writeFileSync ( inputFilePath , buf ) ;
81- getJsdocOutput ( [ inputFilePath ] , options , function ( err , data ) {
82- if ( err ) {
83- done ( err ) ;
84- } else {
85- try {
86- data = applyOptions ( data , options ) ;
87- self . push ( data ) ;
88- self . push ( null ) ;
89- done ( ) ;
90- } catch ( err ) {
91- done ( err ) ;
92- }
93- }
94- fs . unlinkSync ( inputFilePath ) ;
31+ output . emit ( 'error' , err ) ;
9532 } ) ;
96- } ;
97- return inputStream ;
33+ return {
34+ v : output
35+ } ;
36+ } ) ( ) ;
37+
38+ if ( ( typeof _ret === 'undefined' ? 'undefined' : _typeof ( _ret ) ) === "object" ) return _ret . v ;
9839 }
99- }
10040
101- function OutputTransform ( options ) {
102- Transform . call ( this ) ;
103- this . json = new Buffer ( 0 ) ;
104- this . _transform = function ( chunk , enc , done ) {
105- if ( chunk ) this . json = Buffer . concat ( [ this . json , chunk ] ) ;
106- done ( ) ;
107- } ;
108- this . _flush = function ( ) {
109- try {
110- this . json = applyOptions ( this . json , options ) ;
111- } catch ( err ) {
112- err . message += ' [Problem parsing the JSON data output by jsdoc, input data: ' + this . json . toString ( ) . substr ( 0 , 100 ) + ']' ;
113- return this . emit ( 'error' , err ) ;
114- }
115- this . push ( this . json ) ;
116- this . push ( null ) ;
117- } ;
118- }
119- util . inherits ( OutputTransform , Transform ) ;
41+ var jsdocOptions = { } ;
42+ if ( options . html ) jsdocOptions . configure = path . resolve ( __dirname , 'html-conf.json' ) ;
43+ jsdocOptions . files = options . files ;
12044
121- function getJsdocOutput ( src , options , done ) {
122- var jsdocTemplatePath = __dirname ;
123- var jsdocPath = walkBack ( path . join ( __dirname , '..' ) , path . join ( 'node_modules' , 'jsdoc-75lb' , 'jsdoc.js' ) ) ;
45+ var explainStream = jsdoc . createExplainStream ( jsdocOptions ) . once ( 'error' , emitError ) ;
12446
125- if ( ! fs . existsSync ( jsdocPath ) ) {
126- throw Error ( 'jsdoc-parse: cannot find jsdoc: ' + jsdocPath ) ;
127- }
128- var args = [ jsdocPath , '--pedantic' , '-t' , jsdocTemplatePath ] ;
129- if ( options . html ) {
130- args = args . concat ( [ '-c' , path . resolve ( __dirname , 'default-conf.json' ) ] ) ;
131- } else if ( options . conf ) {
132- args = args . concat ( [ '-c ', path . resolve ( options . conf ) ] ) ;
47+ var transformedStream = transform ( ) . once ( 'error' , emitError ) ;
48+
49+ var outputStream = collectJson ( function ( data ) {
50+ return applyOptions ( data , options ) ;
51+ } ) . once ( 'error' , emitError ) ;
52+
53+ function emitError ( err ) {
54+ outputStream . emit ( 'error ', err ) ;
13355 }
134- args = args . concat ( src ) ;
135-
136- var outputFilePath = tempPath ( ) ;
137- var outputFile = fs . openSync ( outputFilePath , 'w' ) ;
138- var outputStderrPath = tempPath ( ) ;
139- var outputStderr = fs . openSync ( outputStderrPath , 'w' ) ;
140- var handle = cp . spawn ( 'node' , args , { stdio : [ process . stdin , outputFile , outputStderr ] } ) ;
141- handle . on ( 'error' , done ) ;
142- handle . on ( 'close' , function ( code ) {
143- var stderr = fs . readFileSync ( outputStderrPath , 'utf8' ) ;
144- var stdout = fs . readFileSync ( outputFilePath , 'utf8' ) ;
145- if ( / n o i n p u t f i l e s / . test ( stdout ) ) code = 1 ;
146-
147- if ( code ) {
148- fs . unlinkSync ( outputFilePath ) ;
149- fs . unlinkSync ( outputStderrPath ) ;
150- done ( new Error ( stderr || stdout ) ) ;
151- } else {
152- fs . unlinkSync ( outputFilePath ) ;
153- fs . unlinkSync ( outputStderrPath ) ;
154- done ( null , stdout ) ;
155- }
156- } ) ;
56+
57+ return connect ( explainStream , connect ( transformedStream , outputStream ) ) ;
15758}
15859
15960function applyOptions ( data , options ) {
160- data = JSON . parse ( data . toString ( ) ) ;
16161 if ( options . stats ) {
16262 return JSON . stringify ( getStats ( data ) , null , ' ' ) + '\n' ;
16363 } else {
@@ -198,4 +98,40 @@ function sort(array, sortBy) {
19898 } else {
19999 return a . sortBy ( array , sortBy , order ) ;
200100 }
201- }
101+ }
102+
103+ var ParseOptions = ( function ( ) {
104+ function ParseOptions ( options ) {
105+ _classCallCheck ( this , ParseOptions ) ;
106+
107+ this . src = null ;
108+
109+ this . private = false ;
110+
111+ this . stats ;
112+
113+ this . html = false ;
114+
115+ this . conf = null ;
116+
117+ this [ 'sort-by' ] = [ 'scope' , 'category' , 'kind' , 'order' ] ;
118+
119+ Object . assign ( this , options ) ;
120+ if ( this . src ) {
121+ this . fileSet = fileSet ( this . src ) ;
122+ this . files = this . fileSet . files ;
123+ }
124+ }
125+
126+ _createClass ( ParseOptions , [ {
127+ key : 'validate' ,
128+ value : function validate ( ) {
129+ if ( this . src ) {
130+ assert . ok ( this . fileSet . files . length , 'Please specify valid input files.' ) ;
131+ assert . ok ( ! ( this . fileSet . notExisting && this . fileSet . notExisting . length ) , 'These files do not exist: ' + this . fileSet . notExisting . join ( ', ' ) ) ;
132+ }
133+ }
134+ } ] ) ;
135+
136+ return ParseOptions ;
137+ } ) ( ) ;
0 commit comments