Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit 3dd846b

Browse files
committed
spawn handlebars meta patterns if not found
part of pattern-lab/patternlab-node#611
1 parent 6bfb650 commit 3dd846b

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

_meta/_00-head.hbs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html class="{{ htmlClass }}">
3+
<head>
4+
<title>{{ title }}</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width" />
7+
8+
<link rel="stylesheet" href="../../css/style.css?{{ cacheBuster }}" media="all" />
9+
<link rel="stylesheet" href="../../css/pattern-scaffolding.css?{{ cacheBuster }}" media="all" />
10+
11+
<!-- Begin Pattern Lab (Required for Pattern Lab to run properly) -->
12+
{{{ patternLabHead }}}
13+
<!-- End Pattern Lab -->
14+
15+
</head>
16+
<body class="{{ bodyClass }}">

_meta/_01-foot.hbs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
<!--DO NOT REMOVE-->
3+
{{{ patternLabFoot }}}
4+
5+
</body>
6+
</html>

lib/engine_handlebars.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use strict";
2+
13
/*
24
* handlebars pattern engine for patternlab-node - v0.15.1 - 2015
35
*
@@ -20,9 +22,9 @@
2022
*
2123
*/
2224

23-
"use strict";
24-
25-
var Handlebars = require('handlebars');
25+
const fs = require('fs-extra');
26+
const path = require('path');
27+
const Handlebars = require('handlebars');
2628

2729
// regexes, stored here so they're only compiled once
2830
const findPartialsRE = /{{#?>\s*([\w-\/.]+)(?:.|\s+)*?}}/g;
@@ -92,6 +94,32 @@ var engine_handlebars = {
9294
findPartial: function (partialString) {
9395
var partial = partialString.replace(findPartialsRE, '$1');
9496
return partial;
97+
},
98+
99+
spawnFile: function (config, fileName) {
100+
const paths = config.paths;
101+
const metaFilePath = path.resolve(paths.source.meta, fileName);
102+
try {
103+
fs.statSync(metaFilePath);
104+
} catch (err) {
105+
106+
//not a file, so spawn it from the included file
107+
const localMetaFilePath = path.resolve(__dirname, '_meta/', fileName);
108+
const metaFileContent = fs.readFileSync(path.resolve(__dirname, '..', '_meta/', fileName), 'utf8');
109+
fs.outputFileSync(metaFilePath, metaFileContent);
110+
}
111+
},
112+
113+
/**
114+
* Checks to see if the _meta directory has engine-specific head and foot files,
115+
* spawning them if not found.
116+
*
117+
* @param {object} config - the global config object from core, since we won't
118+
* assume it's already present
119+
*/
120+
spawnMeta: function (config) {
121+
this.spawnFile(config, '_00-head.hbs');
122+
this.spawnFile(config, '_01-foot.hbs');
95123
}
96124
};
97125

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": "1.0.2",
55
"main": "lib/engine_handlebars.js",
66
"dependencies": {
7+
"fs-extra": "^0.30.0",
78
"handlebars": "^4.0.5"
89
},
910
"devDependencies": {},

0 commit comments

Comments
 (0)