Skip to content

Commit

Permalink
[CLI] display file names when there are duplicate contract names (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-maj committed Dec 2, 2022
1 parent def4251 commit 3e87dd3
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-cherries-poke.md
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Display file name on duplicate contract names
16 changes: 16 additions & 0 deletions packages/cli/src/cli/index.ts
Expand Up @@ -101,6 +101,14 @@ $$$$$$\\ $$$$$$$\\ $$\\ $$$$$$\\ $$$$$$$ |$$\\ $$\\ $$\\ $$$$$$\\ $$$$
"-n, --name [name]",
"Name of the pre-built or released contract (such as nft-drop)",
)
.option(
"-f, --file [name]",
"Filter for contract files that contain this file name",
)
.option(
"-cn, --contract-name [name]",
"Filter for contracts that contain this contract name",
)
.option(
"-cv, --contract-version [version]",
"Version of the released contract",
Expand All @@ -120,6 +128,14 @@ $$$$$$\\ $$$$$$$\\ $$\\ $$$$$$\\ $$$$$$$ |$$\\ $$\\ $$\\ $$$$$$\\ $$$$
"Release your protocol so other devs can deploy them and unlock SDKs, Dashboards and Analytics",
)
.option("-p, --path <project-path>", "path to project", ".")
.option(
"-f, --file [name]",
"Filter for contract files that contain the file name",
)
.option(
"-cn, --contract-name [name]",
"Filter for contracts that contain this contract name",
)
.option("--dry-run", "dry run (skip actually publishing)")
.option("-d, --debug", "show debug logs")
.option("--ci", "Continuous Integration mode")
Expand Down
47 changes: 42 additions & 5 deletions packages/cli/src/common/processor.ts
Expand Up @@ -76,7 +76,7 @@ export async function processProject(
}
}

let compiledResult;
let compiledResult: { contracts: ContractPayload[] };
const compileLoader = spinner("Compiling project...");
try {
compiledResult = await build(projectPath, projectType);
Expand Down Expand Up @@ -106,10 +106,47 @@ export async function processProject(
if (options.ci) {
selectedContracts = compiledResult.contracts;
} else {
const choices = compiledResult.contracts.map((c) => ({
name: c.name,
value: c,
}));
const filtered = compiledResult.contracts
.filter((c) => {
if (typeof options.file === "string" && options.file.length > 0) {
return c.fileName.includes(options.file);
}

return true;
})
.filter((c) => {
if (
typeof options.contractName === "string" &&
options.contractName.length > 0
) {
return c.name.includes(options.contractName);
}

return true;
});

if (filtered.length === 0) {
logger.error(`No contracts found matching the specified filters.`);
process.exit(1);
}

const choices = filtered.map((c) => {
if (
compiledResult.contracts.filter(
(other: ContractPayload) => other.name === c.name,
).length > 1
) {
return {
name: `${c.name} - ${chalk.gray(c.fileName)}`,
value: c,
};
}

return {
name: c.name,
value: c,
};
});
const prompt = createContractsPrompt(
choices,
`Choose which contract(s) to ${command}`,
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/core/builder/brownie.ts
Expand Up @@ -55,6 +55,7 @@ export class BrownieBuilder extends BaseBuilder {
metadata: {},
bytecode,
sources: [], // TODO
fileName: "", // TODO
name: contractName,
});
break;
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/core/builder/foundry.ts
Expand Up @@ -88,6 +88,11 @@ export class FoundryBuilder extends BaseBuilder {
})
.filter((path) => path !== undefined) as string[];

const fileNames = Object.keys(
parsedMetadata?.settings?.compilationTarget || {},
);
const fileName = fileNames.length > 0 ? fileNames[0] : "";

if (
this.shouldProcessContract(
contractInfo.abi,
Expand All @@ -99,6 +104,7 @@ export class FoundryBuilder extends BaseBuilder {
name: contractName,
metadata,
bytecode,
fileName,
sources,
});
}
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/core/builder/hardhat.ts
Expand Up @@ -123,11 +123,17 @@ export class HardhatBuilder extends BaseBuilder {
})
.filter((path) => path !== undefined) as string[];

const fileNames = Object.keys(
meta?.settings?.compilationTarget || {},
);
const fileName = fileNames.length > 0 ? fileNames[0] : "";

if (this.shouldProcessContract(abi, deployedBytecode, contractName)) {
contracts.push({
metadata,
bytecode,
name: contractName,
fileName,
sources,
});
}
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/core/builder/solc.ts
Expand Up @@ -140,11 +140,17 @@ export class SolcBuilder extends BaseBuilder {
})
.filter((path) => path !== undefined) as string[];

const fileNames = Object.keys(
parsedMetadata?.settings?.compilationTarget || {},
);
const fileName = fileNames.length > 0 ? fileNames[0] : "";

if (this.shouldProcessContract(abi, deployedBytecode, contractName)) {
contracts.push({
metadata,
bytecode,
name: contractName,
fileName,
sources: _sources,
});
}
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/core/builder/truffle.ts
Expand Up @@ -72,11 +72,18 @@ export class TruffleBuilder extends BaseBuilder {
return undefined;
})
.filter((path) => path !== undefined) as string[];

const fileNames = Object.keys(
parsedMetadata?.settings?.compilationTarget || {},
);
const fileName = fileNames.length > 0 ? fileNames[0] : "";

if (this.shouldProcessContract(abi, deployedBytecode, contractName)) {
contracts.push({
metadata,
bytecode,
name: contractName,
fileName,
sources,
});
}
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/core/interfaces/ContractPayload.ts
Expand Up @@ -17,6 +17,11 @@ export interface ContractPayload {
*/
metadata: any;

/**
* The file name of the original compiled file
*/
fileName: string;

/**
* The source file paths
*/
Expand Down

0 comments on commit 3e87dd3

Please sign in to comment.