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