1 | import { scan } from './scan';
|
2 |
|
3 | export interface FindContractResult {
|
4 | filePath: string;
|
5 | name: string;
|
6 | }
|
7 |
|
8 | export const findContract = async (
|
9 | dir: string,
|
10 | name: string,
|
11 | ): Promise<FindContractResult> => {
|
12 | const contracts = await scan(dir);
|
13 | const found = Object.entries(contracts)
|
14 | .map(([path, contractNames]) => ({
|
15 | path,
|
16 | contractNames: contractNames.filter(
|
17 | (contractName) => name === contractName,
|
18 | ),
|
19 | }))
|
20 | .filter(({ contractNames }) => contractNames.length > 0);
|
21 | if (found.length > 1) {
|
22 | throw new Error(`Found multiple contracts with name ${name}`);
|
23 | } else if (found.length === 0) {
|
24 | throw new Error(`Cound not find contract with name ${name}`);
|
25 | }
|
26 |
|
27 | return {
|
28 | filePath: found[0].path,
|
29 | name,
|
30 | };
|
31 | };
|