diff --git a/.eslintrc b/.eslintrc index 90cbdb8..c445390 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,6 +4,10 @@ "builtin": true, "es6": true }, + "parserOptions": { + "ecmaVersion": 6, + "sourceType": "module" + }, "globals": {}, "rules": { "block-scoped-var": 0, @@ -68,7 +72,7 @@ "no-with": 2, "quotes": [0, "single"], "radix": 2, - "semi": [0, "never"], + "semi": [1, "always"], "strict": 0, "space-before-blocks": 1, "space-before-function-paren": [1, { @@ -79,6 +83,10 @@ "space-infix-ops": 1, "valid-typeof": 2, "vars-on-top": 0, - "wrap-iife": [2, "inside"] + "wrap-iife": [2, "inside"], + "prefer-const": ["error", { + "destructuring": "any", + "ignoreReadBeforeAssign": false + }] } } 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 60a9e29..f5ff817 100644 --- a/lib/engine_handlebars.js +++ b/lib/engine_handlebars.js @@ -1,3 +1,5 @@ +"use strict"; + /* * handlebars pattern engine for patternlab-node * @@ -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; @@ -37,7 +39,7 @@ function escapeAtPartialBlock(partialString) { var engine_handlebars = { engine: Handlebars, engineName: 'handlebars', - engineFileExtension: '.hbs', + engineFileExtension: ['.hbs', '.handlebars'], // partial expansion is only necessary for Mustache templates that have // style modifiers or pattern parameters (I think) @@ -91,6 +93,31 @@ 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 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 93a4dc6..8d1850c 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { - "name": "patternengine-node-handlebars", + "name": "@pattern-lab/patternengine-node-handlebars", "description": "The Handlebars engine for Pattern Lab / Node", - "version": "1.0.3", + "version": "2.0.0-alpha.1", "main": "lib/engine_handlebars.js", "dependencies": { + "fs-extra": "^0.30.0", "handlebars": "^4.0.5" }, "devDependencies": {},