-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathloadData.js
More file actions
29 lines (25 loc) · 890 Bytes
/
loadData.js
File metadata and controls
29 lines (25 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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;
}
}