Skip to content

Commit e41c98c

Browse files
committed
feat: add CLI arguments support for project name and template
- Add --project_name and --template CLI options using commander - Support non-interactive usage via command line flags - Implement template validation with fallback to interactive prompts - Maintain backward compatibility with existing interactive flow
1 parent 8fd4e6b commit e41c98c

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/bin/main.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env node
22
import { ExitPromptError } from "@inquirer/core";
3+
import { Command } from "commander";
34
import { inquireDappData } from "#src/util/dappData";
45
import { spawnTemplate } from "#src/util/spawnTemplate";
6+
import { availableTemplates } from "#src/templateConfigs/index";
57

68
/* This is somewhat a hack to automatically include
79
package.json and "src" folder into "dist"
@@ -12,7 +14,22 @@ import * as pkg from "../../package.json";
1214
(async () => {
1315
console.log(`${pkg.name} ${pkg.version}`);
1416

15-
const dappData = await inquireDappData();
17+
const program = new Command();
18+
19+
program
20+
.name(pkg.name)
21+
.description("CLI to create Polkadot dApps")
22+
.version(pkg.version)
23+
.option("--project-name <name>", "Name of the project")
24+
.option("--template <template>", `Template to use (${availableTemplates.join(", ")})`)
25+
.parse();
26+
27+
const options = program.opts();
28+
29+
const dappData = await inquireDappData({
30+
projectName: options.projectName,
31+
template: options.template,
32+
});
1633

1734
await spawnTemplate(dappData);
1835
})().catch((err: unknown) => {

src/util/dappData.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { input, select, Separator } from "@inquirer/prompts";
2-
import { configs, TemplateNames } from "#src/templateConfigs/index";
2+
import { configs, TemplateNames, availableTemplates } from "#src/templateConfigs/index";
33
import { TemplateConfig, TemplateType } from "#src/types";
44
import { colors as c } from "#src/util/log";
55

@@ -9,6 +9,11 @@ export type DappData = {
99
config: TemplateConfig;
1010
};
1111

12+
export type CliArgs = {
13+
projectName?: string;
14+
template?: string;
15+
};
16+
1217
type Choice = {
1318
value: TemplateNames;
1419
description: string;
@@ -36,12 +41,45 @@ function formatChoices(): (Choice | Separator)[] {
3641
return res;
3742
}
3843

39-
export async function inquireDappData(): Promise<DappData> {
40-
const name = await input({ message: "Project name", required: true, default: "my-polkadot-dapp" });
41-
const template: TemplateNames = await select<TemplateNames>({
44+
function validateTemplate(template: string): TemplateNames | null {
45+
if (availableTemplates.includes(template)) {
46+
return template as TemplateNames;
47+
}
48+
return null;
49+
}
50+
51+
async function promptForTemplate(): Promise<TemplateNames> {
52+
return await select<TemplateNames>({
4253
message: "Select template",
4354
choices: formatChoices(),
4455
});
56+
}
57+
58+
export async function inquireDappData(cliArgs: CliArgs = {}): Promise<DappData> {
59+
// Use CLI arg if provided, otherwise prompt
60+
let name: string;
61+
if (cliArgs.projectName) {
62+
name = cliArgs.projectName;
63+
} else {
64+
name = await input({ message: "Project name", required: true, default: "my-polkadot-dapp" });
65+
}
66+
67+
// Use CLI arg if provided and valid, otherwise prompt
68+
let template: TemplateNames;
69+
if (cliArgs.template) {
70+
const validatedTemplate = validateTemplate(cliArgs.template);
71+
if (validatedTemplate) {
72+
template = validatedTemplate;
73+
console.log(`Using template: ${c.primary(template)}`);
74+
} else {
75+
console.log(
76+
c.primary(`Invalid template "${cliArgs.template}". Available templates: ${availableTemplates.join(", ")}`),
77+
);
78+
template = await promptForTemplate();
79+
}
80+
} else {
81+
template = await promptForTemplate();
82+
}
4583

4684
return { name, config: configs[template], template };
4785
}

0 commit comments

Comments
 (0)