This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbuild.js
53 lines (44 loc) · 1.4 KB
/
build.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const DATA_DIR = path.join(process.cwd(), 'data');
const DATA_FILE_NAME = 'data.json';
// Get FileNames of all Beer JSON files
const getFileNamesFromDir = (dir) => {
const promise = new Promise((resolve, reject) => {
fs.readdir(dir, (err, fileNamesArray) => {
err ? reject(err) : resolve(fileNamesArray);
});
});
return promise;
};
// There might be some invisible files in here such as .DS_Store so only get .json files
const filterNonJSONFiles = (fileNamesArray) => {
return _.filter(fileNamesArray, (f) => path.extname(f) === '.json');
};
// Merge all the json files together into a giant array
const mergeJSONFiles = (fileNamesArray) => {
const dataArray = [];
fileNamesArray.forEach((f) => {
dataArray.push(getFileData(f))
});
return dataArray;
};
// We need to get the data out of the file an easy way is to use require
const getFileData = (fileName) => {
const dataFilePath = path.join(DATA_DIR, fileName);
return require(dataFilePath);
};
// Pass all data to filseystem to write the file
const createDataFile = (data) => {
const jsonData = JSON.stringify(data)
fs.writeFile(DATA_FILE_NAME, jsonData, (err) => {
if (err) throw err;
console.log('File Saved!');
})
};
// Kick off
getFileNamesFromDir(DATA_DIR)
.then(filterNonJSONFiles)
.then(mergeJSONFiles)
.then(createDataFile)