/
data-builder.js
22 lines (18 loc) · 1003 Bytes
/
data-builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const fs = require('fs');
const path = require('path');
const getSystemsDataFromSourceFiles = require('./utils/getSystemsDataFromSourceFiles');
const getTemplateStructure = require('./utils/getTemplateStructure');
const getTimestampFromFilePath = require('./utils/getTimestampFromFilePath');
const dataJSONPath = path.join(__dirname, '../src/data/data.json');
const systemsList = getSystemsDataFromSourceFiles();
const newTemplate = getTemplateStructure(systemsList);
// overwrite all the files with the new template
const updatedSystemsData = systemsList.map(({ filePath, data }) => {
const changedSystemData = { ...newTemplate, ...data };
// overwrite file with the changed system
fs.writeFileSync(filePath, JSON.stringify(changedSystemData, null, 2), 'utf-8');
changedSystemData.system.$addedAt = getTimestampFromFilePath(filePath);
return changedSystemData;
});
// Save systemsList to the data file
fs.writeFileSync(dataJSONPath, JSON.stringify(updatedSystemsData, null, 2), 'utf-8');