diff --git a/packages/core/src/lib/findModules.js b/packages/core/src/lib/findModules.js deleted file mode 100644 index 246612ec8b..0000000000 --- a/packages/core/src/lib/findModules.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict'; - -const path = require('path'); - -const isScopedPackage = require('./isScopedPackage'); - -let fs = require('fs-extra'); // eslint-disable-line - -const isDir = fPath => { - const stats = fs.lstatSync(fPath); - return stats.isDirectory() || stats.isSymbolicLink(); -}; - -module.exports = (dir, filter) => { - /** - * @name findModules - * @desc Traverse the given path and gather possible engines - * @param {string} fPath - The file path to traverse - * @param {Array} foundModules - An array of modules - * @return {Array} - The final array of engines - */ - const findModules = (fPath, foundModules) => { - /** - * @name dirList - * @desc A list of all directories in the given path - * @type {Array} - */ - const dirList = fs - .readdirSync(fPath) - .filter(p => isDir(path.join(fPath, p))); - - /** - * @name m - * @desc For the current dir get all modules - * @type {Array} - */ - const m = foundModules.concat( - dirList.filter(filter).map(mod => { - return { - name: filter(mod), - modulePath: path.join(fPath, mod), - }; - }) - ); - - /** - * 1. Flatten all engines from inner recursions and current dir - * 2. Filter the dirList for scoped packages - * 3. Map over every scoped package and recurse into it to find scoped modules - */ - return [].concat( - ...m, - ...dirList - .filter(isScopedPackage) // 2 - .map(scope => findModules(path.join(fPath, scope), m)) // 3 - ); - }; - - return findModules(dir, []); -}; diff --git a/packages/core/src/lib/pattern_engines.js b/packages/core/src/lib/pattern_engines.js index cde010125e..8182a013bd 100644 --- a/packages/core/src/lib/pattern_engines.js +++ b/packages/core/src/lib/pattern_engines.js @@ -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} 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; } @@ -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. diff --git a/packages/core/src/lib/starterkit_manager.js b/packages/core/src/lib/starterkit_manager.js index f355a8f55f..9371c90f37 100644 --- a/packages/core/src/lib/starterkit_manager.js +++ b/packages/core/src/lib/starterkit_manager.js @@ -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) { diff --git a/packages/core/test/util/patternlab-config.json b/packages/core/test/util/patternlab-config.json index 1d2e964f9a..ea09eeee6a 100644 --- a/packages/core/test/util/patternlab-config.json +++ b/packages/core/test/util/patternlab-config.json @@ -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",