-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #680 from pattern-lab/dev
Pattern Lab Node Core 2.11.0
- Loading branch information
Showing
13 changed files
with
133 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"use strict"; | ||
|
||
const glob = require('glob'), | ||
_ = require('lodash'), | ||
path = require('path'), | ||
yaml = require('js-yaml'); | ||
|
||
/** | ||
* Loads a single config file, in yaml/json format. | ||
* | ||
* @param dataFilesPath - leave off the file extension. | ||
* @param fsDep | ||
* @returns {*} | ||
*/ | ||
function loadFile(dataFilesPath, fsDep) { | ||
const dataFilesFullPath = dataFilesPath + '*.{json,yml,yaml}'; | ||
|
||
if (dataFilesPath) { | ||
const dataFiles = glob.sync(dataFilesFullPath), | ||
dataFile = _.head(dataFiles); | ||
|
||
if (dataFile && fsDep.existsSync(path.resolve(dataFile))) { | ||
return yaml.safeLoad(fsDep.readFileSync(path.resolve(dataFile), 'utf8')); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Loads a set of config files from a folder, in yaml/json format. | ||
* | ||
* @param dataFilesPath - leave off the file extension | ||
* @param excludeFileNames - leave off the file extension | ||
* @param fsDep | ||
* @returns Object, with merged data files, empty object if no files. | ||
*/ | ||
function loadDataFromFolder(dataFilesPath, excludeFileNames, fsDep) { | ||
const dataFilesFullPath = dataFilesPath + '*.{json,yml,yaml}', | ||
excludeFullPath = dataFilesPath + excludeFileNames + '.{json,yml,yaml}'; | ||
|
||
let globOptions = {}; | ||
if (excludeFileNames) { | ||
globOptions.ignore = [excludeFullPath]; | ||
} | ||
|
||
const dataFiles = glob.sync(dataFilesFullPath, globOptions); | ||
let mergeObject = {}; | ||
|
||
dataFiles.forEach(function (filePath) { | ||
let jsonData = yaml.safeLoad(fsDep.readFileSync(path.resolve(filePath), 'utf8')); | ||
mergeObject = _.merge(mergeObject, jsonData); | ||
}); | ||
|
||
return mergeObject; | ||
} | ||
|
||
module.exports = function configFileLoader() { | ||
return { | ||
loadDataFromFile: loadFile, | ||
loadDataFromFolder: loadDataFromFolder | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const tap = require('tap'); | ||
|
||
tap.test('loadDataFromFile - Load ', function(test){ | ||
const fs = require('fs-extra'), | ||
dataLoader = require('../core/lib/data_loader')(), | ||
data_dir = './test/files/_data/'; | ||
|
||
let data = dataLoader.loadDataFromFile(data_dir + 'foo', fs); | ||
test.equals(data.foo, 'bar'); | ||
test.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
{ "data" : "test" } | ||
{ "data" : "test", "from_json" : "from_json" } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from_yaml: "from_yaml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from_yml: "from_yml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters