forked from ItsNickBarry/hardhat-abi-exporter
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_abi.js
82 lines (63 loc) · 2.45 KB
/
export_abi.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
const fs = require('fs');
const path = require('path');
const { HardhatPluginError } = require('hardhat/plugins');
const { Interface, FormatTypes } = require('@ethersproject/abi');
const { types } = require('hardhat/config');
const {
TASK_COMPILE,
} = require('hardhat/builtin-tasks/task-names');
task(
'export-abi'
).addFlag(
'noCompile', 'Don\'t compile before running this task'
).setAction(async function (args, hre) {
if (!args.noCompile) {
await hre.run(TASK_COMPILE, { noExportAbi: true });
}
const configs = hre.config.abiExporter;
await Promise.all(configs.map(abiGroupConfig => {
return hre.run('export-abi-group', { abiGroupConfig })
}));
});
subtask(
'export-abi-group'
).addParam(
'abiGroupConfig', 'a single abi-exporter config object', undefined, types.any
).setAction(async function (args, hre) {
const { abiGroupConfig: config } = args;
const outputDirectory = path.resolve(hre.config.paths.root, config.path);
if (outputDirectory === hre.config.paths.root) {
throw new HardhatPluginError('resolved path must not be root directory');
}
const outputData = [];
const fullNames = await hre.artifacts.getAllFullyQualifiedNames();
await Promise.all(fullNames.map(async function (fullName) {
if (config.only.length && !config.only.some(m => fullName.match(m))) return;
if (config.except.length && config.except.some(m => fullName.match(m))) return;
let { abi, sourceName, contractName } = await hre.artifacts.readArtifact(fullName);
if (!abi.length) return;
abi = abi.filter((element, index, array) => config.filter(element, index, array, fullName));
if (config.pretty) {
abi = new Interface(abi).format(FormatTypes.minimal);
}
const destination = path.resolve(
outputDirectory,
config.rename(sourceName, contractName)
) + '.json';
outputData.push({ abi, destination });
}));
outputData.reduce(function (acc, { destination }) {
if (acc.has(destination)) {
throw new HardhatPluginError(`duplicate output destination: ${ destination }`);
}
acc.add(destination);
return acc;
}, new Set());
if (config.clear) {
await hre.run('clear-abi-group', { path: config.path });
}
await Promise.all(outputData.map(async function ({ abi, destination }) {
await fs.promises.mkdir(path.dirname(destination), { recursive: true });
await fs.promises.writeFile(destination, `${JSON.stringify(abi, null, config.spacing)}\n`, { flag: 'w' });
}));
});