forked from Burstall/hedera-nft-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplodeNFTMetadata.js
153 lines (134 loc) · 4.03 KB
/
explodeNFTMetadata.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const readlineSync = require('readline-sync');
require('dotenv').config();
const fs = require('fs');
const { filesFromPath } = require('files-from-path');
const path = require('path');
const filenameRegex = /(\D+)\d+\.json$/i;
/**
* Helper function to handle command line arguments without any additional packages
* @param {string} arg the argument to look for
* @returns {string} the value specified for the argument
*/
function getArg(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
let customValue;
if (customIndex > -1) {
// Retrieve the value after --custom
customValue = process.argv[customIndex + 1];
}
return customValue;
}
/**
* Helper function to check if command line arguments exist
* @param {string} arg the command line argument to look for
* @returns {boolean}
*/
function getArgFlag(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
if (customIndex > -1) {
return true;
}
return false;
}
/**
* Used to expand the number of NFT metadata files in a directory
* @param {string} fullpath
* @param {number} qty
* @returns {Object} an index of the files pinned in the process
*/
async function explodeNFTMetadataInDirectory(fullpath, qty) {
const pinnedCIDs = {};
// scan for files
try {
// const fullpath = path.resolve(directory);
let maxFileIndex = 0;
const filenameList = [];
const metadataJSONStringList = [];
// parse the async iterator once to get the filenames
for await (const f of filesFromPath(fullpath)) {
// strip to get just the name
// TODO: test on windows
const filename = f.name.split('/').slice(-1);
filenameList.push(filename[0]);
maxFileIndex++;
}
console.log('Found ' +
filenameList.length +
' file to process @\n', fullpath, filenameList);
const proceed = readlineSync.keyInYNStrict('Do you want to expand from ' + maxFileIndex + ' to ' + qty + '?');
if (proceed) {
// read in the files
for (const f in filenameList) {
const filename = fullpath + '\\' + filenameList[f];
try {
const metadataJSONString = fs.readFileSync(filename, 'utf8');
const metadataObj = JSON.parse(metadataJSONString);
metadataObj.name = metadataObj.name.split('#')[0] + '#';
metadataJSONStringList.push(metadataObj);
}
catch (err) {
console.log(`ERROR: Could not read file (${filename})`, err);
process.exit(1);
}
}
for (let i = maxFileIndex; i < qty; i++) {
maxFileIndex++;
const offset = (maxFileIndex % metadataJSONStringList.length);
// copy the last one
const metadataObj = copy(metadataJSONStringList[offset]);
// update the name
metadataObj.name += maxFileIndex;
const filename = fullpath + '/' + filenameList[offset].match(filenameRegex)[1] + maxFileIndex + '.json';
// write the object out to file
fs.writeFile(filename, JSON.stringify(metadataObj), { flag: 'w' }, function(err) {
if (err) {return console.error(err);}
// read it back in to be sure it worked.
fs.readFile(filename, 'utf-8', function(err) {
if (err) {
console.log('Failed to write', metadataObj);
return console.error(err);
}
});
});
}
}
else {
console.log('User Aborted');
}
}
catch (err) {
console.log('ERROR in pinning process', err);
}
return pinnedCIDs;
}
function copy(mainObject) {
const objectCopy = {};
let key;
for (key in mainObject) {
objectCopy[key] = mainObject[key];
}
return objectCopy;
}
async function main() {
if (getArgFlag('h')) {
console.log('Usage: node explodeNFTMetadata.js -dir <directory> -qty XX');
console.log(' -dir <directory> read all files in directory');
console.log(' -qty XXXX expand number of files in sequential number');
process.exit(0);
}
if (getArgFlag('dir')) {
if (getArgFlag('qty')) {
const qty = getArg('qty');
const directory = getArg('dir');
const fullpath = path.resolve(directory);
await explodeNFTMetadataInDirectory(fullpath, qty);
}
else {
console.log('ERROR: no quantity specified');
}
}
else {
console.log('ERROR: no directory specified');
}
}
main();