-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathgenerateEsModulesFromJson.cjs
138 lines (132 loc) · 4.63 KB
/
generateEsModulesFromJson.cjs
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
const path = require('path');
const { writeFile, stat, mkdir } = require('fs/promises');
const minifiers = {
package: ({ name, version, description }) => ({ name, version, description }),
abi: ({ abi }) => ({ abi }),
truffleDeployment: ({ abi, networks }) => ({
abi,
networks: Object.fromEntries(
Object.entries(networks).map(([chainId, { address }]) => [
chainId,
{ address },
]),
),
}),
hardhatDeployment: ({ address }) => ({ address }),
};
const sources = [
['./package.json', { dir: 'sdk', minifier: minifiers.package }],
[
'rlc-faucet-contract/build/contracts/RLC.json',
{ dir: '@iexec/rlc', minifier: minifiers.truffleDeployment },
],
[
'@iexec/poco/package.json',
{ dir: '@iexec/poco', minifier: minifiers.package },
],
[
'@iexec/poco/artifacts/contracts/registries/RegistryEntry.sol/RegistryEntry.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
// warn /build/
'@iexec/poco/build/contracts-min/ERC1538Proxy.json',
{ dir: '@iexec/poco', minifier: minifiers.truffleDeployment },
],
[
'@iexec/poco/artifacts/contracts/IexecInterfaceToken.sol/IexecInterfaceToken.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/IexecInterfaceNative.sol/IexecInterfaceNative.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/apps/AppRegistry.sol/AppRegistry.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/workerpools/WorkerpoolRegistry.sol/WorkerpoolRegistry.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/datasets/DatasetRegistry.sol/DatasetRegistry.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/apps/App.sol/App.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/workerpools/Workerpool.sol/Workerpool.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/datasets/Dataset.sol/Dataset.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/voucher-contracts/artifacts/contracts/beacon/Voucher.sol/Voucher.json',
{ dir: '@iexec/voucher-contracts', minifier: minifiers.abi },
],
[
'@iexec/voucher-contracts/artifacts/contracts/VoucherHub.sol/VoucherHub.json',
{ dir: '@iexec/voucher-contracts', minifier: minifiers.abi },
],
[
'@iexec/voucher-contracts/deployments/bellecour/VoucherHubERC1967Proxy.json',
{
dir: '@iexec/voucher-contracts/deployments/bellecour',
minifier: minifiers.hardhatDeployment,
},
],
[
'@ensdomains/ens-contracts/artifacts/contracts/registry/ENSRegistry.sol/ENSRegistry.json',
{ dir: '@ensdomains/registry', minifier: minifiers.abi },
],
[
'@ensdomains/ens-contracts/artifacts/contracts/registry/FIFSRegistrar.sol/FIFSRegistrar.json',
{ dir: '@ensdomains/registry', minifier: minifiers.abi },
],
[
'@ensdomains/ens-contracts/artifacts/contracts/reverseRegistrar/ReverseRegistrar.sol/ReverseRegistrar.json',
{ dir: '@ensdomains/registry', minifier: minifiers.abi },
],
[
'@ensdomains/ens-contracts/artifacts/contracts/resolvers/PublicResolver.sol/PublicResolver.json',
{ dir: '@ensdomains/resolvers', minifier: minifiers.abi },
],
];
const createEsModule = (jsonObj) => {
let module =
'// this file is auto generated do not edit it\n/* eslint-disable */\n';
Object.entries(jsonObj).forEach(([key, value]) => {
module += `export const ${key} = ${JSON.stringify(value)};\n`;
});
module += `export default { ${Object.keys(jsonObj).join(', ')} };`;
return module;
};
console.log('converting json files to es modules');
sources.map(async ([src, options]) => {
// eslint-disable-next-line import/no-dynamic-require, global-require
const jsonObj = require(src);
const minifiedJsonObj = options.minifier
? options.minifier(jsonObj)
: jsonObj;
const name =
(options && options.name) ||
`${src.split('/').pop().split('.json').shift()}.js`;
const module = createEsModule(minifiedJsonObj);
const outDir = path.join(`src/common/generated`, options && options.dir);
const outDirExists = await stat(outDir)
.then((outDirStat) => outDirStat.isDirectory())
.catch(() => false);
if (!outDirExists) {
await mkdir(outDir, {
recursive: true,
});
}
const outPath = path.join(outDir, name);
await writeFile(outPath, module);
console.log(`${src} => ${outPath}`);
});