Skip to content

Commit 5bf5de9

Browse files
committed
rewrite to use jsdoc-api
1 parent 62ba544 commit 5bf5de9

8 files changed

Lines changed: 338 additions & 419 deletions

File tree

bin/cli.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ if (options.help) {
2323

2424
var parseStream
2525
if (options.src && options.src.length) {
26-
parseStream = parse(options)
27-
parseStream
26+
parse(options)
2827
.on('error', function (err) {
2928
stop(err.stack, 1)
3029
})

es5/jsdoc-parse.js

Lines changed: 62 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,47 @@
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+
35
function _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+
79
var util = require('util');
810
var a = require('array-tools');
9-
var o = require('object-tools');
10-
var fs = require('fs');
1111
var fileSet = require('file-set');
1212
var Transform = require('stream').Transform;
1313
var 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

2118
module.exports = jsdocParse;
2219
jsdocParse.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-
4021
function 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

10147
function OutputTransform(options) {
@@ -118,46 +64,7 @@ function OutputTransform(options) {
11864
}
11965
util.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 (/no input files/.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-
15967
function 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+
})();

es5/transform.js

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,66 @@
33
var o = require('object-tools');
44
var a = require('array-tools');
55
var testValue = require('test-value');
6+
var collectJson = require('collect-json');
67

7-
var data;
8-
exports.createConstructor = createConstructor;
9-
exports.insertConstructors = insertConstructors;
10-
exports.fixConstructorMemberLongnames = fixConstructorMemberLongnames;
11-
exports.setIsExportedFlag = setIsExportedFlag;
12-
exports.setCodename = setCodename;
13-
exports.setID = setID;
14-
exports.updateIDReferences = updateIDReferences;
15-
16-
exports.setData = function (d) {
17-
data = d;return this;
18-
};
19-
exports.getData = function () {
20-
return data;
21-
};
22-
exports.removeQuotes = removeQuotes;
23-
exports.wantedProperties = wantedProperties;
24-
exports.removeUnwanted = removeUnwanted;
25-
exports.cleanProperties = cleanProperties;
26-
exports.buildTodoList = buildTodoList;
27-
exports.extractTypicalName = extractTypicalName;
28-
exports.extractCategory = extractCategory;
29-
exports.extractChainable = extractChainable;
30-
exports.extractCustomTags = extractCustomTags;
31-
exports.setTypedefScope = setTypedefScope;
32-
exports.sortIdentifier = sortIdentifier;
33-
exports.renameThisProperty = renameThisProperty;
34-
exports.removeMemberofFromModule = removeMemberofFromModule;
35-
exports.update = update;
8+
module.exports = transform;
9+
10+
function transform() {
11+
return collectJson(function (data) {
12+
data = fixConstructorMemberLongnames(data);
13+
14+
var json = data.filter(function (i) {
15+
return !i.undocumented && !/package|file/.test(i.kind);
16+
});
17+
18+
json = json.map(setIsExportedFlag);
19+
json = json.map(setCodename);
20+
json = insertConstructors(json);
21+
22+
json = json.map(function (identifier) {
23+
identifier = setID(identifier);
24+
25+
identifier = removeQuotes(identifier);
26+
identifier = cleanProperties(identifier);
27+
identifier = buildTodoList(identifier);
28+
identifier = extractTypicalName(identifier);
29+
identifier = extractCategory(identifier);
30+
identifier = extractChainable(identifier);
31+
identifier = extractCustomTags(identifier);
32+
identifier = setTypedefScope(identifier);
33+
identifier = renameThisProperty(identifier);
34+
identifier = removeMemberofFromModule(identifier);
35+
return identifier;
36+
});
37+
38+
var exported = a.where(json, { isExported: true });
39+
var newIDs = a.pluck(exported, 'id');
40+
41+
newIDs.forEach(function (newID) {
42+
update(json, { isExported: undefined, '!kind': 'module' }, function (identifier) {
43+
return updateIDReferences(identifier, newID);
44+
});
45+
});
46+
47+
json = json.filter(function (identifier) {
48+
var parent = a.findWhere(json, { id: identifier.memberof });
49+
if (parent && parent.isEnum) {
50+
return false;
51+
} else {
52+
return true;
53+
}
54+
});
55+
56+
json = json.map(removeUnwanted);
57+
json = json.map(sortIdentifier);
58+
59+
json.forEach(function (identifier, index) {
60+
identifier.order = index;
61+
});
62+
63+
return JSON.stringify(json, null, ' ');
64+
});
65+
}
3666

3767
function setID(identifier) {
3868
if (identifier.longname) {

0 commit comments

Comments
 (0)