mirror of
https://github.com/zuiidea/antd-admin
synced 2026-11-03 00:14:00 +00:00
feat(create): interactive prompts for example, dir, name, pm
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { confirm, input, select } from "@inquirer/prompts";
|
||||
import type { PackageManagerName } from "./package-manager.js";
|
||||
import { CliError } from "./errors.js";
|
||||
import { normalizePackageName } from "./utils/normalize-package-name.js";
|
||||
|
||||
export async function promptExample(examples: string[]): Promise<string> {
|
||||
return select({
|
||||
message: "Which example do you want?",
|
||||
choices: examples.map((value) => ({ name: value, value })),
|
||||
});
|
||||
}
|
||||
|
||||
export async function promptTargetDir(): Promise<string> {
|
||||
const raw = await input({
|
||||
message:
|
||||
"Project directory (relative path, or '.' for current directory)?",
|
||||
default: "my-antd-admin",
|
||||
});
|
||||
const dir = (raw.trim() || "my-antd-admin").replace(/\\/g, "/");
|
||||
if (dir === ".") {
|
||||
const ok = await confirm({
|
||||
message: "Scaffold into the current directory?",
|
||||
default: false,
|
||||
});
|
||||
if (!ok) {
|
||||
throw new CliError("Aborted.", "INVALID_INPUT");
|
||||
}
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
export async function promptPackageName(defaultName: string): Promise<string> {
|
||||
const raw = await input({
|
||||
message: "npm package name for package.json?",
|
||||
default: defaultName,
|
||||
validate: (value) => {
|
||||
try {
|
||||
normalizePackageName(value);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return e instanceof Error ? e.message : String(e);
|
||||
}
|
||||
},
|
||||
});
|
||||
return normalizePackageName(raw);
|
||||
}
|
||||
|
||||
export async function promptPackageManager(
|
||||
available: PackageManagerName[]
|
||||
): Promise<PackageManagerName> {
|
||||
if (available.length === 0) {
|
||||
throw new CliError(
|
||||
"No supported package manager found in PATH (pnpm, npm, yarn, bun).",
|
||||
"INVALID_INPUT"
|
||||
);
|
||||
}
|
||||
return select({
|
||||
message: "Which package manager do you want to use?",
|
||||
choices: available.map((value) => ({ name: value, value })),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import type { PackageManagerName } from "./package-manager.js";
|
||||
|
||||
export type CreateCliOptions = {
|
||||
packageManager?: PackageManagerName;
|
||||
skipInstall: boolean;
|
||||
skipTransforms: boolean;
|
||||
example?: string;
|
||||
examplePath?: string;
|
||||
git: boolean;
|
||||
};
|
||||
|
||||
export type CreateContext = {
|
||||
example: string;
|
||||
targetDir: string;
|
||||
packageName: string;
|
||||
packageManager: PackageManagerName;
|
||||
};
|
||||
Reference in New Issue
Block a user