Updated javascript to merge

This commit is contained in:
Hugo Bernier 2024-03-24 16:24:10 -04:00 committed by GitHub
parent 191940cf41
commit 813bf24dc5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 9 deletions

View File

@ -2,26 +2,33 @@ const fs = require('fs');
const path = require('path');
const _ = require('lodash');
let result = {};
// Initialize an empty array to store the merged nodes
let mergedNodes = [];
function getDirectories(path) {
return fs.readdirSync(path, { withFileTypes: true })
function getDirectories(dirPath) {
return fs.readdirSync(dirPath, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
}
let directories = getDirectories('samples');
const baseDirectory = 'samples';
const directories = getDirectories(baseDirectory);
directories.forEach(directory => {
let assetsPath = path.join('samples', directory, 'assets');
const assetsPath = path.join(baseDirectory, directory, 'assets');
if (fs.existsSync(assetsPath)) {
let files = fs.readdirSync(assetsPath);
const files = fs.readdirSync(assetsPath);
files.forEach(file => {
if (file === 'sample.json') {
let data = JSON.parse(fs.readFileSync(path.join(assetsPath, file)));
result = _.merge(result, data);
const data = JSON.parse(fs.readFileSync(path.join(assetsPath, file), 'utf8'));
// Assuming data is an array of nodes, concatenate it to the mergedNodes array
mergedNodes = mergedNodes.concat(data);
}
});
}
});
fs.writeFileSync('samples.json', JSON.stringify(result));
// Write the merged nodes to a new JSON file
fs.writeFileSync('samples.json', JSON.stringify(mergedNodes, null, 2));
console.log('Merged nodes saved to samples.json');