forked from hedera-dev/hedera-code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-metadata.js
72 lines (58 loc) · 2.12 KB
/
upload-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
const { PinataSDK } = require("pinata");
const fs = require("fs");
require("dotenv").config();
const pinata = new PinataSDK({
pinataJwt: process.env.PINATA_JWT,
pinataGateway: process.env.PINATA_GATEWAY,
});
async function uploadJsonToIpfs() {
console.log(`\n=======================================`);
console.log(`- Uploading JSON metadata to IPFS...`);
// Load metadata from a file (metadata-example.json) - replace with name of your file if not using the example
let metadata;
try {
const data = fs.readFileSync('metadata-example.json', 'utf8');
metadata = JSON.parse(data);
} catch (err) {
console.error("Error reading metadata file:", err);
return;
}
try {
// Upload JSON to IPFS via Pinata
const upload = await pinata.upload.json(metadata).addMetadata({
name: `metadata.json`,
});
const ipfsHash = upload.IpfsHash;
console.log(`Uploaded JSON successfully: ${ipfsHash}`);
// Write the IPFS CID to the .env file
updateEnvFile("IPFS_CID", ipfsHash);
} catch (error) {
console.error(`Error uploading JSON:`, error);
}
}
uploadJsonToIpfs();
// ------------------------------------------------------------
// Helper Functions
// ------------------------------------------------------------
// Helper function to read .env file
function readEnvFile() {
if (fs.existsSync('.env')) {
return fs.readFileSync('.env', 'utf8');
}
return '';
}
// Helper function to update or add a key in the .env file
function updateEnvFile(key, value) {
let envFileContent = readEnvFile();
const keyString = `${key}=${value}\n`;
if (envFileContent.includes(`${key}=`)) {
// Replace the existing key value
const updatedEnvContent = envFileContent.replace(new RegExp(`${key}=.*`), keyString.trim() + '\n');
fs.writeFileSync('.env', updatedEnvContent, 'utf8');
console.log(`${key} updated in .env file.`);
} else {
// Append the new key if it doesn't exist
fs.appendFileSync('.env', '\n' + keyString, 'utf8');
console.log(`${key} added to .env file.`);
}
}