13
13
var path = require ( 'path' ) ;
14
14
var diveSync = require ( 'diveSync' ) ;
15
15
var engineMatcher = / ^ p a t t e r n e n g i n e - n o d e - ( .* ) $ / ;
16
- var enginesDirectory = path . join ( process . cwd ( ) , 'node_modules' ) ;
16
+ var enginesDirectories = [
17
+ {
18
+ displayName : 'the core' ,
19
+ path : path . resolve ( __dirname , '..' , '..' , 'node_modules' )
20
+ } ,
21
+ {
22
+ displayName : 'the edition or test directory' ,
23
+ path : path . join ( process . cwd ( ) , 'node_modules' )
24
+ }
25
+ ] ;
17
26
var PatternEngines ; // the main export object
18
27
var engineNameForExtension ; // generated mapping of extension to engine name
19
28
20
29
21
30
// free "private" functions, for internal setup only
22
31
23
- // given a path: return the engine name is the path points to a valid engine
32
+ // given a path: return the engine name if the path points to a valid engine
24
33
// module directory, or false if it doesn't
25
34
function isEngineModule ( filePath ) {
26
35
var baseName = path . basename ( filePath ) ;
@@ -30,54 +39,64 @@ function isEngineModule(filePath) {
30
39
return false ;
31
40
}
32
41
33
- function getEngineModulePath ( engineName ) {
34
- return path . resolve ( enginesDirectory , "patternengine-node-" + engineName ) ;
35
- }
36
-
37
- // go find the names of all found engines
38
- function findSupportedPatternEngineNames ( ) {
39
- var foundPatternEngineNames = [ ] ;
42
+ function findEngineModulesInDirectory ( dir ) {
43
+ var foundEngines = [ ] ;
40
44
41
- // iterate over module directory and build a list of available pattern engine
42
- // names
43
- diveSync ( enginesDirectory , {
45
+ diveSync ( dir , {
44
46
recursive : false ,
45
47
directories : true
46
48
} , function ( err , filePath ) {
47
49
if ( err ) { throw err ; }
48
50
var foundEngineName = isEngineModule ( filePath ) ;
49
51
if ( foundEngineName ) {
50
- foundPatternEngineNames . push ( foundEngineName ) ;
52
+ foundEngines . push ( {
53
+ name : foundEngineName ,
54
+ modulePath : filePath
55
+ } ) ;
51
56
}
52
57
} ) ;
53
58
54
- return foundPatternEngineNames ;
59
+ return foundEngines ;
55
60
}
56
61
57
-
58
- // try to load all supported engines
62
+ // Try to load engines! We scan for engines at each path specified above. This
63
+ // function is kind of a big deal.
59
64
function loadAllEngines ( enginesObject ) {
60
65
console . log ( '\nLoading engines...' ) ;
61
66
62
- enginesObject . supportedPatternEngineNames . forEach ( function ( engineName ) {
63
- var notLoaded = false ;
64
-
65
- try {
66
- enginesObject [ engineName ] = require ( getEngineModulePath ( engineName ) ) ;
67
- } catch ( err ) {
68
- // Handle errors loading each pattern engine. This will usually be
69
- // because the engine's renderer hasn't been installed by the end user
70
- // -- we don't include any of them (except mustache) by default as
71
- // depedencies in package.json.
72
- notLoaded = ( err . code === 'MODULE_NOT_FOUND' ) ;
73
- } finally {
74
- console . log ( '-' , engineName , 'engine:' ,
75
- notLoaded ? 'renderer not installed; engine disabled' : 'good to go' ) ;
76
- }
67
+ enginesDirectories . forEach ( function ( engineDirectory ) {
68
+ var enginesInThisDir = findEngineModulesInDirectory ( engineDirectory . path ) ;
69
+ console . log ( "...scanning for engines in" , engineDirectory . displayName + "..." ) ;
70
+
71
+ // find all engine-named things in this directory and try to load them,
72
+ // unless it's already been loaded.
73
+ enginesInThisDir . forEach ( function ( engineDiscovery ) {
74
+ var errorMessage ;
75
+ var successMessage = "good to go" ;
76
+
77
+ try {
78
+ // give it a try! load 'er up. But not if we already have, of course.
79
+ if ( enginesObject [ engineDiscovery . name ] ) {
80
+ throw new Error ( "already loaded, skipping." ) ;
81
+ }
82
+ enginesObject [ engineDiscovery . name ] = require ( engineDiscovery . modulePath ) ;
83
+ } catch ( err ) {
84
+ errorMessage = err . message ;
85
+ } finally {
86
+ // report on the status of the engine, one way or another!
87
+ console . log ( '-' , engineDiscovery . name , 'engine:' , errorMessage ? errorMessage : successMessage ) ;
88
+ }
89
+ } ) ;
77
90
} ) ;
91
+
92
+ // Complain if for some reason we haven't loaded any engines.
93
+ if ( Object . keys ( enginesObject ) . length === 0 ) {
94
+ throw new Error ( 'No engines loaded! Something is seriously wrong.' ) ;
95
+ }
78
96
console . log ( '...done loading engines.\n' ) ;
79
97
}
80
98
99
+
81
100
// produce a mapping between file extension and engine name for each of the
82
101
// loaded engines
83
102
function createFileExtensionToEngineNameMap ( enginesObject ) {
@@ -108,10 +127,6 @@ function createFileExtensionToEngineNameMap(enginesObject) {
108
127
// methods and properites below should therefore be on its prototype.
109
128
110
129
PatternEngines = Object . create ( {
111
- // build the list of supported pattern engines based on what plugins we have
112
- // in the pattern_engines directory
113
- supportedPatternEngineNames : findSupportedPatternEngineNames ( ) ,
114
-
115
130
getEngineNameForPattern : function ( pattern ) {
116
131
// avoid circular dependency by putting this in here. TODO: is this slow?
117
132
var of = require ( './object_factory' ) ;
0 commit comments