-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmustache.config.js
60 lines (54 loc) · 1.43 KB
/
mustache.config.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
const Mustache = require('mustache')
const fs = require('fs')
// receive network name as argument
async function main(args) {
const networkName = getNetworkName(args)
const networkData = require(`./config/${networkName}.json`)
const template = fs.readFileSync('./subgraph.template.yaml', 'utf8')
const parsedData = putFirstElementFlag(networkData)
const output = Mustache.render(template, parsedData)
writeOutput(output)
}
function getNetworkName(args) {
if (args.length < 3) {
throw new Error('Missing network name argument')
}
return args[2]
}
function writeOutput(output) {
fs.writeFileSync('./subgraph.yaml', output)
}
function putFirstElementFlag(data) {
const dataWithFirstElementFlag = replaceFirstElementFlag(data)
return arrayToObj(dataWithFirstElementFlag)
}
function replaceFirstElementFlag(data) {
return Object.keys(data).map((key, index) => {
if (Array.isArray(data[key]) && data[key].length > 0) {
return {
[key]: data[key].map((item, index) => {
if (index === 0) {
return { ...item, name: key }
} else {
return item
}
}),
}
}
return { [key]: data[key] }
})
}
function arrayToObj(data) {
return data.reduce((acc, curr) => {
return { ...acc, ...curr }
}, {})
}
main(process.argv)
.then(() => {
console.log('Done')
process.exit()
})
.catch(err => {
console.error(err)
process.exit(1)
})