Skip to content

Commit 48b8c6b

Browse files
committed
Expose a sync interface: buildSync
Fixes #389
1 parent d858eca commit 48b8c6b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+115
-29
lines changed

index.js

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
var sort = require('./lib/sort'),
3+
var fs = require('fs'),
4+
sort = require('./lib/sort'),
45
nest = require('./lib/nest'),
56
filterAccess = require('./lib/filter_access'),
67
filterJS = require('./lib/filter_js'),
@@ -81,50 +82,86 @@ function expandInputs(indexes, options, callback) {
8182
*/
8283
module.exports.build = function (indexes, options, callback) {
8384
options = options || {};
84-
options.hljs = options.hljs || {};
8585

8686
if (typeof indexes === 'string') {
8787
indexes = [indexes];
8888
}
8989

90-
if (!options.access) {
91-
options.access = ['public', 'undefined', 'protected'];
92-
}
93-
94-
var parseFn = (options.polyglot) ? polyglot : parseJavaScript;
95-
9690
return expandInputs(indexes, options, function (error, inputs) {
9791
if (error) {
9892
return callback(error);
9993
}
10094
try {
101-
callback(null,
102-
filterAccess(options.access,
103-
hierarchy(
104-
sort(
105-
inputs
106-
.filter(filterJS(options.extension, options.polyglot))
107-
.reduce(function (memo, file) {
108-
return memo.concat(parseFn(file));
109-
}, [])
110-
.map(pipeline(
111-
inferName(),
112-
inferAugments(),
113-
inferKind(),
114-
inferParams(),
115-
inferProperties(),
116-
inferReturn(),
117-
inferMembership(),
118-
nest,
119-
options.github && github
120-
))
121-
.filter(Boolean), options))));
95+
callback(null, buildSync(inputs, options));
12296
} catch (e) {
12397
callback(e);
12498
}
12599
});
126100
};
127101

102+
/**
103+
* Generate JavaScript documentation given a list of inputs. This internal
104+
* method does not support require-following and it returns its results
105+
* synchronously, rather than by calling a callback.
106+
*
107+
* @param {Array<string>} indexes files to process
108+
* @param {Object} options options
109+
* @param {Array<string>} options.external a string regex / glob match pattern
110+
* that defines what external modules will be whitelisted and included in the
111+
* generated documentation.
112+
* @param {boolean} [options.polyglot=false] parse comments with a regex rather than
113+
* a proper parser. This enables support of non-JavaScript languages but
114+
* reduces documentation's ability to infer structure of code.
115+
* @param {boolean} [options.shallow=false] whether to avoid dependency parsing
116+
* even in JavaScript code. With the polyglot option set, this has no effect.
117+
* @param {Array<string|Object>} [options.order=[]] optional array that
118+
* defines sorting order of documentation
119+
* @param {Array<string>} [options.access=[]] an array of access levels
120+
* to output in documentation
121+
* @param {Object} [options.hljs] hljs optional options
122+
* @param {boolean} [options.hljs.highlightAuto=false] hljs automatically detect language
123+
* @param {Array} [options.hljs.languages] languages for hljs to choose from
124+
* @returns {Object} list of results
125+
* @public
126+
*/
127+
function buildSync(indexes, options) {
128+
options = options || {};
129+
options.hljs = options.hljs || {};
130+
131+
if (!options.access) {
132+
options.access = ['public', 'undefined', 'protected'];
133+
}
134+
135+
var parseFn = (options.polyglot) ? polyglot : parseJavaScript;
136+
137+
return filterAccess(options.access,
138+
hierarchy(
139+
sort(indexes.map(function (index) {
140+
if (typeof index === 'string') {
141+
return {
142+
source: fs.readFileSync(index, 'utf8'),
143+
file: index
144+
};
145+
}
146+
return index;
147+
}).filter(filterJS(options.extension, options.polyglot))
148+
.reduce(function (memo, file) {
149+
return memo.concat(parseFn(file));
150+
}, [])
151+
.map(pipeline(
152+
inferName(),
153+
inferAugments(),
154+
inferKind(),
155+
inferParams(),
156+
inferProperties(),
157+
inferReturn(),
158+
inferMembership(),
159+
nest,
160+
options.github && github
161+
))
162+
.filter(Boolean), options)));
163+
}
164+
128165
/**
129166
* Lint files for non-standard or incorrect documentation
130167
* information, returning a potentially-empty string
@@ -180,6 +217,7 @@ module.exports.lint = function lint(indexes, options, callback) {
180217
};
181218

182219
module.exports.expandInputs = expandInputs;
220+
module.exports.buildSync = buildSync;
183221

184222
module.exports.formats = {
185223
html: require('./lib/output/html'),
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)