Skip to content

Commit af599a4

Browse files
author
Brandon Morrison
committed
349: Implement configFileLoader helper methods, refactor config loader to use helper methods
1 parent e39eab9 commit af599a4

File tree

3 files changed

+70
-35
lines changed

3 files changed

+70
-35
lines changed

core/lib/config_file_loader.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
3+
const glob = require('glob'),
4+
_ = require('lodash'),
5+
path = require('path'),
6+
yaml = require('js-yaml');
7+
8+
/**
9+
*
10+
* @param dataFilesPath
11+
* @param fsDep
12+
* @returns {*}
13+
*/
14+
function loadFile(dataFilesPath, fsDep) {
15+
const dataFilesFullPath = dataFilesPath + '*.{json,yml,yaml}';
16+
17+
const dataFiles = glob.sync(dataFilesFullPath),
18+
dataFile = _.head(dataFiles);
19+
20+
if (fsDep.existsSync(path.resolve(dataFile))) {
21+
return yaml.safeLoad(fsDep.readFileSync(path.resolve(dataFile), 'utf8'));
22+
}
23+
24+
return null;
25+
}
26+
27+
/**
28+
*
29+
* @param dataFilesPath
30+
* @param excludeFileNames
31+
* @param fsDep
32+
* @returns {{}}
33+
*/
34+
function loadConfigFromFolder(dataFilesPath, excludeFileNames, fsDep) {
35+
const dataFilesFullPath = dataFilesPath + '*.{json,yml,yaml}',
36+
excludeFullPath = dataFilesPath + excludeFileNames + '.{json,yml,yaml}';
37+
38+
const dataFiles = glob.sync(dataFilesFullPath, {"ignore" : [excludeFullPath]});
39+
let mergeObject = {};
40+
41+
dataFiles.forEach(function (filePath) {
42+
let jsonData = yaml.safeLoad(fsDep.readFileSync(path.resolve(filePath), 'utf8'));
43+
mergeObject = _.merge(mergeObject, jsonData);
44+
});
45+
46+
return mergeObject;
47+
}
48+
49+
module.exports = function configFileLoader() {
50+
return {
51+
loadConfigFromFile: loadFile,
52+
loadConfigFromFolder: loadConfigFromFolder
53+
};
54+
};

core/lib/pattern_assembler.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var path = require('path'),
88
pph = require('./pseudopattern_hunter'),
99
mp = require('./markdown_parser'),
1010
plutils = require('./utilities'),
11+
configLoader = require('./config_file_loader')(),
1112
patternEngines = require('./pattern_engines'),
1213
lh = require('./lineage_hunter'),
1314
lih = require('./list_item_hunter'),
@@ -318,16 +319,13 @@ var pattern_assembler = function () {
318319

319320
//look for a json file for this template
320321
try {
321-
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".json");
322-
try {
323-
var jsonFilenameStats = fs.statSync(jsonFilename);
324-
} catch (err) {
325-
//not a file
326-
}
327-
if (jsonFilenameStats && jsonFilenameStats.isFile()) {
328-
currentPattern.jsonFileData = fs.readJSONSync(jsonFilename);
322+
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName);
323+
let configData = configLoader.loadConfigFromFile(jsonFilename, fs);
324+
325+
if (configData) {
326+
currentPattern.jsonFileData = configData;
329327
if (patternlab.config.debug) {
330-
console.log('processPatternIterative: found pattern-specific data.json for ' + currentPattern.patternPartial);
328+
console.log('processPatternIterative: found pattern-specific config data for ' + currentPattern.patternPartial);
331329
}
332330
}
333331
}
@@ -338,17 +336,14 @@ var pattern_assembler = function () {
338336

339337
//look for a listitems.json file for this template
340338
try {
341-
var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems.json");
342-
try {
343-
var listJsonFileStats = fs.statSync(listJsonFileName);
344-
} catch (err) {
345-
//not a file
346-
}
347-
if (listJsonFileStats && listJsonFileStats.isFile()) {
348-
currentPattern.listitems = fs.readJSONSync(listJsonFileName);
339+
var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems");
340+
let listItemsConfig = configLoader.loadConfigFromFile(listJsonFileName, fs);
341+
342+
if (listItemsConfig) {
343+
currentPattern.listitems = listItemsConfig;
349344
buildListItems(currentPattern);
350345
if (patternlab.config.debug) {
351-
console.log('found pattern-specific listitems.json for ' + currentPattern.patternPartial);
346+
console.log('found pattern-specific listitems config for ' + currentPattern.patternPartial);
352347
}
353348
}
354349
}

core/lib/patternlab.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
"use strict";
1212

1313
var diveSync = require('diveSync'),
14-
glob = require('glob'),
1514
_ = require('lodash'),
1615
path = require('path'),
1716
chalk = require('chalk'),
18-
yaml = require('js-yaml'),
1917
cleanHtml = require('js-beautify').html,
2018
inherits = require('util').inherits,
2119
pm = require('./plugin_manager'),
2220
fs = require('fs-extra'),
2321
packageInfo = require('../../package.json'),
22+
configLoader = require('./config_file_loader')(),
2423
plutils = require('./utilities'),
2524
PatternGraph = require('./pattern_graph').PatternGraph;
2625

@@ -46,14 +45,7 @@ var EventEmitter = require('events').EventEmitter;
4645
* @returns {{}}
4746
*/
4847
function buildPatternData(dataFilesPath, fsDep) {
49-
let dataFiles = glob.sync(dataFilesPath + '*.{json,yml,yaml}', {"ignore" : [dataFilesPath + 'listitems.{json,yml,yaml}']});
50-
let mergeObject = {};
51-
dataFiles.forEach(function (filePath) {
52-
let jsonData = yaml.safeLoad(fsDep.readFileSync(path.resolve(filePath), 'utf8'));
53-
mergeObject = _.merge(mergeObject, jsonData);
54-
});
55-
56-
return mergeObject;
48+
return configLoader.loadConfigFromFolder(dataFilesPath, 'listitems', fsDep);
5749
}
5850

5951
// GTP: these two diveSync pattern processors factored out so they can be reused
@@ -500,13 +492,7 @@ var patternlab_engine = function (config) {
500492
patternlab.data = {};
501493
}
502494
try {
503-
let dataFiles = glob.sync(paths.source.data + 'listitems.{json,yml,yaml}');
504-
let mergeObject = {};
505-
dataFiles.forEach(function (filePath) {
506-
let jsonData = yaml.safeLoad(fs.readFileSync(path.resolve(filePath), 'utf8'));
507-
mergeObject = _.merge(mergeObject, jsonData);
508-
});
509-
patternlab.listitems = mergeObject;
495+
patternlab.listitems = configLoader.loadConfigFromFile(path.resolve(paths.source.data, 'listitems.{json,yml,yaml}'));
510496
} catch (ex) {
511497
plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems file. Pattern Lab may not work without this file.');
512498
patternlab.listitems = {};

0 commit comments

Comments
 (0)