3
3
const fs = require ( 'fs' ) ;
4
4
const path = require ( 'path' ) ;
5
5
6
- const HANDLEBARS_PATTERN = / \. ( h b s | h a n d l e b a r s ) $ / ;
7
- const JAVASCRIPT_PATTERN = / \. ( j s ) $ / ;
6
+ const HANDLEBARS_PATTERN = / \. ( h b s | h a n d l e b a r s ) $ / u ;
7
+ const JAVASCRIPT_PATTERN = / \. ( j s ) $ / u ;
8
8
9
9
const NODE_MODULE_DIRECTORIES = [
10
10
path . join ( __dirname , '..' , 'node_modules' ) ,
11
11
path . join ( process . cwd ( ) , 'node_modules' )
12
12
] ;
13
13
14
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
- */
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
24
25
25
const findPackagePath = pkg =>
26
- Promise . all ( NODE_MODULE_DIRECTORIES . map ( dir => new Promise ( resolve => {
26
+ Promise . all ( NODE_MODULE_DIRECTORIES . map ( dir =>
27
+ new Promise ( resolve => {
27
28
28
- const filepath = path . join ( dir , pkg ) ;
29
+ const filepath = path . join ( dir , pkg ) ;
29
30
30
- fs . stat ( filepath , ( err , stats ) => {
31
+ fs . stat ( filepath , ( err , stats ) => {
31
32
32
- if ( ! err && stats . isDirectory ( ) ) {
33
+ if ( ! err && stats . isDirectory ( ) ) {
33
34
34
- return resolve ( filepath ) ;
35
+ return resolve ( filepath ) ;
35
36
36
- }
37
+ }
37
38
38
- return resolve ( null ) ;
39
+ return resolve ( null ) ;
39
40
40
- } ) ;
41
+ } ) ;
41
42
42
- } ) ) ) . then ( dirs => dirs . filter ( dir => dir ) ) ;
43
+ } ) ) ) . then ( dirs => dirs . filter ( dir => dir ) ) ;
43
44
44
45
/**
45
46
* Load parser based on user defined choice.
@@ -53,47 +54,52 @@ const findPackagePath = pkg =>
53
54
* @private
54
55
*/
55
56
56
- const loadParser = config => new Promise ( ( resolve , reject ) => {
57
+ const loadParser = config =>
58
+ new Promise ( ( resolve , reject ) => {
57
59
58
- fs . stat ( config . parser , ( err , stats ) => {
60
+ fs . stat ( config . parser , ( err , stats ) => {
59
61
60
- if ( err ) {
62
+ if ( err ) {
61
63
62
- findPackagePath ( `doxdox-parser-${ config . parser } ` ) . then ( parser => {
64
+ findPackagePath ( `doxdox-parser-${ config . parser } ` ) . then ( parser => {
63
65
64
- if ( parser . length ) {
66
+ if ( parser . length ) {
65
67
66
- resolve ( require ( parser [ 0 ] ) ) ;
68
+ resolve ( require ( parser [ 0 ] ) ) ;
67
69
68
- } else {
70
+ } else {
69
71
70
- reject ( new Error ( 'Invalid parser specified.' ) ) ;
72
+ reject ( new Error ( 'Invalid parser specified.' ) ) ;
71
73
72
- }
74
+ }
73
75
74
- } ) ;
76
+ } ) ;
75
77
76
- } else if ( stats && stats . isFile ( ) && config . parser . match ( JAVASCRIPT_PATTERN ) ) {
78
+ } else if (
79
+ stats &&
80
+ stats . isFile ( ) &&
81
+ config . parser . match ( JAVASCRIPT_PATTERN )
82
+ ) {
77
83
78
- if ( path . isAbsolute ( config . parser ) ) {
84
+ if ( path . isAbsolute ( config . parser ) ) {
79
85
80
- resolve ( require ( config . parser ) ) ;
86
+ resolve ( require ( config . parser ) ) ;
81
87
82
- } else {
88
+ } else {
83
89
84
- resolve ( require ( path . join ( process . cwd ( ) , config . parser ) ) ) ;
90
+ resolve ( require ( path . join ( process . cwd ( ) , config . parser ) ) ) ;
85
91
86
- }
92
+ }
87
93
88
- } else {
94
+ } else {
89
95
90
- reject ( new Error ( 'Invalid parser specified.' ) ) ;
96
+ reject ( new Error ( 'Invalid parser specified.' ) ) ;
91
97
92
- }
98
+ }
93
99
94
- } ) ;
100
+ } ) ;
95
101
96
- } ) ;
102
+ } ) ;
97
103
98
104
/**
99
105
* Load layout plugin based on user defined choice.
@@ -108,52 +114,61 @@ const loadParser = config => new Promise((resolve, reject) => {
108
114
* @private
109
115
*/
110
116
111
- const loadPlugin = config => new Promise ( ( resolve , reject ) => {
117
+ const loadPlugin = config =>
118
+ new Promise ( ( resolve , reject ) => {
112
119
113
- fs . stat ( config . layout , ( err , stats ) => {
120
+ fs . stat ( config . layout , ( err , stats ) => {
114
121
115
- if ( err ) {
122
+ if ( err ) {
116
123
117
- findPackagePath ( `doxdox-plugin-${ config . layout } ` ) . then ( plugin => {
124
+ findPackagePath ( `doxdox-plugin-${ config . layout } ` ) . then ( plugin => {
118
125
119
- if ( plugin . length ) {
126
+ if ( plugin . length ) {
120
127
121
- resolve ( require ( plugin [ 0 ] ) ) ;
128
+ resolve ( require ( plugin [ 0 ] ) ) ;
122
129
123
- } else {
130
+ } else {
124
131
125
- reject ( new Error ( 'Invalid layout specified.' ) ) ;
132
+ reject ( new Error ( 'Invalid layout specified.' ) ) ;
126
133
127
- }
134
+ }
128
135
129
- } ) ;
136
+ } ) ;
130
137
131
- } else if ( stats && stats . isFile ( ) && config . layout . match ( HANDLEBARS_PATTERN ) ) {
138
+ } else if (
139
+ stats &&
140
+ stats . isFile ( ) &&
141
+ config . layout . match ( HANDLEBARS_PATTERN )
142
+ ) {
132
143
133
- resolve ( require ( 'doxdox-plugin-handlebars' ) ) ;
144
+ resolve ( require ( 'doxdox-plugin-handlebars' ) ) ;
134
145
135
- } else if ( stats && stats . isFile ( ) && config . layout . match ( JAVASCRIPT_PATTERN ) ) {
146
+ } else if (
147
+ stats &&
148
+ stats . isFile ( ) &&
149
+ config . layout . match ( JAVASCRIPT_PATTERN )
150
+ ) {
136
151
137
- if ( path . isAbsolute ( config . layout ) ) {
152
+ if ( path . isAbsolute ( config . layout ) ) {
138
153
139
- resolve ( require ( config . layout ) ) ;
154
+ resolve ( require ( config . layout ) ) ;
140
155
141
- } else {
156
+ } else {
142
157
143
- resolve ( require ( path . join ( process . cwd ( ) , config . layout ) ) ) ;
158
+ resolve ( require ( path . join ( process . cwd ( ) , config . layout ) ) ) ;
144
159
145
- }
160
+ }
161
+
162
+ } else {
146
163
147
- } else {
164
+ reject ( new Error ( 'Invalid layout specified.' ) ) ;
148
165
149
- reject ( new Error ( 'Invalid layout specified.' ) ) ;
166
+ }
150
167
151
- }
168
+ } ) ;
152
169
153
170
} ) ;
154
171
155
- } ) ;
156
-
157
172
module . exports = {
158
173
findPackagePath,
159
174
loadParser,
0 commit comments