1+ "use strict" ;
2+
13/*
24 * handlebars pattern engine for patternlab-node
35 *
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
2830const findPartialsRE = / { { # ? > \s * ( [ \w -\/ . ] + ) (?: .| \s + ) * ?} } / g;
@@ -37,7 +39,7 @@ function escapeAtPartialBlock(partialString) {
3739var engine_handlebars = {
3840 engine : Handlebars ,
3941 engineName : 'handlebars' ,
40- engineFileExtension : '.hbs' ,
42+ engineFileExtension : [ '.hbs' , '.handlebars' ] ,
4143
4244 // partial expansion is only necessary for Mustache templates that have
4345 // style modifiers or pattern parameters (I think)
@@ -91,6 +93,31 @@ var engine_handlebars = {
9193 findPartial : function ( partialString ) {
9294 var partial = partialString . replace ( findPartialsRE , '$1' ) ;
9395 return partial ;
96+ } ,
97+
98+ spawnFile : function ( config , fileName ) {
99+ const paths = config . paths ;
100+ const metaFilePath = path . resolve ( paths . source . meta , fileName ) ;
101+ try {
102+ fs . statSync ( metaFilePath ) ;
103+ } catch ( err ) {
104+
105+ //not a file, so spawn it from the included file
106+ const metaFileContent = fs . readFileSync ( path . resolve ( __dirname , '..' , '_meta/' , fileName ) , 'utf8' ) ;
107+ fs . outputFileSync ( metaFilePath , metaFileContent ) ;
108+ }
109+ } ,
110+
111+ /**
112+ * Checks to see if the _meta directory has engine-specific head and foot files,
113+ * spawning them if not found.
114+ *
115+ * @param {object } config - the global config object from core, since we won't
116+ * assume it's already present
117+ */
118+ spawnMeta : function ( config ) {
119+ this . spawnFile ( config , '_00-head.hbs' ) ;
120+ this . spawnFile ( config , '_01-foot.hbs' ) ;
94121 }
95122} ;
96123
0 commit comments