Skip to content

Commit 52d52ac

Browse files
committed
PatternLab only loads the pattern engines from the config
1 parent 1d28ab0 commit 52d52ac

File tree

4 files changed

+49
-152
lines changed

4 files changed

+49
-152
lines changed

Diff for: packages/core/src/lib/findModules.js

-60
This file was deleted.

Diff for: packages/core/src/lib/pattern_engines.js

+38-91
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,11 @@
11
// special shoutout to Geoffrey Pursell for single-handedly making Pattern Lab Node Pattern Engines possible! aww thanks :)
22
'use strict';
3-
const { existsSync } = require('fs');
43
const path = require('path');
54

6-
const findModules = require('./findModules');
7-
8-
const engineMatcher = /^engine-(.*)$/;
9-
105
const logger = require('./log');
116

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;
569
return foundEngines;
5710
}
5811

@@ -80,49 +33,43 @@ const PatternEngines = Object.create({
8033
loadAllEngines: function(patternLabConfig) {
8134
const self = this;
8235

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);
12459
}
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+
}
12673
});
12774

12875
// Complain if for some reason we haven't loaded any engines.

Diff for: packages/core/src/lib/starterkit_manager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const starterkit_manager = function(config) {
108108
*
109109
* @return {array} List of starter kits installed
110110
*/
111-
//TODO review for deletion or convert callers to use findModules()
111+
//TODO review for deletion
112112
function detectStarterKits() {
113113
const node_modules_path = path.join(process.cwd(), 'node_modules');
114114
const npm_modules = fs.readdirSync(node_modules_path).filter(function(dir) {

Diff for: packages/core/test/util/patternlab-config.json

+10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@
7373
"density": "compact",
7474
"layout": "horizontal"
7575
},
76+
"engines": [
77+
{
78+
"package": "@pattern-lab/engine-mustache",
79+
"extension": "mustache"
80+
},
81+
{
82+
"package": "@pattern-lab/engine-handlebars",
83+
"extension": "hbs"
84+
}
85+
],
7686
"uikits": [
7787
{
7888
"name": "uikit-workshop",

0 commit comments

Comments
 (0)