-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (171 loc) · 5.33 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
*/
const basePath = process.cwd();
const fs = require("fs");
const namePrefix = "Your Collection";
const description = "Remember to replace this description";
const baseUri = "ipfs://NewUriToReplace";
//套数x的路径,存放图片和完整的_metadata.json
const files1 = `${basePath}/files1`;
const files2 = `${basePath}/files2`;
//合并之后的路径
const mergeresult = `${basePath}/mergeresult`;
let attributesList = new Set();
let idList = new Set();
let metadataList = [];
const getMetaData = (path) => {
return JSON.parse(fs.readFileSync(path, 'utf8'));
}
const cleanName = (_str) => {
let nameWithoutExtension = _str.slice(0, -4);
return nameWithoutExtension;
};
const generateAllJson = (path) => {
let allJson = [];
fs
.readdirSync(path)
.filter((item) => /.(json)$/g.test(item))
.map((i, index) => {
let jsonfile = `${path}/${i}`;
const data = JSON.parse(fs.readFileSync(jsonfile, 'utf8'));
let attrs = data.attributes;
let newAttrs = [];
attrs.map((attr) => {
newAttrs.push(
{
"trait_type": attr.trait_type,
"value": attr.value
}
)
})
let newMeta = {
edition: i.slice(0, -5),
name: `${namePrefix} #${i}`,
description: description,
image: `${baseUri}/${i}.png`,
attributes: newAttrs,
};
allJson.push(newMeta);
});
//console.log(allJson);
fs.writeFileSync(
`${path}/json/_metadata.json`,
JSON.stringify(allJson, null, 2)
);
};
const getElements = (path) => {
//合并所有json文件
generateAllJson(path);
const filesJson = `${path}/json/_metadata.json`;
const metadatas = getMetaData(filesJson);
//console.log("metadatas",metadatas);
return fs
.readdirSync(path)
.filter((item) => /.(png)$/g.test(item))
.map((i, index) => {
if (i.includes("-")) {
throw new Error(`layer name can not contain dashes, please fix: ${i}`);
}
let name = cleanName(i);
//console.log(name,"name")
let meta = metadatas.filter((data) => { return data.edition == name });
let attributes = meta ? JSON.stringify(meta[0].attributes) : {};
return {
id: index,
name: name,
filename: i,
path: `${path}/${i}`,
meta: JSON.stringify(meta || {}),
attributes: attributes,
};
});
};
const buildSetup = () => {
if (fs.existsSync(mergeresult)) {
fs.rmdirSync(mergeresult, { recursive: true });
}
fs.mkdirSync(mergeresult);
fs.mkdirSync(`${mergeresult}/json`);
fs.mkdirSync(`${mergeresult}/images`);
};
const saveImage = (item) => {
var data = fs.readFileSync(item.path);
fs.writeFileSync(`${mergeresult}/images/${item.name}.png`, data);
console.log(`Writing image for ${item.name}`);
};
const saveMetaDataSingleFile = (item) => {
let meta = item.meta;
//console.log(meta)
fs.writeFileSync(
`${mergeresult}/json/${item.name}.json`,
JSON.stringify(meta, null, 2)
);
console.log(`Writing metadata for ${item.name}`);
};
const saveAllMetaData = (_data) => {
fs.writeFileSync(
`${mergeresult}/json/_metadata.json`,
JSON.stringify(_data, null, 2)
);
};
const generateId = (min, max) => {
let id = Math.floor(Math.random() * (max - min)) + min;
if(!idList.has(id))
{
idList.add(id);
return id;
}else{
generateId(min,max);
}
}
const startCreating = async () => {
try {
let res1 = getElements(files1);
let res2 = getElements(files2);
let res = res1.concat(res2);
res.map((item) => {
let attributes = item.attributes;
if (attributesList.has(attributes)) {
throw new Error(`attributes already exists: ${attributes}`);
} else {
attributesList.add(attributes);
}
});
let dateTime = Date.now();
for (let i = 1; i <= res.length; i++) {
//取随机id,唯一
let id =generateId(0,10000);
let item = res[i - 1];
item.name = id;
let oldMeta = JSON.parse(item.meta)[0];
let attrs = oldMeta.attributes;
let newAttrs = [];
attrs.map((attr) => {
newAttrs.push(attr)
})
//console.log(newAttrs)
let newMeta = {
name: `${namePrefix} #${id}`,
description: description,
image: `${baseUri}/${id}.png`,
//dna: oldMeta.dna,
//edition: id,
//date: dateTime,
attributes: newAttrs,
//compiler: oldMeta.compiler,
}
item.meta = newMeta;
saveImage(item);
saveMetaDataSingleFile(item);
metadataList.push(newMeta);
}
saveAllMetaData(metadataList);
}
catch (e) {
console.log(e)
}
}
buildSetup();
startCreating();
//generateAllJson(files1);