Skip to content

Commit

Permalink
PatternLab only loads the pattern engines from the config
Browse files Browse the repository at this point in the history
  • Loading branch information
ringods committed Sep 1, 2020
1 parent 1d28ab0 commit 52d52ac
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 152 deletions.
60 changes: 0 additions & 60 deletions packages/core/src/lib/findModules.js

This file was deleted.

129 changes: 38 additions & 91 deletions packages/core/src/lib/pattern_engines.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,11 @@
// special shoutout to Geoffrey Pursell for single-handedly making Pattern Lab Node Pattern Engines possible! aww thanks :)
'use strict';
const { existsSync } = require('fs');
const path = require('path');

const findModules = require('./findModules');

const engineMatcher = /^engine-(.*)$/;

const logger = require('./log');

const enginesDirectories = [
{
displayName: 'the core',
path: path.resolve(__dirname, '..', '..', 'node_modules'),
},
{
displayName: 'the edition or test directory',
path: path.join(process.cwd(), 'node_modules'),
},
];

/**
* Given a path: return the engine name if the path points to a valid engine
* module directory, or false if it doesn't.
* @param filePath
* @returns Engine name if exists or FALSE
*/
function isEngineModule(filePath) {
const baseName = path.basename(filePath);
const engineMatch = baseName.match(engineMatcher);

if (engineMatch) {
return engineMatch[1];
}
return false;
}

/**
* @name resolveEngines
* @desc Creates an array of all available patternlab engines
* @param {string} dir - The directory to search for engines and scoped engines)
* @return {Array<Engine>} An array of engine objects
*/
function resolveEngines(dir) {
// Guard against non-existent directories.
if (!existsSync(dir)) {
return []; // Silence is golden …
}

return findModules(dir, isEngineModule);
}

function findEngineModulesInDirectory(dir) {
const foundEngines = resolveEngines(dir);
function findEnginesInConfig(config) {
const foundEngines = config.engines;
return foundEngines;
}

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

// Try to load engines! We scan for engines at each path specified above. This
// function is kind of a big deal.
enginesDirectories.forEach(function(engineDirectory) {
const enginesInThisDir = findEngineModulesInDirectory(
engineDirectory.path
);

logger.debug(`Loading engines from ${engineDirectory.displayName}...`);

// find all engine-named things in this directory and try to load them,
// unless it's already been loaded.
enginesInThisDir.forEach(function(engineDiscovery) {
let errorMessage;
const successMessage = 'good to go';

try {
// Give it a try! load 'er up. But not if we already have,
// of course. Also pass the pattern lab config object into
// the engine's closure scope so it can know things about
// things.
if (self[engineDiscovery.name]) {
throw new Error('already loaded, skipping.');
}
self[engineDiscovery.name] = require(engineDiscovery.modulePath);
if (
typeof self[engineDiscovery.name].usePatternLabConfig === 'function'
) {
self[engineDiscovery.name].usePatternLabConfig(patternLabConfig);
}
if (typeof self[engineDiscovery.name].spawnMeta === 'function') {
self[engineDiscovery.name].spawnMeta(patternLabConfig);
}
} catch (err) {
errorMessage = err.message;
} finally {
// report on the status of the engine, one way or another!
logger.info(
`Pattern Engine ${engineDiscovery.name}: ${
errorMessage ? errorMessage : successMessage
}`
);
// Try to load engines! We load the engines configured in patternlab-config.json
const enginesInConfig = findEnginesInConfig(patternLabConfig);

logger.debug('Loading engines from patternlab-config.json');

// Try loading each of the configured pattern engines
enginesInConfig.forEach(function(engineConfig) {
let errorMessage;
const successMessage = 'good to go';

try {
// Give it a try! load 'er up. But not if we already have,
// of course. Also pass the pattern lab config object into
// the engine's closure scope so it can know things about
// things.
if (self[engineConfig.extension]) {
throw new Error('already loaded, skipping.');
}
self[engineConfig.extension] = require(engineConfig.package);
if (
typeof self[engineConfig.extension].usePatternLabConfig === 'function'
) {
self[engineConfig.extension].usePatternLabConfig(patternLabConfig);
}
});
if (typeof self[engineConfig.extension].spawnMeta === 'function') {
self[engineConfig.extension].spawnMeta(patternLabConfig);
}
} catch (err) {
errorMessage = err.message;
} finally {
// report on the status of the engine, one way or another!
logger.info(
`Pattern Engine ${engineConfig.extension}: ${
errorMessage ? errorMessage : successMessage
}`
);
}
});

// Complain if for some reason we haven't loaded any engines.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/starterkit_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const starterkit_manager = function(config) {
*
* @return {array} List of starter kits installed
*/
//TODO review for deletion or convert callers to use findModules()
//TODO review for deletion
function detectStarterKits() {
const node_modules_path = path.join(process.cwd(), 'node_modules');
const npm_modules = fs.readdirSync(node_modules_path).filter(function(dir) {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/util/patternlab-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@
"density": "compact",
"layout": "horizontal"
},
"engines": [
{
"package": "@pattern-lab/engine-mustache",
"extension": "mustache"
},
{
"package": "@pattern-lab/engine-handlebars",
"extension": "hbs"
}
],
"uikits": [
{
"name": "uikit-workshop",
Expand Down

0 comments on commit 52d52ac

Please sign in to comment.