Skip to content

Commit

Permalink
feat: use proxy for finder instead of directly
Browse files Browse the repository at this point in the history
  • Loading branch information
emretepedev committed Dec 24, 2022
1 parent 04dd0cf commit f3a50c8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
12 changes: 4 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`,
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export const finderAction: ActionType<FinderTaskArguments> = 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})`
Expand Down
42 changes: 28 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");

Expand All @@ -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
);
}
},
};
Expand Down

0 comments on commit f3a50c8

Please sign in to comment.