-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmerge-config.js
100 lines (84 loc) · 2.66 KB
/
merge-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
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
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const yaml = require("js-yaml");
async function run() {
const protocolsPath = path.join(__dirname, 'protocols');
const combinedConfigPath = path.join(__dirname, 'config.json');
const protocolIds = fs.readdirSync(protocolsPath, { withFileTypes: true })
.filter(dirent => dirent.isDirectory()) // Filter only directories
.filter(dirent =>
fs.existsSync(path.join(protocolsPath, dirent.name, 'config.yaml')) ||
fs.existsSync(path.join(protocolsPath, dirent.name, 'config.json')))
.map(dirent => dirent.name);
const data = {protocols: []};
for (const protocolId of protocolIds) {
let protocolConfig;
const yamlConfigPath = path.join(protocolsPath, protocolId, 'config.yaml');
if (fs.existsSync(yamlConfigPath)) {
const protocolConfigStr = fs.readFileSync(yamlConfigPath, 'utf8');
protocolConfig = formatProtocolConfig({
id: protocolId,
...yaml.load(protocolConfigStr)
})
} else {
let jsonConfigPath = path.join(protocolsPath, protocolId, 'config.json');
const protocolConfigStr = fs.readFileSync(jsonConfigPath, 'utf8');
protocolConfig = protocolConfig = formatProtocolConfig({
id: protocolId,
...JSON.parse(protocolConfigStr)
})
}
const {icon} = protocolConfig;
const iconPath = path.join(__dirname, `protocols/${protocolId}/${icon}`);
protocolConfig.hash = await createMD5(iconPath);
data.protocols.push(protocolConfig);
}
fs.writeFile(combinedConfigPath, JSON.stringify(data, null, 2), (err) => {
if (err) {
console.error('Error writing to file', err);
}
});
}
function formatProtocolConfig(config) {
const {id, name, icon, category, metadata} = config;
const {pt, yt, lp} = metadata;
return {
id,
name,
icon,
category: category.toLowerCase(),
metadata: {
pt: formatMetadataAssets(pt),
yt: formatMetadataAssets(yt),
lp: formatMetadataAssets(lp),
},
};
}
function formatMetadataAssets(assets) {
const result = [];
for (const asset of (assets ?? [])) {
const {chainId, address, integrationUrl, description, subtitle} = asset;
result.push({
chainId,
address: address.toLowerCase(),
integrationUrl,
description,
subtitle,
})
}
return result;
}
function createMD5(filePath) {
return new Promise((res, rej) => {
const hash = crypto.createHash('md5');
const rStream = fs.createReadStream(filePath);
rStream.on('data', (data) => {
hash.update(data);
});
rStream.on('end', () => {
res(hash.digest('hex'));
});
})
}
void run();