UNPKG

4.08 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs');
4const path = require('path');
5
6const HANDLEBARS_PATTERN = /\.(hbs|handlebars)$/u;
7const JAVASCRIPT_PATTERN = /\.(js)$/u;
8
9const NODE_MODULE_DIRECTORIES = [
10 path.join(__dirname, '..', 'node_modules'),
11 path.join(process.cwd(), 'node_modules')
12];
13
14/**
15 * Find which node_modules directory to load package from.
16 *
17 * findPackagePath('doxdox-parser-dox').then(parser => {});
18 * findPackagePath('doxdox-plugin-bootstrap').then(plugin => {});
19 *
20 * @param {String} pkg Package name as string.
21 * @return {Object} Promise
22 * @private
23 */
24
25const findPackagePath = pkg =>
26 Promise.all(NODE_MODULE_DIRECTORIES.map(dir =>
27 new Promise(resolve => {
28
29 const filepath = path.join(dir, pkg);
30
31 fs.stat(filepath, (err, stats) => {
32
33 if (!err && stats.isDirectory()) {
34
35 return resolve(filepath);
36
37 }
38
39 return resolve(null);
40
41 });
42
43 }))).then(dirs => dirs.filter(dir => dir));
44
45/**
46 * Load parser based on user defined choice.
47 *
48 * loadParser({'parser': 'dox'}).then(parser => {});
49 * loadParser({'parser': 'parser.js'}).then(parser => {});
50 *
51 * @param {Object} config Configuration object.
52 * @param {String} config.parser String representing the parser to be loaded.
53 * @return {Object} Promise
54 * @private
55 */
56
57const loadParser = config =>
58 new Promise((resolve, reject) => {
59
60 fs.stat(config.parser, (err, stats) => {
61
62 if (err) {
63
64 findPackagePath(`doxdox-parser-${config.parser}`).then(parser => {
65
66 if (parser.length) {
67
68 resolve(require(parser[0]));
69
70 } else {
71
72 reject(new Error('Invalid parser specified.'));
73
74 }
75
76 });
77
78 } else if (
79 stats &&
80 stats.isFile() &&
81 config.parser.match(JAVASCRIPT_PATTERN)
82 ) {
83
84 if (path.isAbsolute(config.parser)) {
85
86 resolve(require(config.parser));
87
88 } else {
89
90 resolve(require(path.join(process.cwd(), config.parser)));
91
92 }
93
94 } else {
95
96 reject(new Error('Invalid parser specified.'));
97
98 }
99
100 });
101
102 });
103
104/**
105 * Load layout plugin based on user defined choice.
106 *
107 * loadPlugin({'layout': 'markdown'}).then(plugin => {});
108 * loadPlugin({'layout': 'templates/README.hbs'}).then(plugin => {});
109 * loadPlugin({'layout': 'plugin.js'}).then(plugin => {});
110 *
111 * @param {Object} config Configuration object.
112 * @param {String} config.layout String representing the layout plugin to be loaded.
113 * @return {Object} Promise
114 * @private
115 */
116
117const loadPlugin = config =>
118 new Promise((resolve, reject) => {
119
120 fs.stat(config.layout, (err, stats) => {
121
122 if (err) {
123
124 findPackagePath(`doxdox-plugin-${config.layout}`).then(plugin => {
125
126 if (plugin.length) {
127
128 resolve(require(plugin[0]));
129
130 } else {
131
132 reject(new Error('Invalid layout specified.'));
133
134 }
135
136 });
137
138 } else if (
139 stats &&
140 stats.isFile() &&
141 config.layout.match(HANDLEBARS_PATTERN)
142 ) {
143
144 resolve(require('doxdox-plugin-handlebars'));
145
146 } else if (
147 stats &&
148 stats.isFile() &&
149 config.layout.match(JAVASCRIPT_PATTERN)
150 ) {
151
152 if (path.isAbsolute(config.layout)) {
153
154 resolve(require(config.layout));
155
156 } else {
157
158 resolve(require(path.join(process.cwd(), config.layout)));
159
160 }
161
162 } else {
163
164 reject(new Error('Invalid layout specified.'));
165
166 }
167
168 });
169
170 });
171
172module.exports = {
173 findPackagePath,
174 loadParser,
175 loadPlugin
176};