forked from HashLips/hashlips_art_engine
-
Notifications
You must be signed in to change notification settings - Fork 200
/
metaplex.js
114 lines (102 loc) · 3.41 KB
/
metaplex.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
"use strict";
const fs = require("fs");
const path = require("path");
const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
const chalk = require("chalk");
const {
NFTName,
symbol,
description,
external_url,
royaltyFee,
creators,
} = require(path.join(basePath, "/Solana/solana_config.js"));
const { startIndex } = require(path.join(basePath, "/src/config.js"));
const imagesDir = `${basePath}/build/images`;
const jsonDir = `${basePath}/build/json`;
const metaplexFilePath = `${basePath}/build/solana`;
const metaplexDir = `${basePath}/build/solana`;
const setup = () => {
if (fs.existsSync(metaplexFilePath)) {
fs.rmSync(metaplexFilePath, {
recursive: true,
});
}
fs.mkdirSync(metaplexFilePath);
fs.mkdirSync(path.join(metaplexFilePath, "/json"));
fs.mkdirSync(path.join(metaplexFilePath, "/images"));
};
const getIndividualImageFiles = () => {
return fs
.readdirSync(imagesDir)
.filter((item) => /^[0-9]{1,6}.png/g.test(item));
};
const getIndividualJsonFiles = () => {
return fs
.readdirSync(jsonDir)
.filter((item) => /^[0-9]{1,6}.json/g.test(item));
};
setup();
console.log(chalk.bgGreenBright.black("Beginning Solana/Metaplex conversion"));
console.log({ startIndex });
console.log(
chalk.green(
`\nExtracting metaplex-ready files.\nWriting to folder: ${metaplexFilePath}`
)
);
// Rename all image files to n-1.png (to be zero indexed "start at zero") and store in solana/images
const imageFiles = getIndividualImageFiles();
imageFiles.forEach((file) => {
let nameWithoutExtension = file.slice(0, -4);
let editionCountFromFileName = Number(nameWithoutExtension);
let newEditionCount = editionCountFromFileName - startIndex;
fs.copyFile(
`${imagesDir}/${file}`,
path.join(`${metaplexDir}`, "images", `${newEditionCount}.png`),
() => {}
);
});
console.log(`\nFinished converting images to being metaplex-ready.\n`);
// Identify json files
const jsonFiles = getIndividualJsonFiles();
console.log(
chalk.green(`Found ${jsonFiles.length} json files in "${jsonDir}" to process`)
);
// Iterate, open and put in metadata list
jsonFiles.forEach((file) => {
let nameWithoutExtension = file.slice(0, -4);
let editionCountFromFileName = Number(nameWithoutExtension);
let newEditionCount = editionCountFromFileName - startIndex;
const rawData = fs.readFileSync(`${jsonDir}/${file}`);
const jsonData = JSON.parse(rawData);
let tempMetadata = {
name: NFTName + " " + jsonData.name,
symbol: symbol,
description: description,
seller_fee_basis_points: royaltyFee,
image: `${newEditionCount}.png`,
...(external_url !== "" && { external_url }),
attributes: jsonData.attributes,
properties: {
edition: jsonData.edition,
files: [
{
uri: `${newEditionCount}.png`,
type: "image/png",
},
],
category: "image",
creators: creators,
compiler: "HashLips Art Engine - NFTChef fork | qualifieddevs.io",
},
};
fs.writeFileSync(
path.join(`${metaplexDir}`, "json", `${newEditionCount}.json`),
JSON.stringify(tempMetadata, null, 2)
);
});
console.log(
`\nFinished converting json metadata files to being metaplex-ready.`
);
console.log(chalk.green(`\nConversion was finished successfully!\n`));