From 3dd846b68561d5b2a333a8d389c5f061bd034b52 Mon Sep 17 00:00:00 2001 From: Brian Muenzenmeyer Date: Tue, 16 May 2017 12:24:59 -0500 Subject: [PATCH] spawn handlebars meta patterns if not found part of https://github.com/pattern-lab/patternlab-node/issues/611 --- _meta/_00-head.hbs | 16 ++++++++++++++++ _meta/_01-foot.hbs | 6 ++++++ lib/engine_handlebars.js | 34 +++++++++++++++++++++++++++++++--- package.json | 1 + 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 _meta/_00-head.hbs create mode 100644 _meta/_01-foot.hbs diff --git a/_meta/_00-head.hbs b/_meta/_00-head.hbs new file mode 100644 index 0000000..b1f5c1c --- /dev/null +++ b/_meta/_00-head.hbs @@ -0,0 +1,16 @@ + + + + {{ title }} + + + + + + + + {{{ patternLabHead }}} + + + + diff --git a/_meta/_01-foot.hbs b/_meta/_01-foot.hbs new file mode 100644 index 0000000..797d941 --- /dev/null +++ b/_meta/_01-foot.hbs @@ -0,0 +1,6 @@ + + +{{{ patternLabFoot }}} + + + diff --git a/lib/engine_handlebars.js b/lib/engine_handlebars.js index 0e6748f..5b5da08 100644 --- a/lib/engine_handlebars.js +++ b/lib/engine_handlebars.js @@ -1,3 +1,5 @@ +"use strict"; + /* * handlebars pattern engine for patternlab-node - v0.15.1 - 2015 * @@ -20,9 +22,9 @@ * */ -"use strict"; - -var Handlebars = require('handlebars'); +const fs = require('fs-extra'); +const path = require('path'); +const Handlebars = require('handlebars'); // regexes, stored here so they're only compiled once const findPartialsRE = /{{#?>\s*([\w-\/.]+)(?:.|\s+)*?}}/g; @@ -92,6 +94,32 @@ var engine_handlebars = { findPartial: function (partialString) { var partial = partialString.replace(findPartialsRE, '$1'); return partial; + }, + + spawnFile: function (config, fileName) { + const paths = config.paths; + const metaFilePath = path.resolve(paths.source.meta, fileName); + try { + fs.statSync(metaFilePath); + } catch (err) { + + //not a file, so spawn it from the included file + const localMetaFilePath = path.resolve(__dirname, '_meta/', fileName); + const metaFileContent = fs.readFileSync(path.resolve(__dirname, '..', '_meta/', fileName), 'utf8'); + fs.outputFileSync(metaFilePath, metaFileContent); + } + }, + + /** + * Checks to see if the _meta directory has engine-specific head and foot files, + * spawning them if not found. + * + * @param {object} config - the global config object from core, since we won't + * assume it's already present + */ + spawnMeta: function (config) { + this.spawnFile(config, '_00-head.hbs'); + this.spawnFile(config, '_01-foot.hbs'); } }; diff --git a/package.json b/package.json index 064719b..e6350ec 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "version": "1.0.2", "main": "lib/engine_handlebars.js", "dependencies": { + "fs-extra": "^0.30.0", "handlebars": "^4.0.5" }, "devDependencies": {},