Skip to content

Commit e85e565

Browse files
committed
better tracking of uploaded metadata
1 parent c7d3714 commit e85e565

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

utils/nftport/mint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (!fs.existsSync(path.join(`${basePath}/build`, "/minted"))) {
1515

1616
async function main() {
1717
const ipfsMetas = JSON.parse(
18-
fs.readFileSync(`${basePath}/build/json/_ipfsMetas.json`)
18+
fs.readFileSync(`${basePath}/build/ipfsMetas/_ipfsMetas.json`)
1919
);
2020

2121
for (const meta of ipfsMetas) {

utils/nftport/uploadMetas.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fetch = require("node-fetch");
2+
const path = require("path");
23
const basePath = process.cwd();
34
const fs = require("fs");
45
const readDir = `${basePath}/build/json`; // change this directory if you are uploading generic images first in order to do a reveal.
@@ -8,46 +9,48 @@ const TIMEOUT = 1000; // Milliseconds. Extend this if needed to wait for each up
89

910
const allMetadata = [];
1011

12+
if (!fs.existsSync(path.join(`${basePath}/build`, "/ipfsMetas"))) {
13+
fs.mkdirSync(path.join(`${basePath}/build`, "ipfsMetas"));
14+
}
15+
1116
async function main() {
12-
const ipfsMetasFile = `${basePath}/build/json/_ipfsMetas.json`;
13-
fs.access(ipfsMetasFile, fs.F_OK, async (err) => {
14-
if (err) {
15-
fs.writeFileSync(ipfsMetasFile, "[]")
16-
}
17-
18-
const ipfsMetas = JSON.parse(fs.readFileSync(ipfsMetasFile));
19-
const files = fs.readdirSync(readDir);
20-
files.sort(function(a, b){
21-
return a.split(".")[0] - b.split(".")[0];
22-
});
23-
for (const file of files) {
24-
if (file !== "_metadata.json" && file !== "_ipfsMetas.json") {
25-
let jsonFile = fs.readFileSync(`${readDir}/${file}`);
26-
let metaData = JSON.parse(jsonFile);
27-
28-
const uploadedMeta = ipfsMetas.find(item => {
29-
return item.custom_fields.edition === metaData.custom_fields.edition;
30-
})
31-
32-
if(!uploadedMeta) {
33-
try {
34-
const response = await fetchWithRetry(jsonFile);
35-
allMetadata.push(response);
36-
console.log(`${response.name} metadata uploaded!`);
37-
} catch(err) {
38-
console.log(`Catch: ${err}`)
39-
}
17+
const files = fs.readdirSync(readDir);
18+
files.sort(function(a, b){
19+
return a.split(".")[0] - b.split(".")[0];
20+
});
21+
for (const file of files) {
22+
if (file !== "_metadata.json" && file !== "_ipfsMetas.json") {
23+
let jsonFile = fs.readFileSync(`${readDir}/${file}`);
24+
let metaData = JSON.parse(jsonFile);
25+
const uploadedMeta = `${basePath}/build/ipfsMetas/${metaData.custom_fields.edition}.json`;
26+
27+
try {
28+
fs.accessSync(uploadedMeta);
29+
const uploadedMetaFile = fs.readFileSync(uploadedMeta)
30+
if(uploadedMetaFile.length > 0) {
31+
const ipfsMeta = JSON.parse(uploadedMetaFile)
32+
if(ipfsMeta.response !== "OK") throw 'metadata not uploaded'
33+
allMetadata.push(ipfsMeta);
34+
console.log(`${metaData.name} metadata already uploaded`);
4035
} else {
41-
allMetadata.push(uploadedMeta);
42-
console.log(`${uploadedMeta.name} metadata already uploaded`);
36+
throw 'metadata not uploaded'
37+
}
38+
} catch(err) {
39+
try {
40+
const response = await fetchWithRetry(jsonFile);
41+
allMetadata.push(response);
42+
writeResponseMetaData(response)
43+
console.log(`${response.name} metadata uploaded!`);
44+
} catch(err) {
45+
console.log(`Catch: ${err}`)
4346
}
4447
}
4548
}
4649
fs.writeFileSync(
47-
`${basePath}/build/json/_ipfsMetas.json`,
50+
`${basePath}/build/ipfsMetas/_ipfsMetas.json`,
4851
JSON.stringify(allMetadata, null, 2)
4952
);
50-
})
53+
}
5154
}
5255

5356
main();
@@ -103,3 +106,7 @@ async function fetchWithRetry(file) {
103106
return fetch_retry(file);
104107
});
105108
}
109+
110+
const writeResponseMetaData = (_data) => {
111+
fs.writeFileSync(`${basePath}/build/ipfsMetas/${_data.custom_fields.edition}.json`, JSON.stringify(_data, null, 2));
112+
};

0 commit comments

Comments
 (0)