Skip to content

Commit 89fb67f

Browse files
dignifiedquiretmcw
authored andcommitted
feat(build): load passed in config option (#625)
* feat(build): load passed in config option Fixes #documentationjs/gulp-documentation#27 * refactor(build): streamline loadConfig path
1 parent 363a108 commit 89fb67f

File tree

6 files changed

+85
-15
lines changed

6 files changed

+85
-15
lines changed

index.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ var fs = require('fs'),
2424
formatLint = require('./lib/lint').formatLint,
2525
garbageCollect = require('./lib/garbage_collect'),
2626
lintComments = require('./lib/lint').lintComments,
27-
markdownAST = require('./lib/output/markdown_ast');
27+
markdownAST = require('./lib/output/markdown_ast'),
28+
loadConfig = require('./lib/load_config');
2829

2930
/**
3031
* Build a pipeline of comment handlers.
@@ -64,6 +65,19 @@ function expandInputs(indexes, options, callback) {
6465
inputFn(indexes, options, callback);
6566
}
6667

68+
/**
69+
* Given an options object, it expands the `config` field
70+
* if it exists.
71+
*
72+
* @param {Object} options - options to process
73+
* @returns {undefined}
74+
*/
75+
function expandConfig(options) {
76+
if (options && typeof options.config === 'string') {
77+
Object.assign(options, loadConfig(options.config));
78+
}
79+
}
80+
6781
/**
6882
* Generate JavaScript documentation as a list of parsed JSDoc
6983
* comments, given a root file as a path.
@@ -114,6 +128,8 @@ function expandInputs(indexes, options, callback) {
114128
function build(indexes, options, callback) {
115129
options = options || {};
116130

131+
expandConfig(options);
132+
117133
if (typeof indexes === 'string') {
118134
indexes = [indexes];
119135
}
@@ -122,11 +138,14 @@ function build(indexes, options, callback) {
122138
if (error) {
123139
return callback(error);
124140
}
141+
142+
var result;
125143
try {
126-
callback(null, buildSync(inputs, options));
144+
result = buildSync(inputs, options);
127145
} catch (e) {
128-
callback(e);
146+
return callback(e);
129147
}
148+
callback(null, result);
130149
});
131150
}
132151

@@ -137,6 +156,7 @@ function build(indexes, options, callback) {
137156
*
138157
* @param {Array<string>} indexes files to process
139158
* @param {Object} options options
159+
* @param {string} config path to configuration file to load
140160
* @param {Array<string>} options.external a string regex / glob match pattern
141161
* that defines what external modules will be whitelisted and included in the
142162
* generated documentation.
@@ -172,6 +192,8 @@ function buildSync(indexes, options) {
172192
options = options || {};
173193
options.hljs = options.hljs || {};
174194

195+
expandConfig(options);
196+
175197
if (!options.access) {
176198
options.access = ['public', 'undefined', 'protected'];
177199
}
@@ -259,6 +281,8 @@ function buildSync(indexes, options) {
259281
function lint(indexes, options, callback) {
260282
options = options || {};
261283

284+
expandConfig(options);
285+
262286
if (typeof indexes === 'string') {
263287
indexes = [indexes];
264288
}

lib/commands/build.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,6 @@ module.exports.handler = function build(argv, callback) {
4949
if (argv.f === 'html' && argv.o === 'stdout') {
5050
throw new Error('The HTML output mode requires a destination directory set with -o');
5151
}
52-
var formatterOptions = {
53-
name: argv.name || (argv.package || {}).name,
54-
version: argv['project-version'] || (argv.package || {}).version,
55-
theme: argv.theme,
56-
paths: argv.paths,
57-
hljs: argv.hljs || {}
58-
};
5952

6053
var generator = documentation.build
6154
.bind(null, argv.input, argv, onDocumented);
@@ -68,6 +61,14 @@ module.exports.handler = function build(argv, callback) {
6861
throw err;
6962
}
7063

64+
var formatterOptions = {
65+
name: argv.name || (argv.package || {}).name,
66+
version: argv['project-version'] || (argv.package || {}).version,
67+
theme: argv.theme,
68+
paths: argv.paths,
69+
hljs: argv.hljs || {}
70+
};
71+
7172
documentation.formats[argv.format](comments, formatterOptions, onFormatted);
7273
}
7374

lib/commands/shared_options.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var path = require('path');
2-
var loadConfig = require('../load_config');
32

43
/**
54
* Adds shared options to any command that runs documentation
@@ -16,12 +15,9 @@ module.exports.sharedInputOptions = {
1615
type: 'boolean'
1716
},
1817
'config': {
19-
config: true,
2018
describe: 'configuration file. an array defining explicit sort order',
2119
alias: 'c',
22-
configParser: function (configPath) {
23-
return loadConfig(configPath);
24-
}
20+
type: 'string'
2521
},
2622
'external': {
2723
describe: 'a string / glob match pattern that defines which external ' +

test/fixture/class.config.output.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
2+
3+
# MyClass
4+
5+
This is my class, a demo thing.
6+
7+
**Properties**
8+
9+
- `howMany` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** how many things it contains
10+
11+
## getFoo
12+
13+
Get the number 42
14+
15+
**Parameters**
16+
17+
- `getIt` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether to get the number
18+
19+
Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** forty-two
20+
21+
## getUndefined
22+
23+
Get undefined
24+
25+
Returns **[undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)** does not return anything.
26+
27+
# Hello
28+
29+
World

test/fixture/simple.config.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
toc:
2+
- MyClass
3+
- name: Hello
4+
description: World

test/test.js

+16
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,22 @@ test('highlightAuto md output', function (t) {
250250
});
251251
});
252252

253+
test('config', function (t) {
254+
var file = path.join(__dirname, 'fixture', 'class.input.js');
255+
var result = fs.readFileSync(path.join(__dirname, 'fixture', 'class.config.output.md')).toString();
256+
documentation.build([file], {
257+
config: path.join(__dirname, 'fixture', 'simple.config.yml')
258+
}, function (err, out) {
259+
t.ifError(err);
260+
outputMarkdown(out, {}, function (err, md) {
261+
t.ifError(err);
262+
263+
t.equal(md, result, 'rendered markdown is equal');
264+
t.end();
265+
});
266+
});
267+
});
268+
253269
test('multi-file input', function (t) {
254270
documentation.build([
255271
path.join(__dirname, 'fixture', 'simple.input.js'),

0 commit comments

Comments
 (0)