Skip to content

Commit d941ce8

Browse files
author
Brandon Morrison
committed
Rename config_loader to data_loader
1 parent af599a4 commit d941ce8

File tree

5 files changed

+82
-60
lines changed

5 files changed

+82
-60
lines changed

core/lib/config_file_loader.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

core/lib/data_loader.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"use strict";
2+
3+
const glob = require('glob'),
4+
_ = require('lodash'),
5+
path = require('path'),
6+
yaml = require('js-yaml');
7+
8+
/**
9+
* Loads a single config file, in yaml/json format.
10+
*
11+
* @param dataFilesPath - leave off the file extension.
12+
* @param fsDep
13+
* @returns {*}
14+
*/
15+
function loadFile(dataFilesPath, fsDep) {
16+
const dataFilesFullPath = dataFilesPath + '*.{json,yml,yaml}';
17+
18+
if (dataFilesPath) {
19+
const dataFiles = glob.sync(dataFilesFullPath),
20+
dataFile = _.head(dataFiles);
21+
22+
if (dataFile && fsDep.existsSync(path.resolve(dataFile))) {
23+
return yaml.safeLoad(fsDep.readFileSync(path.resolve(dataFile), 'utf8'));
24+
}
25+
}
26+
27+
return null;
28+
}
29+
30+
/**
31+
* Loads a set of config files from a folder, in yaml/json format.
32+
*
33+
* @param dataFilesPath - leave off the file extension
34+
* @param excludeFileNames - leave off the file extension
35+
* @param fsDep
36+
* @returns Object, with merged data files, empty object if no files.
37+
*/
38+
function loadDataFromFolder(dataFilesPath, excludeFileNames, fsDep) {
39+
const dataFilesFullPath = dataFilesPath + '*.{json,yml,yaml}',
40+
excludeFullPath = dataFilesPath + excludeFileNames + '.{json,yml,yaml}';
41+
42+
let globOptions = {};
43+
if (excludeFileNames) {
44+
globOptions.ignore = [excludeFullPath];
45+
}
46+
47+
const dataFiles = glob.sync(dataFilesFullPath, globOptions);
48+
let mergeObject = {};
49+
50+
dataFiles.forEach(function (filePath) {
51+
let jsonData = yaml.safeLoad(fsDep.readFileSync(path.resolve(filePath), 'utf8'));
52+
mergeObject = _.merge(mergeObject, jsonData);
53+
});
54+
55+
return mergeObject;
56+
}
57+
58+
module.exports = function configFileLoader() {
59+
return {
60+
loadDataFromFile: loadFile,
61+
loadDataFromFolder: loadDataFromFolder
62+
};
63+
};

core/lib/pattern_assembler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +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')(),
11+
dataLoader = require('./data_loader')(),
1212
patternEngines = require('./pattern_engines'),
1313
lh = require('./lineage_hunter'),
1414
lih = require('./list_item_hunter'),
@@ -320,7 +320,7 @@ var pattern_assembler = function () {
320320
//look for a json file for this template
321321
try {
322322
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName);
323-
let configData = configLoader.loadConfigFromFile(jsonFilename, fs);
323+
let configData = dataLoader.loadDataFromFile(jsonFilename, fs);
324324

325325
if (configData) {
326326
currentPattern.jsonFileData = configData;
@@ -337,7 +337,7 @@ var pattern_assembler = function () {
337337
//look for a listitems.json file for this template
338338
try {
339339
var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems");
340-
let listItemsConfig = configLoader.loadConfigFromFile(listJsonFileName, fs);
340+
let listItemsConfig = dataLoader.loadDataFromFile(listJsonFileName, fs);
341341

342342
if (listItemsConfig) {
343343
currentPattern.listitems = listItemsConfig;

core/lib/patternlab.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var diveSync = require('diveSync'),
1919
pm = require('./plugin_manager'),
2020
fs = require('fs-extra'),
2121
packageInfo = require('../../package.json'),
22-
configLoader = require('./config_file_loader')(),
22+
dataLoader = require('./data_loader')(),
2323
plutils = require('./utilities'),
2424
PatternGraph = require('./pattern_graph').PatternGraph;
2525

@@ -45,7 +45,7 @@ var EventEmitter = require('events').EventEmitter;
4545
* @returns {{}}
4646
*/
4747
function buildPatternData(dataFilesPath, fsDep) {
48-
return configLoader.loadConfigFromFolder(dataFilesPath, 'listitems', fsDep);
48+
return dataLoader.loadDataFromFolder(dataFilesPath, 'listitems', fsDep);
4949
}
5050

5151
// GTP: these two diveSync pattern processors factored out so they can be reused
@@ -492,7 +492,7 @@ var patternlab_engine = function (config) {
492492
patternlab.data = {};
493493
}
494494
try {
495-
patternlab.listitems = configLoader.loadConfigFromFile(path.resolve(paths.source.data, 'listitems.{json,yml,yaml}'));
495+
patternlab.listitems = dataLoader.loadDataFromFile(path.resolve(paths.source.data, 'listitems.{json,yml,yaml}'));
496496
} catch (ex) {
497497
plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems file. Pattern Lab may not work without this file.');
498498
patternlab.listitems = {};

test/data_loader_tests.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const tap = require('tap');
4+
5+
tap.test('loadDataFromFile - Load ', function(test){
6+
const fs = require('fs-extra'),
7+
dataLoader = require('../core/lib/data_loader')(),
8+
data_dir = './test/files/_data/';
9+
10+
let data = dataLoader.loadDataFromFile(data_dir + 'foo', fs);
11+
test.equals(data.foo, 'bar');
12+
test.end();
13+
});

0 commit comments

Comments
 (0)