diff --git a/src/index.ts b/src/index.ts index f83d133..6a1d8c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,26 +8,22 @@ import { Finder } from "./Finder"; import { finderCompileAction } from "./tasks/compile"; import { finderAction } from "./tasks/finder"; import "./type-extensions"; +import { getFinderProxy } from "./utils"; extendConfig(finderConfigExtender); extendEnvironment((hre) => { - hre.finder = lazyObject(() => new Finder(hre)); + hre.finder = lazyObject(() => getFinderProxy(new Finder(hre))); }); task(TASK_FINDER) - .addOptionalPositionalParam( + .addOptionalParam( "path", "Path to the contract file.", undefined, types.inputFile ) - .addOptionalPositionalParam( - "name", - "Name of the contract.", - undefined, - types.string - ) + .addOptionalParam("name", "Name of the contract.", undefined, types.string) .addOptionalVariadicPositionalParam( "outputs", `Types of output the contract wants to print. All supported outputs: ${SUPPORTED_OUTPUTS.toString()}`, diff --git a/src/tasks/finder.ts b/src/tasks/finder.ts index 7950402..f5ba153 100644 --- a/src/tasks/finder.ts +++ b/src/tasks/finder.ts @@ -89,8 +89,8 @@ export const finderAction: ActionType = async ( const outputName = formatOutputName(output); const functionName = `get${outputName.pascalCaseFormat}`; const content = prettify - ? await finderProxy[functionName] - : JSON.stringify(await finderProxy[functionName]); + ? await (finderProxy as any)[functionName]() + : JSON.stringify(await (finderProxy as any)[functionName]()); useConsole( `======= ${outputName.humanReadableFormat} ======= (${finder.contractFullyQualifiedName})` diff --git a/src/utils.ts b/src/utils.ts index eb7c5c8..72419fb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,10 +8,10 @@ import type { Finder } from "./Finder"; export const uppercaseFirstChar = (str: string) => str.charAt(0).toUpperCase() + str.slice(1); -export const formatOutputName = (str: string, seperator: string = "-") => { - const words = str.split(seperator); - const uppercasedWords = words.map((word) => uppercaseFirstChar(word)); - const humanReadableFormat = uppercasedWords.join(" "); +export const formatOutputName = (str: string, separator: string = "-") => { + const words = str.split(separator); + const uppercaseWords = words.map((word) => uppercaseFirstChar(word)); + const humanReadableFormat = uppercaseWords.join(" "); const searchRegexp = new RegExp(" ", "g"); const pascalCaseFormat = humanReadableFormat.replace(searchRegexp, ""); @@ -21,22 +21,36 @@ export const formatOutputName = (str: string, seperator: string = "-") => { }; }; -export const getFinderProxy = (finder: Finder) => { +export const getFinderProxy = (finder: Finder): Finder => { const handler = { get(target: any, property: string) { try { - return target[property](); - } catch (error: any) { - if (!(error instanceof HardhatPluginError)) + if ( + property !== "setFor" && + (!target.contractPath || !target.contractName) + ) { throw new HardhatPluginError( PLUGIN_NAME, - `\nSomething went wrong in logic for '${property}' in ${target.constructor.name} class\n` + - `Error:\n` + - `name: ${error.name}\n` + - `message: ${error.message}\n` + - `stack: ${error.stack}`, - error + `\nYou have to set 'config.finder.contract' option or run 'Finder.setFor(...args)' function before using ${property} function.` ); + } + + // @ts-ignore + return Reflect.get(...arguments); + } catch (error: any) { + if (error instanceof HardhatPluginError) { + throw error; + } + + throw new HardhatPluginError( + PLUGIN_NAME, + `\nSomething went wrong with '${property}' in ${target.constructor.name} class\n` + + `Error:\n` + + `name: ${error?.name}\n` + + `message: ${error?.message}\n` + + `stack: ${error?.stack}`, + error + ); } }, };