Skip to content

Commit 3d3dfd9

Browse files
committed
Added local plugin resolution.
Improved logic in findPackagePath method. Added test.
1 parent aebaff0 commit 3d3dfd9

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

lib/loaders.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fs = require('fs');
44
const path = require('path');
55

66
const HANDLEBARS_REGEX = /\.(hbs|handlebars)$/;
7+
const JAVASCRIPT_REGEX = /\.(js)$/;
78

89
const NODE_MODULE_DIRECTORIES = [
910
path.join(__dirname, '../node_modules'),
@@ -28,13 +29,7 @@ const findPackagePath = pkg =>
2829

2930
fs.stat(path, (err, stats) => {
3031

31-
if (err) {
32-
33-
return resolve(null);
34-
35-
}
36-
37-
if (stats.isDirectory()) {
32+
if (!err && stats.isDirectory()) {
3833

3934
return resolve(path);
4035

@@ -80,6 +75,7 @@ const loadParser = config => new Promise((resolve, reject) => {
8075
*
8176
* loadPlugin({'layout': 'markdown'}).then(plugin => {});
8277
* loadPlugin({'layout': 'templates/README.hbs'}).then(plugin => {});
78+
* loadPlugin({'layout': 'plugin.js'}).then(plugin => {});
8379
*
8480
* @param {Object} config Configuration object.
8581
* @param {String} config.layout String representing the layout plugin to be loaded.
@@ -111,6 +107,10 @@ const loadPlugin = config => new Promise((resolve, reject) => {
111107

112108
resolve(require('doxdox-plugin-handlebars'));
113109

110+
} else if (stats && stats.isFile() && config.layout.match(JAVASCRIPT_REGEX)) {
111+
112+
resolve(require(`${process.cwd()}/${config.layout}`));
113+
114114
} else {
115115

116116
reject('Invalid layout specified.');

test/fixtures/plugin.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const Handlebars = require('handlebars');
5+
6+
/**
7+
* Test plugin for doxdox.
8+
*
9+
* @example parseInputs(inputs, {'parser': 'dox', 'layout': 'plugins.js'}).then(content => console.log(content));
10+
* @param {Array} data Methods parsed using a doxdox parser.
11+
* @return {Promise} Promise with generated content.
12+
* @public
13+
*/
14+
15+
const plugin = data => new Promise((resolve, reject) => {
16+
17+
fs.readFile(path.join(__dirname, 'template.hbs'), 'utf8', (err, contents) => {
18+
19+
if (err) {
20+
21+
return reject(err);
22+
23+
}
24+
25+
const template = Handlebars.compile(contents);
26+
27+
return resolve(template(data));
28+
29+
});
30+
31+
});
32+
33+
module.exports = plugin;

test/specs/loaders.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ describe('loaders', () => {
7777

7878
}));
7979

80+
it('load custom plugin via JavaScript file', () =>
81+
loaders.loadPlugin({'layout': './test/fixtures/plugin.js'}));
82+
8083
});
8184

8285
});

0 commit comments

Comments
 (0)