-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
63 lines (55 loc) · 1.62 KB
/
generate.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
require("dotenv").config()
const fs = require("fs")
const chainrand = require("chainrand")
const sharp = require("sharp")
function mkdir (dir) {
if (!fs.existsSync(dir))
fs.mkdirSync(dir)
return dir
}
const imageDir = mkdir("images")
const metadataDir = mkdir("metadata")
async function generate () {
const composeRaw = await fs.promises.readFile("compose.json")
const compose = JSON.parse(composeRaw)
const crng = chainrand.CRNG(process.env.RANDOMNESS + process.env.SEED_KEY)
const results = Array(compose.total).fill(1).map(x => {
return compose.attributes.map(a => {
let c = crng.choice(a.choices, a.choices.map(c => c.weight))
c.trait_type = a.trait_type
return c
})
})
console.log("Generating metadata...")
// Write the jsons
results.forEach(async (e, i) => {
const p = metadataDir + "/" + i + ".json"
const d = {
name: "Mini Sora #" + i,
description: (
"This is a sample generative NFT to demonstrate Chainrand. " +
"It is based on the Sora's Dreamworld project. \n" +
"[Chainrand](https:\/\/chainrand.io) \n" +
"[Sora's Dreamworld](https:\/\/sorasdreamworld.io)"
),
external_url: "https:\/\/chainrand.io",
attributes: e.map(c => {
return {
trait_type: c.trait_type,
value: c.value
}
})
}
await fs.promises.writeFile(p, JSON.stringify(d, null, 4))
})
console.log("Generating images...")
// Write the images
results.forEach(async (e, i) => {
await sharp(e[0].path)
.composite(e.slice(1).map(x => { return { input: x.path } }))
.toFile(imageDir + "/" + i + ".png", (err) => {
if (err !== null) console.log("Error: ", err)
})
})
}
generate()