Skip to content

Commit

Permalink
feat(finder): add writeToFile option and outputDir parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
emretepedev committed Dec 29, 2024
1 parent bf95e62 commit 2570008
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ typings/

test/**/artifacts
test/**/cache
test/**/finder-outputs
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import "hardhat-finder";
This plugin adds the `finder` task to Hardhat:

```
Usage: hardhat [GLOBAL OPTIONS] finder [--colorify] [--compact] [--depth <INT>] [--include-dependencies] [--max-string-length <INT>] [--name <STRING>] [--no-compile] [--path <INPUTFILE>] [--prettify] [...outputs]
$ hardhat finder --prettify --colorify contracts/Example.sol Example abi bytecode
Usage: hardhat [GLOBAL OPTIONS] finder [--colorify] [--compact] [--name <STRING>] [--path <INPUTFILE>] [--depth <INT>] [--include-dependencies] [--max-string-length <INT>] [--no-compile] [--output-dir <STRING>] [--prettify] [--write-to-file] [...outputs]
$ hardhat finder --prettify --colorify --path contracts/Example.sol --name Example abi bytecode
@@@@@@@ contracts/Example.sol:Example @@@@@@@
======= Abi ======= (contracts/Example.sol:Example)
Expand Down Expand Up @@ -109,6 +109,8 @@ module.exports = {
| colorify | _Boolean_ | false | Colorize the outputs. |
| prettify | _Boolean_ | false | Beautify the outputs. |
| compact | _Boolean_ | false | Compact the outputs. |
| writeToFile | _Boolean_ | false | Write the outputs to a file. |
| outputDir | _String_ | finder-outputs | Directory to save the outputs. |
| noCompile | _Boolean_ | false | Don't compile before running this task. |
| runOnCompile | _Boolean_ | false | Run finder task during compile task. |

Expand Down
4 changes: 4 additions & 0 deletions src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
DEFAULT_INCLUDE_DEPENDENCIES,
DEFAULT_MAX_STRING_LENGTH,
DEFAULT_NO_COMPILE,
DEFAULT_OUTPUT_DIR,
DEFAULT_OUTPUTS,
DEFAULT_PRETTIFY,
DEFAULT_RUN_ON_COMPILE,
DEFAULT_WRITE_TO_FILE,
} from "~/constants";
import type { FinderConfig } from "~/types";

Expand All @@ -21,4 +23,6 @@ export const defaultFinderConfig: FinderConfig = {
compact: DEFAULT_COMPACT,
noCompile: DEFAULT_NO_COMPILE,
runOnCompile: DEFAULT_RUN_ON_COMPILE,
writeToFile: DEFAULT_WRITE_TO_FILE,
outputDir: DEFAULT_OUTPUT_DIR,
};
2 changes: 2 additions & 0 deletions src/constants/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ export const DEFAULT_PRETTIFY = false;
export const DEFAULT_COMPACT = false;
export const DEFAULT_NO_COMPILE = false;
export const DEFAULT_RUN_ON_COMPILE = false;
export const DEFAULT_WRITE_TO_FILE = false;
export const DEFAULT_OUTPUT_DIR = "finder-outputs";
24 changes: 24 additions & 0 deletions src/tasks/finder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { existsSync, mkdirSync, writeFileSync } from "fs";
import { task, types } from "hardhat/config";
import { HardhatPluginError } from "hardhat/plugins";
import type { ActionType } from "hardhat/types";
import { dirname } from "path";
import type { InspectOptions } from "util";
import { PLUGIN_NAME, SUPPORTED_OUTPUTS, TASK_FINDER } from "~/constants";
import type { ContractInfo, FinderTaskArguments } from "~/types";
Expand All @@ -24,6 +26,8 @@ const finderAction: ActionType<FinderTaskArguments> = async (
prettify,
compact,
noCompile,
writeToFile,
outputDir,
},
{ config, finder }
) => {
Expand All @@ -38,6 +42,8 @@ const finderAction: ActionType<FinderTaskArguments> = async (
prettify,
compact,
noCompile,
writeToFile,
outputDir,
} = prepareTaskArguments());

validateTaskArguments();
Expand Down Expand Up @@ -95,6 +101,15 @@ const finderAction: ActionType<FinderTaskArguments> = async (
`======= ${outputName.humanReadableFormat} ======= (${contractInfo.fullyQualifiedName})`
);
useInspectConsole(content, inspectOptions);

if (writeToFile) {
const filePath = `${outputDir}/${contractInfo.fullyQualifiedName}/${output}`;
const dirPath = dirname(filePath);
if (!existsSync(dirPath)) {
mkdirSync(dirPath, { recursive: true });
}
writeFileSync(filePath, content);
}
}
}

Expand All @@ -114,6 +129,8 @@ const finderAction: ActionType<FinderTaskArguments> = async (
prettify: prettify || config.finder.prettify,
compact: compact || config.finder.compact,
noCompile: noCompile || config.finder.noCompile,
writeToFile: writeToFile || config.finder.writeToFile,
outputDir: outputDir || config.finder.outputDir,
};
}

Expand Down Expand Up @@ -164,5 +181,12 @@ task<FinderTaskArguments>(TASK_FINDER)
.addFlag("prettify", "Beautify the outputs.")
.addFlag("compact", "Compact the outputs.")
.addFlag("noCompile", "Don't compile before running this task.")
.addFlag("writeToFile", "Write the outputs to a file.")
.addOptionalParam(
"outputDir",
"Directory to save the outputs.",
undefined,
types.string
)
.setDescription("Find various outputs of any existing contracts.")
.setAction(finderAction);
4 changes: 4 additions & 0 deletions src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface FinderConfig {
compact: boolean;
noCompile: boolean;
runOnCompile: boolean;
writeToFile: boolean;
outputDir: string;
}

export interface FinderUserConfig {
Expand All @@ -28,4 +30,6 @@ export interface FinderUserConfig {
compact?: boolean;
noCompile?: boolean;
runOnCompile?: boolean;
writeToFile: boolean;
outputDir: string;
}
2 changes: 2 additions & 0 deletions src/types/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface FinderTaskArguments {
prettify?: boolean;
compact?: boolean;
noCompile?: boolean;
writeToFile?: boolean;
outputDir?: string;
}

export interface CompilerTaskArguments {
Expand Down

0 comments on commit 2570008

Please sign in to comment.