-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathgenerateEsModulesFromJson.cjs
More file actions
98 lines (91 loc) · 3.12 KB
/
generateEsModulesFromJson.cjs
File metadata and controls
98 lines (91 loc) · 3.12 KB
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
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 },
],
[
'@iexec/poco/artifacts/contracts/IexecInterfaceToken.sol/IexecInterfaceToken.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 },
],
];
const createEsModule = (jsonObj) => {
let module = '// this file is auto generated do not edit it\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}`);
});