Skip to content

Commit 9f2ac66

Browse files
committed
Move buildGlobalData() into PatternLab class.
1 parent cee0279 commit 9f2ac66

File tree

1 file changed

+65
-62
lines changed

1 file changed

+65
-62
lines changed

core/lib/patternlab.js

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,70 @@ class PatternLab {
7070
// TODO: determine if this is the best place to wire up plugins
7171
initializePlugins(this);
7272
}
73+
74+
buildGlobalData() {
75+
const paths = this.config.paths;
76+
77+
//
78+
// COLLECT GLOBAL LIBRARY DATA
79+
//
80+
81+
// data.json
82+
try {
83+
this.data = buildPatternData(paths.source.data, fs);
84+
} catch (ex) {
85+
plutils.error('missing or malformed' + paths.source.data + 'data.json Pattern Lab may not work without this file.');
86+
this.data = {};
87+
}
88+
// listitems.json
89+
try {
90+
this.listitems = fs.readJSONSync(path.resolve(paths.source.data, 'listitems.json'));
91+
} catch (ex) {
92+
plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems.json file. Pattern Lab may not work without this file.');
93+
this.listitems = {};
94+
}
95+
// load up all the necessary files from pattern lab that apply to every template
96+
try {
97+
this.header = fs.readFileSync(path.resolve(paths.source.patternlabFiles['general-header']), 'utf8');
98+
this.footer = fs.readFileSync(path.resolve(paths.source.patternlabFiles['general-footer']), 'utf8');
99+
this.patternSection = fs.readFileSync(path.resolve(paths.source.patternlabFiles.patternSection), 'utf8');
100+
this.patternSectionSubType = fs.readFileSync(path.resolve(paths.source.patternlabFiles.patternSectionSubtype), 'utf8');
101+
this.viewAll = fs.readFileSync(path.resolve(paths.source.patternlabFiles.viewall), 'utf8');
102+
} catch (ex) {
103+
console.log(ex);
104+
plutils.error('\nERROR: missing an essential file from ' + paths.source.patternlabFiles + '. Pattern Lab won\'t work without this file.\n');
105+
106+
// GTP: it seems increasingly naughty as we refactor to just unilaterally do this here,
107+
// but whatever. For now.
108+
process.exit(1);
109+
}
110+
111+
112+
//
113+
// INITIALIZE EMPTY GLOBAL DATA STRUCTURES
114+
//
115+
this.patterns = [];
116+
this.subtypePatterns = {};
117+
this.partials = {};
118+
this.data.link = {};
119+
120+
this.setCacheBust();
121+
122+
pattern_assembler.combine_listItems(this);
123+
124+
this.events.emit('patternlab-build-global-data-end', this);
125+
}
126+
127+
setCacheBust() {
128+
if (this.config.cacheBust) {
129+
if (this.config.debug) {
130+
console.log('setting cacheBuster value for frontend assets.');
131+
}
132+
this.cacheBuster = new Date().getTime();
133+
} else {
134+
this.cacheBuster = 0;
135+
}
136+
}
73137
}
74138

75139
function buildPatternData(dataFilesPath, fsDep) {
@@ -209,7 +273,6 @@ inherits(PatternLabEventEmitter, EventEmitter);
209273

210274
const patternlab_engine = function (config) {
211275
const patternlab = new PatternLab(config);
212-
213276
const paths = patternlab.config.paths;
214277

215278
function getVersion() {
@@ -299,17 +362,6 @@ const patternlab_engine = function (config) {
299362
// }
300363
}
301364

302-
function setCacheBust() {
303-
if (patternlab.config.cacheBust) {
304-
if (patternlab.config.debug) {
305-
console.log('setting cacheBuster value for frontend assets.');
306-
}
307-
patternlab.cacheBuster = new Date().getTime();
308-
} else {
309-
patternlab.cacheBuster = 0;
310-
}
311-
}
312-
313365
function listStarterkits() {
314366
const starterkit_manager = new sm(patternlab.config);
315367
return starterkit_manager.list_starterkits();
@@ -512,55 +564,6 @@ const patternlab_engine = function (config) {
512564
return PatternGraph.loadFromFile(patternlab);
513565
}
514566

515-
function buildGlobalData() {
516-
//
517-
// COLLECT GLOBAL LIBRARY DATA
518-
//
519-
520-
// data.json
521-
try {
522-
patternlab.data = buildPatternData(paths.source.data, fs);
523-
} catch (ex) {
524-
plutils.error('missing or malformed' + paths.source.data + 'data.json Pattern Lab may not work without this file.');
525-
patternlab.data = {};
526-
}
527-
// listitems.json
528-
try {
529-
patternlab.listitems = fs.readJSONSync(path.resolve(paths.source.data, 'listitems.json'));
530-
} catch (ex) {
531-
plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems.json file. Pattern Lab may not work without this file.');
532-
patternlab.listitems = {};
533-
}
534-
// load up all the necessary files from pattern lab that apply to every template
535-
try {
536-
patternlab.header = fs.readFileSync(path.resolve(paths.source.patternlabFiles['general-header']), 'utf8');
537-
patternlab.footer = fs.readFileSync(path.resolve(paths.source.patternlabFiles['general-footer']), 'utf8');
538-
patternlab.patternSection = fs.readFileSync(path.resolve(paths.source.patternlabFiles.patternSection), 'utf8');
539-
patternlab.patternSectionSubType = fs.readFileSync(path.resolve(paths.source.patternlabFiles.patternSectionSubtype), 'utf8');
540-
patternlab.viewAll = fs.readFileSync(path.resolve(paths.source.patternlabFiles.viewall), 'utf8');
541-
} catch (ex) {
542-
console.log(ex);
543-
plutils.error('\nERROR: missing an essential file from ' + paths.source.patternlabFiles + '. Pattern Lab won\'t work without this file.\n');
544-
process.exit(1);
545-
}
546-
547-
548-
//
549-
// INITIALIZE EMPTY GLOBAL DATA STRUCTURES
550-
//
551-
552-
patternlab.patterns = [];
553-
patternlab.subtypePatterns = {};
554-
patternlab.partials = {};
555-
patternlab.data.link = {};
556-
557-
setCacheBust();
558-
559-
pattern_assembler.combine_listItems(patternlab);
560-
561-
patternlab.events.emit('patternlab-build-global-data-end', patternlab);
562-
}
563-
564567

565568
function cleanBuildDirectory(incrementalBuildsEnabled) {
566569
if (incrementalBuildsEnabled) {
@@ -603,7 +606,7 @@ const patternlab_engine = function (config) {
603606
//
604607
cleanBuildDirectory(patternlab.incrementalBuildsEnabled);
605608

606-
buildGlobalData();
609+
patternlab.buildGlobalData();
607610

608611
// diveSync once to perform iterative populating of patternlab object
609612
return processAllPatternsIterative(paths.source.patterns, patternlab).then(() => {

0 commit comments

Comments
 (0)