From 25700084105e96d984ef099a97aae76bd0fefdbc Mon Sep 17 00:00:00 2001 From: emretepedev Date: Sun, 29 Dec 2024 07:50:20 +0300 Subject: [PATCH] feat(finder): add writeToFile option and outputDir parameter --- .gitignore | 1 + README.md | 6 ++++-- src/config/plugin.ts | 4 ++++ src/constants/task.ts | 2 ++ src/tasks/finder.ts | 24 ++++++++++++++++++++++++ src/types/plugin.ts | 4 ++++ src/types/task.ts | 2 ++ 7 files changed, 41 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6476726..74f17fd 100644 --- a/.gitignore +++ b/.gitignore @@ -64,3 +64,4 @@ typings/ test/**/artifacts test/**/cache +test/**/finder-outputs diff --git a/README.md b/README.md index f171d90..18bcf19 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,8 @@ import "hardhat-finder"; This plugin adds the `finder` task to Hardhat: ``` -Usage: hardhat [GLOBAL OPTIONS] finder [--colorify] [--compact] [--depth ] [--include-dependencies] [--max-string-length ] [--name ] [--no-compile] [--path ] [--prettify] [...outputs] -$ hardhat finder --prettify --colorify contracts/Example.sol Example abi bytecode +Usage: hardhat [GLOBAL OPTIONS] finder [--colorify] [--compact] [--name ] [--path ] [--depth ] [--include-dependencies] [--max-string-length ] [--no-compile] [--output-dir ] [--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) @@ -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. | diff --git a/src/config/plugin.ts b/src/config/plugin.ts index 7543ed5..656bb3d 100644 --- a/src/config/plugin.ts +++ b/src/config/plugin.ts @@ -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"; @@ -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, }; diff --git a/src/constants/task.ts b/src/constants/task.ts index 9223a5c..0c1d8b8 100644 --- a/src/constants/task.ts +++ b/src/constants/task.ts @@ -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"; diff --git a/src/tasks/finder.ts b/src/tasks/finder.ts index 6012fa3..cfbebb1 100644 --- a/src/tasks/finder.ts +++ b/src/tasks/finder.ts @@ -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"; @@ -24,6 +26,8 @@ const finderAction: ActionType = async ( prettify, compact, noCompile, + writeToFile, + outputDir, }, { config, finder } ) => { @@ -38,6 +42,8 @@ const finderAction: ActionType = async ( prettify, compact, noCompile, + writeToFile, + outputDir, } = prepareTaskArguments()); validateTaskArguments(); @@ -95,6 +101,15 @@ const finderAction: ActionType = 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); + } } } @@ -114,6 +129,8 @@ const finderAction: ActionType = 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, }; } @@ -164,5 +181,12 @@ task(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); diff --git a/src/types/plugin.ts b/src/types/plugin.ts index 98dc177..1c55d29 100644 --- a/src/types/plugin.ts +++ b/src/types/plugin.ts @@ -12,6 +12,8 @@ export interface FinderConfig { compact: boolean; noCompile: boolean; runOnCompile: boolean; + writeToFile: boolean; + outputDir: string; } export interface FinderUserConfig { @@ -28,4 +30,6 @@ export interface FinderUserConfig { compact?: boolean; noCompile?: boolean; runOnCompile?: boolean; + writeToFile: boolean; + outputDir: string; } diff --git a/src/types/task.ts b/src/types/task.ts index 3335dcd..a4b2502 100644 --- a/src/types/task.ts +++ b/src/types/task.ts @@ -9,6 +9,8 @@ export interface FinderTaskArguments { prettify?: boolean; compact?: boolean; noCompile?: boolean; + writeToFile?: boolean; + outputDir?: string; } export interface CompilerTaskArguments {