1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 |
|
6 | const HANDLEBARS_PATTERN = /\.(hbs|handlebars)$/u;
|
7 | const JAVASCRIPT_PATTERN = /\.(js)$/u;
|
8 |
|
9 | const NODE_MODULE_DIRECTORIES = [
|
10 | path.join(__dirname, '..', 'node_modules'),
|
11 | path.join(process.cwd(), 'node_modules')
|
12 | ];
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const 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 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | const 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 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | const 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 |
|
172 | module.exports = {
|
173 | findPackagePath,
|
174 | loadParser,
|
175 | loadPlugin
|
176 | };
|