From bf6b79fe190a0da131895013f797a4036afe905d Mon Sep 17 00:00:00 2001 From: Geoffrey Pursell Date: Tue, 3 Oct 2017 13:19:04 -0500 Subject: [PATCH 1/9] first commit: apply a thin coating of Promises to start the process of an end-to-end async build process --- core/lib/patternlab.js | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index a033c3a0e..b01f1eeb1 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -280,11 +280,13 @@ const patternlab_engine = function (config) { return value; } + // GTP: this commented out now on the advice of Brian, because we think nobody looks at it, + // and it causes problems. //debug file can be written by setting flag on patternlab-config.json - if (patternlab.config.debug) { - console.log('writing patternlab debug file to ./patternlab.json'); - fs.outputFileSync('./patternlab.json', JSON.stringify(patternlab, propertyStringReplacer, 3)); - } + // if (patternlab.config.debug) { + // console.log('writing patternlab debug file to ./patternlab.json'); + // fs.outputFileSync('./patternlab.json', JSON.stringify(patternlab, propertyStringReplacer, 3)); + // } } function setCacheBust() { @@ -382,7 +384,7 @@ const patternlab_engine = function (config) { function renderSinglePattern(pattern, head) { // Pattern does not need to be built and recompiled more than once if (!pattern.isPattern || pattern.compileState === CompileState.CLEAN) { - return false; + return Promise.resolve(false); } // Allows serializing the compile state @@ -478,7 +480,8 @@ const patternlab_engine = function (config) { // Allows serializing the compile state patternlab.graph.node(pattern).compileState = pattern.compileState = CompileState.CLEAN; plutils.log.info("Built pattern: " + pattern.patternPartial); - return true; + + return Promise.resolve(true); } /** @@ -630,17 +633,21 @@ const patternlab_engine = function (config) { } //render all patterns last, so lineageR works - patternsToBuild.forEach(pattern => renderSinglePattern(pattern, head)); - - // Saves the pattern graph when all files have been compiled - PatternGraph.storeToFile(patternlab); - if (patternlab.config.exportToGraphViz) { - PatternGraph.exportToDot(patternlab, "dependencyGraph.dot"); - plutils.log.info(`Exported pattern graph to ${path.join(config.paths.public.root, "dependencyGraph.dot")}`); - } - - //export patterns if necessary - pattern_exporter.export_patterns(patternlab); + return patternsToBuild + .reduce((previousPromise, pattern) => { + return previousPromise.then(() => renderSinglePattern(pattern, head)); + }, Promise.resolve()) + .then(() => { + // Saves the pattern graph when all files have been compiled + PatternGraph.storeToFile(patternlab); + if (patternlab.config.exportToGraphViz) { + PatternGraph.exportToDot(patternlab, "dependencyGraph.dot"); + plutils.log.info(`Exported pattern graph to ${path.join(config.paths.public.root, "dependencyGraph.dot")}`); + } + + //export patterns if necessary + pattern_exporter.export_patterns(patternlab); + }); }).catch((err) => { console.log('Error in buildPatterns():', err); }); From 2139de3a85a7f892c6096185933c91764b6b8d0e Mon Sep 17 00:00:00 2001 From: Geoffrey Pursell Date: Tue, 17 Oct 2017 15:13:46 -0500 Subject: [PATCH 2/9] factor out some logical chunks of the main build process --- core/lib/patternlab.js | 92 ++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 34 deletions(-) diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index b01f1eeb1..d8e70da3f 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -502,53 +502,26 @@ const patternlab_engine = function (config) { return PatternGraph.loadFromFile(patternlab); } - function buildPatterns(deletePatternDir) { - - if (patternlab.config.debug) { - console.log( - chalk.bold('\n====[ Pattern Lab / Node'), - `- v${packageInfo.version}`, - chalk.bold(']====\n') - ); - } - - patternlab.events.emit('patternlab-build-pattern-start', patternlab); - - const graph = patternlab.graph = loadPatternGraph(deletePatternDir); - - const graphNeedsUpgrade = !PatternGraph.checkVersion(graph); - - if (graphNeedsUpgrade) { - plutils.log.info("Due to an upgrade, a complete rebuild is required and the public/patterns directory was deleted. " + - "Incremental build is available again on the next successful run."); - - // Ensure that the freshly built graph has the latest version again. - patternlab.graph.upgradeVersion(); - } - - // Flags - const incrementalBuildsEnabled = !(deletePatternDir || graphNeedsUpgrade); - - if (incrementalBuildsEnabled) { - plutils.log.info("Incremental builds enabled."); - } else { - // needs to be done BEFORE processing patterns - fs.removeSync(paths.public.patterns); - fs.emptyDirSync(paths.public.patterns); - } + function buildGlobalData() { + // + // COLLECT GLOBAL LIBRARY DATA + // + // data.json try { patternlab.data = buildPatternData(paths.source.data, fs); } catch (ex) { plutils.error('missing or malformed' + paths.source.data + 'data.json Pattern Lab may not work without this file.'); patternlab.data = {}; } + // listitems.json try { patternlab.listitems = fs.readJSONSync(path.resolve(paths.source.data, 'listitems.json')); } catch (ex) { plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems.json file. Pattern Lab may not work without this file.'); patternlab.listitems = {}; } + // load up all the necessary files from pattern lab that apply to every template try { patternlab.header = fs.readFileSync(path.resolve(paths.source.patternlabFiles['general-header']), 'utf8'); patternlab.footer = fs.readFileSync(path.resolve(paths.source.patternlabFiles['general-footer']), 'utf8'); @@ -560,6 +533,12 @@ const patternlab_engine = function (config) { plutils.error('\nERROR: missing an essential file from ' + paths.source.patternlabFiles + '. Pattern Lab won\'t work without this file.\n'); process.exit(1); } + + + // + // INITIALIZE EMPTY GLOBAL DATA STRUCTURES + // + patternlab.patterns = []; patternlab.subtypePatterns = {}; patternlab.partials = {}; @@ -570,6 +549,51 @@ const patternlab_engine = function (config) { pattern_assembler.combine_listItems(patternlab); patternlab.events.emit('patternlab-build-global-data-end', patternlab); + } + + + function cleanBuildDirectory(incrementalBuildsEnabled) { + if (incrementalBuildsEnabled) { + plutils.log.info("Incremental builds enabled."); + } else { + // needs to be done BEFORE processing patterns + fs.removeSync(paths.public.patterns); + fs.emptyDirSync(paths.public.patterns); + } + } + + function buildPatterns(deletePatternDir) { + + if (patternlab.config.debug) { + console.log( + chalk.bold('\n====[ Pattern Lab / Node'), + `- v${packageInfo.version}`, + chalk.bold(']====\n') + ); + } + + patternlab.events.emit('patternlab-build-pattern-start', patternlab); + + // + // CHECK INCREMENTAL BUILD GRAPH + // + const graph = patternlab.graph = loadPatternGraph(deletePatternDir); + const graphNeedsUpgrade = !PatternGraph.checkVersion(graph); + if (graphNeedsUpgrade) { + plutils.log.info("Due to an upgrade, a complete rebuild is required and the public/patterns directory was deleted. " + + "Incremental build is available again on the next successful run."); + + // Ensure that the freshly built graph has the latest version again. + patternlab.graph.upgradeVersion(); + } + // Flags + const incrementalBuildsEnabled = !(deletePatternDir || graphNeedsUpgrade); + + // + // CLEAN BUILD DIRECTORY, maybe + // + cleanBuildDirectory(incrementalBuildsEnabled); + buildGlobalData(); // diveSync once to perform iterative populating of patternlab object return processAllPatternsIterative(paths.source.patterns, patternlab).then(() => { From 8b9e7d0efb599d8cfc2ae280fb34f48b05cd97da Mon Sep 17 00:00:00 2001 From: Geoffrey Pursell Date: Tue, 17 Oct 2017 18:19:57 -0500 Subject: [PATCH 3/9] migrate the patternlab.incrementalBuildsEnabled flag into the global patternlab state object --- core/lib/patternlab.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index d8e70da3f..e3bbcc3cd 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -587,12 +587,13 @@ const patternlab_engine = function (config) { patternlab.graph.upgradeVersion(); } // Flags - const incrementalBuildsEnabled = !(deletePatternDir || graphNeedsUpgrade); + patternlab.incrementalBuildsEnabled = !(deletePatternDir || graphNeedsUpgrade); // // CLEAN BUILD DIRECTORY, maybe // - cleanBuildDirectory(incrementalBuildsEnabled); + cleanBuildDirectory(patternlab.incrementalBuildsEnabled); + buildGlobalData(); // diveSync once to perform iterative populating of patternlab object @@ -637,7 +638,7 @@ const patternlab_engine = function (config) { // rebuild all patterns patternsToBuild = null; - if (incrementalBuildsEnabled) { + if (patternlab.incrementalBuildsEnabled) { // When the graph was loaded from file, some patterns might have been moved/deleted between runs // so the graph data become out of sync patternlab.graph.sync().forEach(n => { From cee027909be833a2df567d88f6e904ee18abff15 Mon Sep 17 00:00:00 2001 From: Geoffrey Pursell Date: Tue, 17 Oct 2017 22:13:47 -0500 Subject: [PATCH 4/9] a decent start at a real Pattern Lab object class --- core/lib/patternlab.js | 45 +++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index e3bbcc3cd..aeb6e755c 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -47,6 +47,31 @@ plutils.log.on('info', msg => console.log(msg)); const patternEngines = require('./pattern_engines'); const EventEmitter = require('events').EventEmitter; +class PatternLab { + constructor(config) { + // Either use the config we were passed, or load one up from the config file ourselves + this.config = config || fs.readJSONSync(path.resolve(__dirname, '../../patternlab-config.json')); + // Load up engines please + this.engines = patternEngines; + this.engines.loadAllEngines(config); + + // Cache the package.json in RAM + this.package = fs.readJSONSync(path.resolve(__dirname, '../../package.json')); + + // Make ye olde event emitter + this.events = new PatternLabEventEmitter(); + + // Make a place for the pattern graph to sit + this.graph = null; + + // Verify correctness of configuration (?) + checkConfiguration(this); + + // TODO: determine if this is the best place to wire up plugins + initializePlugins(this); + } +} + function buildPatternData(dataFilesPath, fsDep) { const dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']}); let mergeObject = {}; @@ -183,22 +208,7 @@ function PatternLabEventEmitter() { inherits(PatternLabEventEmitter, EventEmitter); const patternlab_engine = function (config) { - const patternlab = {}; - - patternlab.engines = patternEngines; - patternlab.engines.loadAllEngines(config); - - patternlab.package = fs.readJSONSync(path.resolve(__dirname, '../../package.json')); - patternlab.config = config || fs.readJSONSync(path.resolve(__dirname, '../../patternlab-config.json')); - patternlab.events = new PatternLabEventEmitter(); - - // Initialized when building - patternlab.graph = null; - - checkConfiguration(patternlab); - - //todo: determine if this is the best place to wire up plugins - initializePlugins(patternlab); + const patternlab = new PatternLab(config); const paths = patternlab.config.paths; @@ -563,6 +573,7 @@ const patternlab_engine = function (config) { } function buildPatterns(deletePatternDir) { + patternlab.events.emit('patternlab-build-pattern-start', patternlab); if (patternlab.config.debug) { console.log( @@ -572,8 +583,6 @@ const patternlab_engine = function (config) { ); } - patternlab.events.emit('patternlab-build-pattern-start', patternlab); - // // CHECK INCREMENTAL BUILD GRAPH // From 9f2ac665beded633e473f12603fc57f8ae59faae Mon Sep 17 00:00:00 2001 From: Geoffrey Pursell Date: Tue, 17 Oct 2017 22:22:22 -0500 Subject: [PATCH 5/9] Move buildGlobalData() into PatternLab class. --- core/lib/patternlab.js | 127 +++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 62 deletions(-) diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index aeb6e755c..b9f4a0b66 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -70,6 +70,70 @@ class PatternLab { // TODO: determine if this is the best place to wire up plugins initializePlugins(this); } + + buildGlobalData() { + const paths = this.config.paths; + + // + // COLLECT GLOBAL LIBRARY DATA + // + + // data.json + try { + this.data = buildPatternData(paths.source.data, fs); + } catch (ex) { + plutils.error('missing or malformed' + paths.source.data + 'data.json Pattern Lab may not work without this file.'); + this.data = {}; + } + // listitems.json + try { + this.listitems = fs.readJSONSync(path.resolve(paths.source.data, 'listitems.json')); + } catch (ex) { + plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems.json file. Pattern Lab may not work without this file.'); + this.listitems = {}; + } + // load up all the necessary files from pattern lab that apply to every template + try { + this.header = fs.readFileSync(path.resolve(paths.source.patternlabFiles['general-header']), 'utf8'); + this.footer = fs.readFileSync(path.resolve(paths.source.patternlabFiles['general-footer']), 'utf8'); + this.patternSection = fs.readFileSync(path.resolve(paths.source.patternlabFiles.patternSection), 'utf8'); + this.patternSectionSubType = fs.readFileSync(path.resolve(paths.source.patternlabFiles.patternSectionSubtype), 'utf8'); + this.viewAll = fs.readFileSync(path.resolve(paths.source.patternlabFiles.viewall), 'utf8'); + } catch (ex) { + console.log(ex); + plutils.error('\nERROR: missing an essential file from ' + paths.source.patternlabFiles + '. Pattern Lab won\'t work without this file.\n'); + + // GTP: it seems increasingly naughty as we refactor to just unilaterally do this here, + // but whatever. For now. + process.exit(1); + } + + + // + // INITIALIZE EMPTY GLOBAL DATA STRUCTURES + // + this.patterns = []; + this.subtypePatterns = {}; + this.partials = {}; + this.data.link = {}; + + this.setCacheBust(); + + pattern_assembler.combine_listItems(this); + + this.events.emit('patternlab-build-global-data-end', this); + } + + setCacheBust() { + if (this.config.cacheBust) { + if (this.config.debug) { + console.log('setting cacheBuster value for frontend assets.'); + } + this.cacheBuster = new Date().getTime(); + } else { + this.cacheBuster = 0; + } + } } function buildPatternData(dataFilesPath, fsDep) { @@ -209,7 +273,6 @@ inherits(PatternLabEventEmitter, EventEmitter); const patternlab_engine = function (config) { const patternlab = new PatternLab(config); - const paths = patternlab.config.paths; function getVersion() { @@ -299,17 +362,6 @@ const patternlab_engine = function (config) { // } } - function setCacheBust() { - if (patternlab.config.cacheBust) { - if (patternlab.config.debug) { - console.log('setting cacheBuster value for frontend assets.'); - } - patternlab.cacheBuster = new Date().getTime(); - } else { - patternlab.cacheBuster = 0; - } - } - function listStarterkits() { const starterkit_manager = new sm(patternlab.config); return starterkit_manager.list_starterkits(); @@ -512,55 +564,6 @@ const patternlab_engine = function (config) { return PatternGraph.loadFromFile(patternlab); } - function buildGlobalData() { - // - // COLLECT GLOBAL LIBRARY DATA - // - - // data.json - try { - patternlab.data = buildPatternData(paths.source.data, fs); - } catch (ex) { - plutils.error('missing or malformed' + paths.source.data + 'data.json Pattern Lab may not work without this file.'); - patternlab.data = {}; - } - // listitems.json - try { - patternlab.listitems = fs.readJSONSync(path.resolve(paths.source.data, 'listitems.json')); - } catch (ex) { - plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems.json file. Pattern Lab may not work without this file.'); - patternlab.listitems = {}; - } - // load up all the necessary files from pattern lab that apply to every template - try { - patternlab.header = fs.readFileSync(path.resolve(paths.source.patternlabFiles['general-header']), 'utf8'); - patternlab.footer = fs.readFileSync(path.resolve(paths.source.patternlabFiles['general-footer']), 'utf8'); - patternlab.patternSection = fs.readFileSync(path.resolve(paths.source.patternlabFiles.patternSection), 'utf8'); - patternlab.patternSectionSubType = fs.readFileSync(path.resolve(paths.source.patternlabFiles.patternSectionSubtype), 'utf8'); - patternlab.viewAll = fs.readFileSync(path.resolve(paths.source.patternlabFiles.viewall), 'utf8'); - } catch (ex) { - console.log(ex); - plutils.error('\nERROR: missing an essential file from ' + paths.source.patternlabFiles + '. Pattern Lab won\'t work without this file.\n'); - process.exit(1); - } - - - // - // INITIALIZE EMPTY GLOBAL DATA STRUCTURES - // - - patternlab.patterns = []; - patternlab.subtypePatterns = {}; - patternlab.partials = {}; - patternlab.data.link = {}; - - setCacheBust(); - - pattern_assembler.combine_listItems(patternlab); - - patternlab.events.emit('patternlab-build-global-data-end', patternlab); - } - function cleanBuildDirectory(incrementalBuildsEnabled) { if (incrementalBuildsEnabled) { @@ -603,7 +606,7 @@ const patternlab_engine = function (config) { // cleanBuildDirectory(patternlab.incrementalBuildsEnabled); - buildGlobalData(); + patternlab.buildGlobalData(); // diveSync once to perform iterative populating of patternlab object return processAllPatternsIterative(paths.source.patterns, patternlab).then(() => { From 6ca768ee0d3c822e2e8b0c6037ed14aae260c7c7 Mon Sep 17 00:00:00 2001 From: Geoffrey Pursell Date: Tue, 17 Oct 2017 22:29:18 -0500 Subject: [PATCH 6/9] Moving more things logically into the PatternLab class --- core/lib/patternlab.js | 125 +++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 55 deletions(-) diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index b9f4a0b66..832420a9a 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -134,6 +134,70 @@ class PatternLab { this.cacheBuster = 0; } } + + + // Pattern processing methods + + /** + * Process the user-defined pattern head and prepare it for rendering + */ + processHeadPattern() { + try { + const headPath = path.resolve(this.config.paths.source.meta, '_00-head.mustache'); + const headPattern = new Pattern(headPath, null, this); + headPattern.template = fs.readFileSync(headPath, 'utf8'); + headPattern.isPattern = false; + headPattern.isMetaPattern = true; + pattern_assembler.decomposePattern(headPattern, this, true); + this.userHead = headPattern.extendedTemplate; + } + catch (ex) { + plutils.error('\nWARNING: Could not find the user-editable header template, currently configured to be at ' + path.join(this.config.paths.source.meta, '_00-head.mustache') + '. Your configured path may be incorrect (check this.config.paths.source.meta in your config file), the file may have been deleted, or it may have been left in the wrong place during a migration or update.\n'); + if (this.config.debug) { console.log(ex); } + + // GTP: it seems increasingly naughty as we refactor to just unilaterally do this here, + // but whatever. For now. + process.exit(1); + } + } + + /** + * Process the user-defined pattern footer and prepare it for rendering + */ + processFootPattern() { + try { + const footPath = path.resolve(this.config.paths.source.meta, '_01-foot.mustache'); + const footPattern = new Pattern(footPath, null, this); + footPattern.template = fs.readFileSync(footPath, 'utf8'); + footPattern.isPattern = false; + footPattern.isMetaPattern = true; + pattern_assembler.decomposePattern(footPattern, this, true); + this.userFoot = footPattern.extendedTemplate; + } + catch (ex) { + plutils.error('\nWARNING: Could not find the user-editable footer template, currently configured to be at ' + path.join(this.config.paths.source.meta, '_01-foot.mustache') + '. Your configured path may be incorrect (check this.config.paths.source.meta in your config file), the file may have been deleted, or it may have been left in the wrong place during a migration or update.\n'); + if (this.config.debug) { console.log(ex); } + + // GTP: it seems increasingly naughty as we refactor to just unilaterally do this here, + // but whatever. For now. + process.exit(1); + } + } + + + // info methods + + getVersion() { + return this.package.version; + } + logVersion() { + console.log(this.package.version); + } + getSupportedTemplateExtensions() { + return this.engines.getSupportedFileExtensions(); + } + + } function buildPatternData(dataFilesPath, fsDep) { @@ -275,18 +339,6 @@ const patternlab_engine = function (config) { const patternlab = new PatternLab(config); const paths = patternlab.config.paths; - function getVersion() { - return patternlab.package.version; - } - - function logVersion() { - console.log(patternlab.package.version); - } - - function getSupportedTemplateExtensions() { - return patternlab.engines.getSupportedFileExtensions(); - } - function help() { console.log(''); @@ -372,45 +424,7 @@ const patternlab_engine = function (config) { starterkit_manager.load_starterkit(starterkitName, clean); } - /** - * Process the user-defined pattern head and prepare it for rendering - */ - function processHeadPattern() { - try { - const headPath = path.resolve(paths.source.meta, '_00-head.mustache'); - const headPattern = new Pattern(headPath, null, patternlab); - headPattern.template = fs.readFileSync(headPath, 'utf8'); - headPattern.isPattern = false; - headPattern.isMetaPattern = true; - pattern_assembler.decomposePattern(headPattern, patternlab, true); - patternlab.userHead = headPattern.extendedTemplate; - } - catch (ex) { - plutils.error('\nWARNING: Could not find the user-editable header template, currently configured to be at ' + path.join(config.paths.source.meta, '_00-head.mustache') + '. Your configured path may be incorrect (check paths.source.meta in your config file), the file may have been deleted, or it may have been left in the wrong place during a migration or update.\n'); - if (patternlab.config.debug) { console.log(ex); } - process.exit(1); - } - } - /** - * Process the user-defined pattern footer and prepare it for rendering - */ - function processFootPattern() { - try { - const footPath = path.resolve(paths.source.meta, '_01-foot.mustache'); - const footPattern = new Pattern(footPath, null, patternlab); - footPattern.template = fs.readFileSync(footPath, 'utf8'); - footPattern.isPattern = false; - footPattern.isMetaPattern = true; - pattern_assembler.decomposePattern(footPattern, patternlab, true); - patternlab.userFoot = footPattern.extendedTemplate; - } - catch (ex) { - plutils.error('\nWARNING: Could not find the user-editable footer template, currently configured to be at ' + path.join(config.paths.source.meta, '_01-foot.mustache') + '. Your configured path may be incorrect (check paths.source.meta in your config file), the file may have been deleted, or it may have been left in the wrong place during a migration or update.\n'); - if (patternlab.config.debug) { console.log(ex); } - process.exit(1); - } - } function writePatternFiles(headHTML, pattern, footerHTML) { const nullFormatter = str => str; @@ -623,8 +637,9 @@ const patternlab_engine = function (config) { processAllPatternsRecursive(paths.source.patterns, patternlab); //take the user defined head and foot and process any data and patterns that apply - processHeadPattern(); - processFootPattern(); + // GTP: should these really be invoked from outside? + patternlab.processHeadPattern(); + patternlab.processFootPattern(); //cascade any patternStates lineage_hunter.cascade_pattern_states(patternlab); @@ -692,10 +707,10 @@ const patternlab_engine = function (config) { return { version: function () { - return logVersion(); + return patternlab.logVersion(); }, v: function () { - return getVersion(); + return patternlab.getVersion(); }, build: function (callback, deletePatternDir) { if (patternlab && patternlab.isBusy) { @@ -735,7 +750,7 @@ const patternlab_engine = function (config) { installPlugin(pluginName); }, getSupportedTemplateExtensions: function () { - return getSupportedTemplateExtensions(); + return patternlab.getSupportedTemplateExtensions(); } }; }; From 004bb181e8c64f8b65bd51e25abc096eae111009 Mon Sep 17 00:00:00 2001 From: Geoffrey Pursell Date: Tue, 17 Oct 2017 22:36:44 -0500 Subject: [PATCH 7/9] Move starter kit loading methods into main class. This seems incomplete, since they're still very much invoked from outside. Maybe that's okay. --- core/lib/patternlab.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index 832420a9a..affd3d6fb 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -136,6 +136,19 @@ class PatternLab { } + // Starter Kit loading methods + + listStarterkits() { + const starterkit_manager = new sm(this.config); + return starterkit_manager.list_starterkits(); + } + + loadStarterKit(starterkitName, clean) { + const starterkit_manager = new sm(this.config); + starterkit_manager.load_starterkit(starterkitName, clean); + } + + // Pattern processing methods /** @@ -414,15 +427,7 @@ const patternlab_engine = function (config) { // } } - function listStarterkits() { - const starterkit_manager = new sm(patternlab.config); - return starterkit_manager.list_starterkits(); - } - function loadStarterKit(starterkitName, clean) { - const starterkit_manager = new sm(patternlab.config); - starterkit_manager.load_starterkit(starterkitName, clean); - } @@ -741,10 +746,10 @@ const patternlab_engine = function (config) { }); }, liststarterkits: function () { - return listStarterkits(); + return patternlab.listStarterkits(); }, loadstarterkit: function (starterkitName, clean) { - loadStarterKit(starterkitName, clean); + patternlab.loadStarterKit(starterkitName, clean); }, installplugin: function (pluginName) { installPlugin(pluginName); From 6fc5f5e3d2af98a6247a224704314dd1abc87b19 Mon Sep 17 00:00:00 2001 From: Geoffrey Pursell Date: Tue, 17 Oct 2017 22:39:52 -0500 Subject: [PATCH 8/9] Move writePatternFiles() into the class --- core/lib/patternlab.js | 65 ++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index affd3d6fb..8eff8212b 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -211,6 +211,36 @@ class PatternLab { } + writePatternFiles(headHTML, pattern, footerHTML) { + const nullFormatter = str => str; + const defaultFormatter = codeString => cleanHtml(codeString, {indent_size: 2}); + const makePath = type => path.join(this.config.paths.public.patterns, pattern.getPatternLink(this, type)); + const patternPage = headHTML + pattern.patternPartialCode + footerHTML; + const eng = pattern.engine; + + //beautify the output if configured to do so + const formatters = this.config.cleanOutputHtml ? { + rendered: eng.renderedCodeFormatter || defaultFormatter, + rawTemplate: eng.rawTemplateCodeFormatter || defaultFormatter, + markupOnly: eng.markupOnlyCodeFormatter || defaultFormatter + } : { + rendered: nullFormatter, + rawTemplate: nullFormatter, + markupOnly: nullFormatter + }; + + //prepare the path and contents of each output file + const outputFiles = [ + { path: makePath('rendered'), content: formatters.rendered(patternPage, pattern) }, + { path: makePath('rawTemplate'), content: formatters.rawTemplate(pattern.template, pattern) }, + { path: makePath('markupOnly'), content: formatters.markupOnly(pattern.patternPartialCode, pattern) } + ].concat( + eng.addOutputFiles ? eng.addOutputFiles(this.config.paths, this) : [] + ); + + //write the compiled template to the public patterns directory + outputFiles.forEach(outFile => fs.outputFileSync(outFile.path, outFile.content)); + } } function buildPatternData(dataFilesPath, fsDep) { @@ -429,39 +459,6 @@ const patternlab_engine = function (config) { - - - function writePatternFiles(headHTML, pattern, footerHTML) { - const nullFormatter = str => str; - const defaultFormatter = codeString => cleanHtml(codeString, {indent_size: 2}); - const makePath = type => path.join(paths.public.patterns, pattern.getPatternLink(patternlab, type)); - const patternPage = headHTML + pattern.patternPartialCode + footerHTML; - const eng = pattern.engine; - - //beautify the output if configured to do so - const formatters = config.cleanOutputHtml ? { - rendered: eng.renderedCodeFormatter || defaultFormatter, - rawTemplate: eng.rawTemplateCodeFormatter || defaultFormatter, - markupOnly: eng.markupOnlyCodeFormatter || defaultFormatter - } : { - rendered: nullFormatter, - rawTemplate: nullFormatter, - markupOnly: nullFormatter - }; - - //prepare the path and contents of each output file - const outputFiles = [ - { path: makePath('rendered'), content: formatters.rendered(patternPage, pattern) }, - { path: makePath('rawTemplate'), content: formatters.rawTemplate(pattern.template, pattern) }, - { path: makePath('markupOnly'), content: formatters.markupOnly(pattern.patternPartialCode, pattern) } - ].concat( - eng.addOutputFiles ? eng.addOutputFiles(paths, patternlab) : [] - ); - - //write the compiled template to the public patterns directory - outputFiles.forEach(outFile => fs.outputFileSync(outFile.path, outFile.content)); - } - function renderSinglePattern(pattern, head) { // Pattern does not need to be built and recompiled more than once if (!pattern.isPattern || pattern.compileState === CompileState.CLEAN) { @@ -554,7 +551,7 @@ const patternlab_engine = function (config) { patternlab.events.emit('patternlab-pattern-write-begin', patternlab, pattern); //write the compiled template to the public patterns directory - writePatternFiles(headHTML, pattern, footerHTML); + patternlab.writePatternFiles(headHTML, pattern, footerHTML); patternlab.events.emit('patternlab-pattern-write-end', patternlab, pattern); From 7c162876e477985ecd402ac13e802ee5ef0e67b1 Mon Sep 17 00:00:00 2001 From: Brian Muenzenmeyer Date: Tue, 24 Oct 2017 16:22:11 -0500 Subject: [PATCH 9/9] chore(package): Update live-server dependency, lock file --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8db5e864b..80de22468 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@pattern-lab/patternlab-node", - "version": "3.0.0-alpha.1", + "version": "3.0.0-alpha.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1899,7 +1899,7 @@ } }, "live-server": { - "version": "github:pattern-lab/live-server#d46ec211455efb0ae2ad140b09e151bc2649c496", + "version": "github:pattern-lab/live-server#5db065902f95266af29b7e80873c3a4dbdd4620d", "requires": { "chokidar": "1.7.0", "colors": "1.1.2",