Created
April 15, 2024 12:56
-
-
Save Chmarusso/46c4a595654dcc38823ae56f48800c51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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