|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | +const { loadImage } = require("canvas"); |
| 4 | + |
| 5 | +const basePath = process.cwd(); |
| 6 | +const buildDir = `${basePath}/build/json`; |
| 7 | +const inputDir = `${basePath}/build/images`; |
| 8 | +const namePrefix = "sneaker"; |
| 9 | +const description = "sneakernft world"; |
| 10 | +const baseUri = "ipfs://NewUriToReplace"; |
| 11 | +const console = require("console"); |
| 12 | + |
| 13 | +const metadataList = []; |
| 14 | + |
| 15 | +const buildSetup = () => { |
| 16 | + if (fs.existsSync(buildDir)) { |
| 17 | + fs.rmdirSync(buildDir, { recursive: true }); |
| 18 | + } |
| 19 | + fs.mkdirSync(buildDir); |
| 20 | +}; |
| 21 | + |
| 22 | +const getImages = (_dir) => { |
| 23 | + try { |
| 24 | + return fs |
| 25 | + .readdirSync(_dir) |
| 26 | + .filter((item) => { |
| 27 | + let extension = path.extname(`${_dir}${item}`); |
| 28 | + if (extension == ".png" || extension == ".jpg") { |
| 29 | + return item; |
| 30 | + } |
| 31 | + }) |
| 32 | + .map((i) => { |
| 33 | + return { |
| 34 | + filename: i, |
| 35 | + path: `${_dir}/${i}`, |
| 36 | + }; |
| 37 | + }); |
| 38 | + } catch { |
| 39 | + return null; |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +const loadImgData = async (_imgObject) => { |
| 44 | + try { |
| 45 | + const image = await loadImage(`${_imgObject.path}`); |
| 46 | + return { |
| 47 | + imgObject: _imgObject, |
| 48 | + loadedImage: image, |
| 49 | + }; |
| 50 | + } catch (error) { |
| 51 | + console.error("Error loading image:", error); |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +const saveMetadata = (_loadedImageObject) => { |
| 58 | + let shortName = _loadedImageObject.imgObject.filename.replace( |
| 59 | + /\.[^/.]+$/, |
| 60 | + "" |
| 61 | + ); |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + let tempMetadata = { |
| 66 | + name: `${namePrefix} #${shortName}`, |
| 67 | + description: description, |
| 68 | + image: `${baseUri}/${shortName}.png`, |
| 69 | +}; |
| 70 | + fs.writeFileSync( |
| 71 | + `${buildDir}/${shortName}.json`, |
| 72 | + JSON.stringify(tempMetadata, null, 2) |
| 73 | + ); |
| 74 | + metadataList.push(tempMetadata); |
| 75 | +}; |
| 76 | + |
| 77 | +const writeMetaData = (_data) => { |
| 78 | + fs.writeFileSync(`${buildDir}/_metadata.json`, _data); |
| 79 | +}; |
| 80 | + |
| 81 | +const startCreating = async () => { |
| 82 | + const images = getImages(inputDir); |
| 83 | + if (images == null) { |
| 84 | + console.log("Please generate collection first."); |
| 85 | + return; |
| 86 | + } |
| 87 | + let loadedImageObjects = []; |
| 88 | + images.forEach((imgObject) => { |
| 89 | + loadedImageObjects.push(loadImgData(imgObject)); |
| 90 | + }); |
| 91 | + await Promise.all(loadedImageObjects).then((loadedImageObjectArray) => { |
| 92 | + loadedImageObjectArray.forEach((loadedImageObject) => { |
| 93 | + |
| 94 | + saveMetadata(loadedImageObject); |
| 95 | + console.log( |
| 96 | + `Created metadata for image: ${loadedImageObject.imgObject.filename}` |
| 97 | + ); |
| 98 | + }); |
| 99 | + }); |
| 100 | + writeMetaData(JSON.stringify(metadataList, null, 2)); |
| 101 | +}; |
| 102 | + |
| 103 | +buildSetup(); |
| 104 | +startCreating(); |
0 commit comments