@@ -6,6 +6,7 @@ const chalk = require('chalk');
6
6
const engineMatcher = / ^ p a t t e r n e n g i n e - n o d e - ( .* ) $ / ;
7
7
const scopeMatch = / ^ @ ( .* ) $ / ;
8
8
const isDir = fPath => lstatSync ( fPath ) . isDirectory ( ) ;
9
+ const logger = require ( './log' ) ;
9
10
10
11
const enginesDirectories = [ {
11
12
displayName : 'the core' ,
@@ -20,7 +21,7 @@ const enginesDirectories = [{
20
21
function isEngineModule ( filePath ) {
21
22
const baseName = path . basename ( filePath ) ;
22
23
const engineMatch = baseName . match ( engineMatcher ) ;
23
-
24
+
24
25
if ( engineMatch ) { return engineMatch [ 1 ] ; }
25
26
return false ;
26
27
}
@@ -43,12 +44,12 @@ function isScopedPackage(filePath) {
43
44
* @return {Array<Engine> } An array of engine objects
44
45
*/
45
46
function resolveEngines ( dir ) {
46
-
47
+
47
48
// Guard against non-existent directories.
48
49
if ( ! existsSync ( dir ) ) {
49
50
return [ ] ; // Silence is golden …
50
51
}
51
-
52
+
52
53
/**
53
54
* @name walk
54
55
* @desc Traverse the given path and gather possible engines
@@ -57,14 +58,14 @@ function resolveEngines(dir) {
57
58
* @return {Array<Engine> } - The final array of engines
58
59
*/
59
60
const walk = ( fPath , engines ) => {
60
-
61
+
61
62
/**
62
63
* @name dirList
63
64
* @desc A list of all directories in the given path
64
65
* @type {Array<string> }
65
66
*/
66
67
const dirList = readdirSync ( fPath ) . filter ( p => isDir ( path . join ( fPath , p ) ) ) ;
67
-
68
+
68
69
/**
69
70
* @name e
70
71
* @desc For the current dir get all engines
@@ -79,7 +80,7 @@ function resolveEngines(dir) {
79
80
}
80
81
} )
81
82
) ;
82
-
83
+
83
84
/**
84
85
* 1. Flatten all engines from inner recursions and current dir
85
86
* 2. Filter the dirList for scoped packages
@@ -92,7 +93,7 @@ function resolveEngines(dir) {
92
93
. map ( scope => walk ( path . join ( fPath , scope ) , e ) ) // 3
93
94
) ;
94
95
} ;
95
-
96
+
96
97
return walk ( dir , [ ] ) ;
97
98
}
98
99
@@ -117,27 +118,23 @@ function findEngineModulesInDirectory(dir) {
117
118
// methods and properites below should therefore be on its prototype.
118
119
119
120
const PatternEngines = Object . create ( {
120
-
121
+
121
122
loadAllEngines : function ( patternLabConfig ) {
122
123
var self = this ;
123
-
124
+
124
125
// Try to load engines! We scan for engines at each path specified above. This
125
126
// function is kind of a big deal.
126
127
enginesDirectories . forEach ( function ( engineDirectory ) {
127
128
const enginesInThisDir = findEngineModulesInDirectory ( engineDirectory . path ) ;
128
- if ( patternLabConfig . debug ) {
129
- console . log ( chalk . bold ( `Loading engines from ${ engineDirectory . displayName } ...\n` ) ) ;
130
- }
131
-
129
+
130
+ logger . debug ( `Loading engines from ${ engineDirectory . displayName } ...\n` ) ;
131
+
132
132
// find all engine-named things in this directory and try to load them,
133
133
// unless it's already been loaded.
134
134
enginesInThisDir . forEach ( function ( engineDiscovery ) {
135
135
let errorMessage ;
136
136
const successMessage = "good to go" ;
137
- if ( patternLabConfig . debug ) {
138
- chalk . green ( successMessage ) ;
139
- }
140
-
137
+
141
138
try {
142
139
// Give it a try! load 'er up. But not if we already have,
143
140
// of course. Also pass the pattern lab config object into
@@ -157,23 +154,18 @@ const PatternEngines = Object.create({
157
154
errorMessage = err . message ;
158
155
} finally {
159
156
// report on the status of the engine, one way or another!
160
- if ( patternLabConfig . debug ) {
161
- console . log ( ` ${ engineDiscovery . name } :` , errorMessage ? chalk . red ( errorMessage ) : successMessage ) ;
162
- }
157
+ logger . info ( `Pattern Engine ${ engineDiscovery . name } : ${ errorMessage ? errorMessage : successMessage } ` ) ;
163
158
}
164
159
} ) ;
165
- console . log ( '' ) ;
166
160
} ) ;
167
-
161
+
168
162
// Complain if for some reason we haven't loaded any engines.
169
163
if ( Object . keys ( self ) . length === 0 ) {
170
- throw new Error ( 'No engines loaded! Something is seriously wrong.' ) ;
171
- }
172
- if ( patternLabConfig . debug ) {
173
- console . log ( chalk . bold ( 'Done loading engines.\n' ) ) ;
164
+ logger . error ( 'No engines loaded! Something is seriously wrong.' ) ;
174
165
}
166
+ logger . debug ( `Done loading engines` ) ;
175
167
} ,
176
-
168
+
177
169
getEngineNameForPattern : function ( pattern ) {
178
170
// avoid circular dependency by putting this in here. TODO: is this slow?
179
171
const of = require ( './object_factory' ) ;
@@ -182,7 +174,7 @@ const PatternEngines = Object.create({
182
174
const engineNames = Object . keys ( this ) ;
183
175
for ( let i = 0 ; i < engineNames . length ; i ++ ) {
184
176
const engine = this [ engineNames [ i ] ] ;
185
-
177
+
186
178
if ( Array . isArray ( engine . engineFileExtension ) ) {
187
179
if ( engine . engineFileExtension . includes ( pattern . fileExtension ) ) {
188
180
return engine . engineName ;
@@ -195,12 +187,12 @@ const PatternEngines = Object.create({
195
187
}
196
188
}
197
189
}
198
-
190
+
199
191
// otherwise, assume it's a plain mustache template string and act
200
192
// accordingly
201
193
return 'mustache' ;
202
194
} ,
203
-
195
+
204
196
getEngineForPattern : function ( pattern ) {
205
197
if ( pattern . isPseudoPattern ) {
206
198
return this . getEngineForPattern ( pattern . basePattern ) ;
@@ -209,7 +201,7 @@ const PatternEngines = Object.create({
209
201
return this [ engineName ] ;
210
202
}
211
203
} ,
212
-
204
+
213
205
// combine all found engines into a single array of supported extensions
214
206
getSupportedFileExtensions : function ( ) {
215
207
const engineNames = Object . keys ( PatternEngines ) ;
@@ -218,19 +210,19 @@ const PatternEngines = Object.create({
218
210
} ) ;
219
211
return [ ] . concat . apply ( [ ] , allEnginesExtensions ) ;
220
212
} ,
221
-
213
+
222
214
isFileExtensionSupported : function ( fileExtension ) {
223
215
const supportedExtensions = PatternEngines . getSupportedFileExtensions ( ) ;
224
216
return ( supportedExtensions . lastIndexOf ( fileExtension ) !== - 1 ) ;
225
217
} ,
226
-
218
+
227
219
// given a filename, return a boolean: whether or not the filename indicates
228
220
// that the file is pseudopattern JSON
229
221
isPseudoPatternJSON : function ( filename ) {
230
222
const extension = path . extname ( filename ) ;
231
223
return ( extension === '.json' && filename . indexOf ( '~' ) > - 1 ) ;
232
224
} ,
233
-
225
+
234
226
// takes a filename string, not a full path; a basename (plus extension)
235
227
// ignore _underscored patterns, dotfiles, and anything not recognized by a
236
228
// loaded pattern engine. Pseudo-pattern .json files ARE considered to be
@@ -242,7 +234,7 @@ const PatternEngines = Object.create({
242
234
( extension === '.json' && ! PatternEngines . isPseudoPatternJSON ( filename ) ) ) {
243
235
return false ;
244
236
}
245
-
237
+
246
238
// not a hidden pattern, let's dig deeper
247
239
const supportedPatternFileExtensions = PatternEngines . getSupportedFileExtensions ( ) ;
248
240
return ( supportedPatternFileExtensions . lastIndexOf ( extension ) !== - 1 ||
0 commit comments