Skip to content

Commit 24ef88e

Browse files
committed
refactor to use jsdoc-api
1 parent 5bf5de9 commit 24ef88e

5 files changed

Lines changed: 77 additions & 148 deletions

File tree

bin/cli.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,17 @@ try {
1717
}
1818

1919
if (options.help) {
20-
console.log(usage)
20+
console.error(usage)
2121
process.exit(0)
2222
}
2323

24-
var parseStream
25-
if (options.src && options.src.length) {
26-
parse(options)
27-
.on('error', function (err) {
28-
stop(err.stack, 1)
29-
})
30-
.pipe(process.stdout)
31-
} else {
32-
parseStream = parse(options)
33-
parseStream.on('error', function (err) {
24+
const parseStream = parse(options)
25+
.on('error', function (err) {
3426
stop(err.stack, 1)
3527
})
36-
process.stdin.pipe(parseStream).pipe(process.stdout)
37-
}
28+
parseStream.pipe(process.stdout)
29+
30+
if (!options.src) process.stdin.pipe(parseStream)
3831

3932
function stop (msg, code) {
4033
console.error(ansi.format(msg, 'red'))

es5/jsdoc-parse.js

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,56 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
77
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
88

99
var util = require('util');
10+
var path = require('path');
1011
var a = require('array-tools');
1112
var fileSet = require('file-set');
1213
var Transform = require('stream').Transform;
1314
var cliOptions = require('./cli-options');
1415
var jsdoc = require('jsdoc-api');
1516
var transform = require('./transform');
1617
var collectJson = require('collect-json');
18+
var assert = require('assert');
19+
var connect = require('stream-connect');
1720

1821
module.exports = jsdocParse;
1922
jsdocParse.cliOptions = cliOptions.definitions;
2023

2124
function jsdocParse(options) {
2225
options = new ParseOptions(options);
23-
24-
if (options.invalidMessage) {
26+
try {
27+
options.validate();
28+
} catch (err) {
2529
var _ret = (function () {
2630
var output = new Transform();
2731
process.nextTick(function () {
28-
output.emit('error', new Error(options.invalidMessage));
32+
output.emit('error', err);
2933
});
3034
return {
3135
v: output
3236
};
3337
})();
3438

3539
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
36-
} else {
37-
if (options.src) {
38-
return jsdoc.createExplainStream(options.fileSet.files).pipe(transform()).pipe(collectJson(function (data) {
39-
return applyOptions(data, options);
40-
}));
41-
}
4240
}
4341

44-
return;
45-
}
42+
var jsdocOptions = {};
43+
if (options.html) jsdocOptions.configure = path.resolve(__dirname, 'html-conf.json');
44+
jsdocOptions.files = options.files;
4645

47-
function OutputTransform(options) {
48-
Transform.call(this);
49-
this.json = new Buffer(0);
50-
this._transform = function (chunk, enc, done) {
51-
if (chunk) this.json = Buffer.concat([this.json, chunk]);
52-
done();
53-
};
54-
this._flush = function () {
55-
try {
56-
this.json = applyOptions(this.json, options);
57-
} catch (err) {
58-
err.message += ' [Problem parsing the JSON data output by jsdoc, input data: ' + this.json.toString().substr(0, 100) + ']';
59-
return this.emit('error', err);
60-
}
61-
this.push(this.json);
62-
this.push(null);
63-
};
46+
var explainStream = jsdoc.createExplainStream(jsdocOptions).once('error', emitError);
47+
48+
var transformedStream = transform().once('error', emitError);
49+
50+
var outputStream = collectJson(function (data) {
51+
return applyOptions(data, options);
52+
}).once('error', emitError);
53+
54+
function emitError(err) {
55+
outputStream.emit('error', err);
56+
}
57+
58+
return connect(explainStream, connect(transformedStream, outputStream));
6459
}
65-
util.inherits(OutputTransform, Transform);
6660

6761
function applyOptions(data, options) {
6862
if (options.stats) {
@@ -124,18 +118,18 @@ var ParseOptions = (function () {
124118
this['sort-by'] = ['scope', 'category', 'kind', 'order'];
125119

126120
Object.assign(this, options);
127-
if (this.src) this.fileSet = fileSet(this.src);
121+
if (this.src) {
122+
this.fileSet = fileSet(this.src);
123+
this.files = this.fileSet.files;
124+
}
128125
}
129126

130127
_createClass(ParseOptions, [{
131-
key: 'invalidMessage',
132-
get: function get() {
128+
key: 'validate',
129+
value: function validate() {
133130
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-
}
131+
assert.ok(this.fileSet.files.length, 'Please specify valid input files.');
132+
assert.ok(!(this.fileSet.notExisting && this.fileSet.notExisting.length), 'These files do not exist: ' + this.fileSet.notExisting.join(', '));
139133
}
140134
}
141135
}]);

lib/jsdoc-parse.js

Lines changed: 37 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const cliOptions = require('./cli-options')
88
const jsdoc = require('jsdoc-api')
99
const transform = require('./transform')
1010
const 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
*/
3335
function 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
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"file-set": "~0.2.1",
3434
"jsdoc-api": "~0.1.0",
3535
"object-tools": "^2",
36+
"stream-connect": "^0.2.2",
3637
"test-value": "^1.0.1"
3738
},
3839
"devDependencies": {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ var expected = {
3030
setCodename: require('./transforms/expected/setCodename')
3131
}
3232

33-
test('setID', function (t) {
33+
test.only('setID', function (t) {
34+
console.log(fixture.setID.map)
3435
t.deepEqual(fixture.setID.map(transform.setID), expected.setID)
3536
t.end()
3637
})

0 commit comments

Comments
 (0)