|
1 | 1 | // special shoutout to Geoffrey Pursell for single-handedly making Pattern Lab Node Pattern Engines possible! aww thanks :) |
2 | 2 | 'use strict'; |
3 | | -const { existsSync } = require('fs'); |
4 | 3 | const path = require('path'); |
5 | 4 |
|
6 | | -const findModules = require('./findModules'); |
7 | | - |
8 | | -const engineMatcher = /^engine-(.*)$/; |
9 | | - |
10 | 5 | const logger = require('./log'); |
11 | 6 |
|
12 | | -const enginesDirectories = [ |
13 | | - { |
14 | | - displayName: 'the core', |
15 | | - path: path.resolve(__dirname, '..', '..', 'node_modules'), |
16 | | - }, |
17 | | - { |
18 | | - displayName: 'the edition or test directory', |
19 | | - path: path.join(process.cwd(), 'node_modules'), |
20 | | - }, |
21 | | -]; |
22 | | - |
23 | | -/** |
24 | | - * Given a path: return the engine name if the path points to a valid engine |
25 | | - * module directory, or false if it doesn't. |
26 | | - * @param filePath |
27 | | - * @returns Engine name if exists or FALSE |
28 | | - */ |
29 | | -function isEngineModule(filePath) { |
30 | | - const baseName = path.basename(filePath); |
31 | | - const engineMatch = baseName.match(engineMatcher); |
32 | | - |
33 | | - if (engineMatch) { |
34 | | - return engineMatch[1]; |
35 | | - } |
36 | | - return false; |
37 | | -} |
38 | | - |
39 | | -/** |
40 | | - * @name resolveEngines |
41 | | - * @desc Creates an array of all available patternlab engines |
42 | | - * @param {string} dir - The directory to search for engines and scoped engines) |
43 | | - * @return {Array<Engine>} An array of engine objects |
44 | | - */ |
45 | | -function resolveEngines(dir) { |
46 | | - // Guard against non-existent directories. |
47 | | - if (!existsSync(dir)) { |
48 | | - return []; // Silence is golden … |
49 | | - } |
50 | | - |
51 | | - return findModules(dir, isEngineModule); |
52 | | -} |
53 | | - |
54 | | -function findEngineModulesInDirectory(dir) { |
55 | | - const foundEngines = resolveEngines(dir); |
| 7 | +function findEnginesInConfig(config) { |
| 8 | + const foundEngines = config.engines; |
56 | 9 | return foundEngines; |
57 | 10 | } |
58 | 11 |
|
@@ -80,49 +33,43 @@ const PatternEngines = Object.create({ |
80 | 33 | loadAllEngines: function(patternLabConfig) { |
81 | 34 | const self = this; |
82 | 35 |
|
83 | | - // Try to load engines! We scan for engines at each path specified above. This |
84 | | - // function is kind of a big deal. |
85 | | - enginesDirectories.forEach(function(engineDirectory) { |
86 | | - const enginesInThisDir = findEngineModulesInDirectory( |
87 | | - engineDirectory.path |
88 | | - ); |
89 | | - |
90 | | - logger.debug(`Loading engines from ${engineDirectory.displayName}...`); |
91 | | - |
92 | | - // find all engine-named things in this directory and try to load them, |
93 | | - // unless it's already been loaded. |
94 | | - enginesInThisDir.forEach(function(engineDiscovery) { |
95 | | - let errorMessage; |
96 | | - const successMessage = 'good to go'; |
97 | | - |
98 | | - try { |
99 | | - // Give it a try! load 'er up. But not if we already have, |
100 | | - // of course. Also pass the pattern lab config object into |
101 | | - // the engine's closure scope so it can know things about |
102 | | - // things. |
103 | | - if (self[engineDiscovery.name]) { |
104 | | - throw new Error('already loaded, skipping.'); |
105 | | - } |
106 | | - self[engineDiscovery.name] = require(engineDiscovery.modulePath); |
107 | | - if ( |
108 | | - typeof self[engineDiscovery.name].usePatternLabConfig === 'function' |
109 | | - ) { |
110 | | - self[engineDiscovery.name].usePatternLabConfig(patternLabConfig); |
111 | | - } |
112 | | - if (typeof self[engineDiscovery.name].spawnMeta === 'function') { |
113 | | - self[engineDiscovery.name].spawnMeta(patternLabConfig); |
114 | | - } |
115 | | - } catch (err) { |
116 | | - errorMessage = err.message; |
117 | | - } finally { |
118 | | - // report on the status of the engine, one way or another! |
119 | | - logger.info( |
120 | | - `Pattern Engine ${engineDiscovery.name}: ${ |
121 | | - errorMessage ? errorMessage : successMessage |
122 | | - }` |
123 | | - ); |
| 36 | + // Try to load engines! We load the engines configured in patternlab-config.json |
| 37 | + const enginesInConfig = findEnginesInConfig(patternLabConfig); |
| 38 | + |
| 39 | + logger.debug('Loading engines from patternlab-config.json'); |
| 40 | + |
| 41 | + // Try loading each of the configured pattern engines |
| 42 | + enginesInConfig.forEach(function(engineConfig) { |
| 43 | + let errorMessage; |
| 44 | + const successMessage = 'good to go'; |
| 45 | + |
| 46 | + try { |
| 47 | + // Give it a try! load 'er up. But not if we already have, |
| 48 | + // of course. Also pass the pattern lab config object into |
| 49 | + // the engine's closure scope so it can know things about |
| 50 | + // things. |
| 51 | + if (self[engineConfig.extension]) { |
| 52 | + throw new Error('already loaded, skipping.'); |
| 53 | + } |
| 54 | + self[engineConfig.extension] = require(engineConfig.package); |
| 55 | + if ( |
| 56 | + typeof self[engineConfig.extension].usePatternLabConfig === 'function' |
| 57 | + ) { |
| 58 | + self[engineConfig.extension].usePatternLabConfig(patternLabConfig); |
124 | 59 | } |
125 | | - }); |
| 60 | + if (typeof self[engineConfig.extension].spawnMeta === 'function') { |
| 61 | + self[engineConfig.extension].spawnMeta(patternLabConfig); |
| 62 | + } |
| 63 | + } catch (err) { |
| 64 | + errorMessage = err.message; |
| 65 | + } finally { |
| 66 | + // report on the status of the engine, one way or another! |
| 67 | + logger.info( |
| 68 | + `Pattern Engine ${engineConfig.extension}: ${ |
| 69 | + errorMessage ? errorMessage : successMessage |
| 70 | + }` |
| 71 | + ); |
| 72 | + } |
126 | 73 | }); |
127 | 74 |
|
128 | 75 | // Complain if for some reason we haven't loaded any engines. |
|
0 commit comments