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' ) ;
6- var path = require ( 'path' ) ;
7+ function _typeof ( obj ) { return obj && typeof Symbol !== "undefined" && obj . constructor === Symbol ? "symbol" : typeof obj ; }
8+
79var 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' ) ;
2017
2118module . exports = jsdocParse ;
2219jsdocParse . cliOptions = cliOptions . definitions ;
2320
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-
4021function 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- }
22+ options = new ParseOptions ( options ) ;
5523
24+ if ( options . invalidMessage ) {
25+ var _ret = ( function ( ) {
26+ var output = new Transform ( ) ;
5627 process . nextTick ( function ( ) {
57- output . emit ( 'error' , new Error ( msg ) ) ;
28+ output . emit ( 'error' , new Error ( options . invalidMessage ) ) ;
5829 } ) ;
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 ;
30+ return {
31+ v : output
32+ } ;
33+ } ) ( ) ;
34+
35+ if ( ( typeof _ret === 'undefined' ? 'undefined' : _typeof ( _ret ) ) === "object" ) return _ret . v ;
6936 } 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 ) ;
95- } ) ;
96- } ;
97- return inputStream ;
37+ if ( options . src ) {
38+ return jsdoc . createExplainStream ( options . fileSet . files ) . pipe ( transform ( ) ) . pipe ( collectJson ( function ( data ) {
39+ return applyOptions ( data , options ) ;
40+ } ) ) ;
41+ }
9842 }
43+
44+ return ;
9945}
10046
10147function OutputTransform ( options ) {
@@ -118,46 +64,7 @@ function OutputTransform(options) {
11864}
11965util . inherits ( OutputTransform , Transform ) ;
12066
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' ) ) ;
124-
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 ) ] ) ;
133- }
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- } ) ;
157- }
158-
15967function applyOptions ( data , options ) {
160- data = JSON . parse ( data . toString ( ) ) ;
16168 if ( options . stats ) {
16269 return JSON . stringify ( getStats ( data ) , null , ' ' ) + '\n' ;
16370 } else {
@@ -198,4 +105,40 @@ function sort(array, sortBy) {
198105 } else {
199106 return a . sortBy ( array , sortBy , order ) ;
200107 }
201- }
108+ }
109+
110+ var ParseOptions = ( function ( ) {
111+ function ParseOptions ( options ) {
112+ _classCallCheck ( this , ParseOptions ) ;
113+
114+ this . src = null ;
115+
116+ this . private = false ;
117+
118+ this . stats ;
119+
120+ this . html = false ;
121+
122+ this . conf = null ;
123+
124+ this [ 'sort-by' ] = [ 'scope' , 'category' , 'kind' , 'order' ] ;
125+
126+ Object . assign ( this , options ) ;
127+ if ( this . src ) this . fileSet = fileSet ( this . src ) ;
128+ }
129+
130+ _createClass ( ParseOptions , [ {
131+ key : 'invalidMessage' ,
132+ get : function get ( ) {
133+ if ( this . src ) {
134+ if ( ! this . fileSet . files . length ) {
135+ return '[jsdoc-parse] please specify valid input files.' ;
136+ } else if ( this . fileSet . notExisting && this . fileSet . notExisting . length ) {
137+ return 'These files do not exist: ' + this . fileSet . notExisting . join ( ', ' ) ;
138+ }
139+ }
140+ }
141+ } ] ) ;
142+
143+ return ParseOptions ;
144+ } ) ( ) ;
0 commit comments