Skip to content
9 changes: 9 additions & 0 deletions commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Command } from "commander";

const main = async () => {
console.log("Deploying example contract...");
};

export const deployCommand = new Command("deploy")
.description("Deploy example contract.")
.action(main);
6 changes: 6 additions & 0 deletions commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { deployCommand } from "./deploy";

// Export commands array for dynamic loading by the CLI
export default {
commands: [deployCommand],
};
5 changes: 5 additions & 0 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Command } from "commander";

export const runCommand = new Command("run")
.helpCommand(false)
.description("Run commands from the commands directory");
29 changes: 19 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ import { accountsCommand, solanaCommand } from "@zetachain/toolkit/commands";
import { Command } from "commander";

import { newCommand } from "./commands/new";
import { runCommand } from "./commands/run";
import { addLocalCommands } from "./utils/addLocalCommands";

const program: Command = new Command();
const program: Command = new Command().helpCommand(false);

program
.name("zetachain")
.description("CLI tool for ZetaChain development.")
.version("dev");
const main = async () => {
program
.name("zetachain")
.description("CLI tool for ZetaChain development.")
.version("dev");

program.addCommand(newCommand);
program.addCommand(localnetCommand);
program.addCommand(accountsCommand);
program.addCommand(solanaCommand);
program.addCommand(newCommand);
program.addCommand(localnetCommand);
program.addCommand(accountsCommand);
program.addCommand(solanaCommand);
program.addCommand(runCommand);

program.parse(process.argv);
await addLocalCommands(runCommand);

program.parse(process.argv);
};

main();
17 changes: 17 additions & 0 deletions src/utils/addLocalCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Command } from "commander";
import fs from "fs/promises";
import path from "path";

export const addLocalCommands = async (runCommand: Command) => {
try {
const commandsPath = path.join(process.cwd(), "commands", "index.ts");
await fs.access(commandsPath);

const module = await import(commandsPath);
if (module.default?.commands) {
for (const cmd of module.default.commands) {
runCommand.addCommand(cmd);
}
}
} catch (error) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do something with this catch block

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what we can do. Console logging something every time CLI doesn't find commands can be annoying.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no error to catch, then we shouldn't be using the try/catch pattern.

};