Skip to content

Instantly share code, notes, and snippets.

@Chmarusso
Created April 15, 2024 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chmarusso/46c4a595654dcc38823ae56f48800c51 to your computer and use it in GitHub Desktop.
Save Chmarusso/46c4a595654dcc38823ae56f48800c51 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
// here i have 10k json files delivered by collection owner
const dirPath = '/Users/user/Documents/collection_V1/json';
const updateContent = (filePath, content) => {
const match = filePath.match(/(\d+)\.json$/);
if (match) {
console.log(match[1]); // get tokenId from path
content.image = `ipfs://{directoryWithImages/${match[1]}.jpg` // upload all images to IPFS first
}
return content;
};
fs.readdir(dirPath, (err, files) => {
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
files.forEach((file, index) => {
if (path.extname(file) === '.json') {
const filePath = path.join(dirPath, file);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file from disk: ${err}`);
} else {
// parse JSON string to JSON object
const jsonData = JSON.parse(data);
// update content of JSON object
const updatedData = updateContent(filePath, jsonData);
// write updated content back to file
fs.writeFile(filePath, JSON.stringify(updatedData, null, 2), (err) => {
if (err) {
console.error(`Error writing file on disk: ${err}`);
}
});
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment