biofriction-wp-theme/node_modules/panini/lib/loadData.js

30 lines
890 B
JavaScript
Raw Normal View History

2021-10-26 14:18:09 +02:00
var fs = require('fs');
var path = require('path');
var utils = require('./utils');
var yaml = require('js-yaml');
/**
* Looks for files with .js, .json, or .yml extensions within the given directory, and adds them as Handlebars variables matching the name of the file.
* @param {string} dir - Folder to check for data files.
*/
module.exports = function(dir) {
var dataFiles = utils.loadFiles(dir, '**/*.{js,json,yml}');
for (var i in dataFiles) {
var file = fs.readFileSync(dataFiles[i]);
var ext = path.extname(dataFiles[i]);
var name = path.basename(dataFiles[i], ext);
var data;
if (ext === '.json' || ext === '.js') {
delete require.cache[require.resolve(dataFiles[i])];
data = require(dataFiles[i])
}
else if (ext === '.yml') {
data = yaml.safeLoad(fs.readFileSync(dataFiles[i]));
}
this.data[name] = data;
}
}