Skip to content

Commit

Permalink
chore: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
thelostone-mc committed Jul 2, 2022
1 parent 1884a08 commit 440a0c6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
17 changes: 16 additions & 1 deletion README.md
Expand Up @@ -29,7 +29,8 @@ Add configuration under the `abiExporter` key:
| `only` | `Array` of `String` matchers used to select included contracts, defaults to all contracts if `length` is 0 | `[]` |
| `except` | `Array` of `String` matchers used to exclude contracts | `[]` |
| `spacing` | number of spaces per indentation level of formatted output | `2` |
| `format` | format type ("json", "minimal", "humanReadable") | `json` |
| `pretty` | whether to use interface-style formatting of output for better readability | `false` |
| `format` | format type ("json", "minimal", "fullName"). Alternative to `pretty` | `json` |
| `filter` | `Function` with signature `(abiElement: any, index: number, abi: any, fullyQualifiedName: string) => boolean` used to filter elements from each exported ABI | `() => true` |
| `rename` | `Function` with signature `(sourceName: string, contractName: string) => string` used to rename an exported ABI (incompatible with `flat` option) | `undefined` |

Expand All @@ -43,9 +44,23 @@ abiExporter: {
flat: true,
only: [':ERC20$'],
spacing: 2,
pretty: true,
format: "minimal",
}

// or

abiExporter: [
{
path: './abi/pretty',
pretty: true,
},
{
path: './abi/ugly',
pretty: false,
},
]

// or

abiExporter: [
Expand Down
4 changes: 3 additions & 1 deletion index.d.ts
Expand Up @@ -8,6 +8,7 @@ interface AbiExporterUserConfig {
only?: string[],
except?: string[],
spacing?: number,
pretty?: boolean,
format?: string,
filter?: (abiElement: any, index: number, abi: any, fullyQualifiedName: string) => boolean,
rename?: (sourceName: string, contractName: string) => string,
Expand All @@ -27,7 +28,8 @@ declare module 'hardhat/types/config' {
only: string[],
except: string[],
spacing: number,
format: string,
pretty?: boolean,
format?: string,
filter: (abiElement: any, index: number, abi: any, fullyQualifiedName: string) => boolean,
rename: (sourceName: string, contractName: string) => string,
}[]
Expand Down
16 changes: 14 additions & 2 deletions index.js
Expand Up @@ -15,9 +15,10 @@ const DEFAULT_CONFIG = {
only: [],
except: [],
spacing: 2,
format: "json",
pretty: false,
filter: () => true,
// `rename` is not defaulted as it may depend on `flat` option
// `format` is not defaulted as it may depend on `pretty` option
};

function validate(config, key, type) {
Expand All @@ -42,13 +43,17 @@ extendConfig(function (config, userConfig) {
validate(conf, 'only', 'array');
validate(conf, 'except', 'array');
validate(conf, 'spacing', 'number');
validate(conf, 'format', 'string');
validate(conf, 'pretty', 'boolean');
validate(conf, 'filter', 'function');

if (conf.flat && typeof conf.rename !== 'undefined') {
throw new HardhatPluginError(PLUGIN_NAME, '`flat` & `rename` config cannot be specified together');
}

if (conf.pretty && typeof conf.format !== 'undefined') {
throw new HardhatPluginError(PLUGIN_NAME, '`pretty` & `format` config cannot be specified together');
}

if (conf.flat) {
conf.rename = (sourceName, contractName) => contractName;
}
Expand All @@ -58,6 +63,13 @@ extendConfig(function (config, userConfig) {
}

validate(conf, 'rename', 'function');

if (!config.format) {
conf.format = conf.pretty ? "minimal": "json";
}

validate(conf, 'format', 'string');

return conf;
});
});
8 changes: 5 additions & 3 deletions tasks/export_abi.js
Expand Up @@ -48,12 +48,14 @@ subtask(

if (!abi.length) return;

abi = abi.filter((element, index, array) => config.filter(element, index, array, fullName));

if (config.format == "minimal") {
if (config.format == "json") {
abi = abi.filter((element, index, array) => config.filter(element, index, array, fullName));
} else if (config.format == "minimal") {
abi = new Interface(abi).format(FormatTypes.minimal);
} else if (config.format == "fullName") {
abi = new Interface(abi).format(FormatTypes.fullName);
} else {
throw new HardhatPluginError(`Unknown format: ${config.format}`);
}

const destination = path.resolve(
Expand Down

0 comments on commit 440a0c6

Please sign in to comment.