Skip to content

Commit 6ca768e

Browse files
committed
Moving more things logically into the PatternLab class
1 parent 9f2ac66 commit 6ca768e

File tree

1 file changed

+70
-55
lines changed

1 file changed

+70
-55
lines changed

core/lib/patternlab.js

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,70 @@ class PatternLab {
134134
this.cacheBuster = 0;
135135
}
136136
}
137+
138+
139+
// Pattern processing methods
140+
141+
/**
142+
* Process the user-defined pattern head and prepare it for rendering
143+
*/
144+
processHeadPattern() {
145+
try {
146+
const headPath = path.resolve(this.config.paths.source.meta, '_00-head.mustache');
147+
const headPattern = new Pattern(headPath, null, this);
148+
headPattern.template = fs.readFileSync(headPath, 'utf8');
149+
headPattern.isPattern = false;
150+
headPattern.isMetaPattern = true;
151+
pattern_assembler.decomposePattern(headPattern, this, true);
152+
this.userHead = headPattern.extendedTemplate;
153+
}
154+
catch (ex) {
155+
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');
156+
if (this.config.debug) { console.log(ex); }
157+
158+
// GTP: it seems increasingly naughty as we refactor to just unilaterally do this here,
159+
// but whatever. For now.
160+
process.exit(1);
161+
}
162+
}
163+
164+
/**
165+
* Process the user-defined pattern footer and prepare it for rendering
166+
*/
167+
processFootPattern() {
168+
try {
169+
const footPath = path.resolve(this.config.paths.source.meta, '_01-foot.mustache');
170+
const footPattern = new Pattern(footPath, null, this);
171+
footPattern.template = fs.readFileSync(footPath, 'utf8');
172+
footPattern.isPattern = false;
173+
footPattern.isMetaPattern = true;
174+
pattern_assembler.decomposePattern(footPattern, this, true);
175+
this.userFoot = footPattern.extendedTemplate;
176+
}
177+
catch (ex) {
178+
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');
179+
if (this.config.debug) { console.log(ex); }
180+
181+
// GTP: it seems increasingly naughty as we refactor to just unilaterally do this here,
182+
// but whatever. For now.
183+
process.exit(1);
184+
}
185+
}
186+
187+
188+
// info methods
189+
190+
getVersion() {
191+
return this.package.version;
192+
}
193+
logVersion() {
194+
console.log(this.package.version);
195+
}
196+
getSupportedTemplateExtensions() {
197+
return this.engines.getSupportedFileExtensions();
198+
}
199+
200+
137201
}
138202

139203
function buildPatternData(dataFilesPath, fsDep) {
@@ -275,18 +339,6 @@ const patternlab_engine = function (config) {
275339
const patternlab = new PatternLab(config);
276340
const paths = patternlab.config.paths;
277341

278-
function getVersion() {
279-
return patternlab.package.version;
280-
}
281-
282-
function logVersion() {
283-
console.log(patternlab.package.version);
284-
}
285-
286-
function getSupportedTemplateExtensions() {
287-
return patternlab.engines.getSupportedFileExtensions();
288-
}
289-
290342
function help() {
291343

292344
console.log('');
@@ -372,45 +424,7 @@ const patternlab_engine = function (config) {
372424
starterkit_manager.load_starterkit(starterkitName, clean);
373425
}
374426

375-
/**
376-
* Process the user-defined pattern head and prepare it for rendering
377-
*/
378-
function processHeadPattern() {
379-
try {
380-
const headPath = path.resolve(paths.source.meta, '_00-head.mustache');
381-
const headPattern = new Pattern(headPath, null, patternlab);
382-
headPattern.template = fs.readFileSync(headPath, 'utf8');
383-
headPattern.isPattern = false;
384-
headPattern.isMetaPattern = true;
385-
pattern_assembler.decomposePattern(headPattern, patternlab, true);
386-
patternlab.userHead = headPattern.extendedTemplate;
387-
}
388-
catch (ex) {
389-
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');
390-
if (patternlab.config.debug) { console.log(ex); }
391-
process.exit(1);
392-
}
393-
}
394427

395-
/**
396-
* Process the user-defined pattern footer and prepare it for rendering
397-
*/
398-
function processFootPattern() {
399-
try {
400-
const footPath = path.resolve(paths.source.meta, '_01-foot.mustache');
401-
const footPattern = new Pattern(footPath, null, patternlab);
402-
footPattern.template = fs.readFileSync(footPath, 'utf8');
403-
footPattern.isPattern = false;
404-
footPattern.isMetaPattern = true;
405-
pattern_assembler.decomposePattern(footPattern, patternlab, true);
406-
patternlab.userFoot = footPattern.extendedTemplate;
407-
}
408-
catch (ex) {
409-
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');
410-
if (patternlab.config.debug) { console.log(ex); }
411-
process.exit(1);
412-
}
413-
}
414428

415429
function writePatternFiles(headHTML, pattern, footerHTML) {
416430
const nullFormatter = str => str;
@@ -623,8 +637,9 @@ const patternlab_engine = function (config) {
623637
processAllPatternsRecursive(paths.source.patterns, patternlab);
624638

625639
//take the user defined head and foot and process any data and patterns that apply
626-
processHeadPattern();
627-
processFootPattern();
640+
// GTP: should these really be invoked from outside?
641+
patternlab.processHeadPattern();
642+
patternlab.processFootPattern();
628643

629644
//cascade any patternStates
630645
lineage_hunter.cascade_pattern_states(patternlab);
@@ -692,10 +707,10 @@ const patternlab_engine = function (config) {
692707

693708
return {
694709
version: function () {
695-
return logVersion();
710+
return patternlab.logVersion();
696711
},
697712
v: function () {
698-
return getVersion();
713+
return patternlab.getVersion();
699714
},
700715
build: function (callback, deletePatternDir) {
701716
if (patternlab && patternlab.isBusy) {
@@ -735,7 +750,7 @@ const patternlab_engine = function (config) {
735750
installPlugin(pluginName);
736751
},
737752
getSupportedTemplateExtensions: function () {
738-
return getSupportedTemplateExtensions();
753+
return patternlab.getSupportedTemplateExtensions();
739754
}
740755
};
741756
};

0 commit comments

Comments
 (0)