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