prompts.ts 4.17 KB
import type { CreateOptions } from "../types";

import fs from "node:fs";
import fsp from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

import { cancel, confirm, isCancel, select, text } from "@clack/prompts";

export async function runCreateInteractive(): Promise<CreateOptions> {
	// 步骤1: 输入项目名
	const projectName = await text({
		message: "请输入项目名称:",
		placeholder: "venus-my-app",
		validate(value) {
			if (!value || value.trim().length === 0) {
				return "项目名称不能为空";
			}
			if (!/^[a-z0-9-]+$/.test(value)) {
				return "项目名称只能包含小写字母、数字和连字符";
			}
		},
	});
	if (!projectName) {
		cancel("项目名称不能为空");
		throw new Error("项目名称不能为空");
	}
	if (isCancel(projectName)) {
		cancel("操作已取消");
		throw new Error("操作已取消");
	}

	// 步骤2: 选择模板类型
	const template = await select<string>({
		message: "请选择模板类型:",
		options: [
			{ value: "vue", label: "Vue (PC 管理后台)" },
			{ value: "mobile", label: "Mobile (移动端 H5)" },
			{ value: "react", label: "React" },
		],
	});

	if (isCancel(template) || typeof template !== "string") {
		cancel("操作已取消");
		throw new Error("操作已取消");
	}

	// 步骤3: 输入构建目录
	const defaultDirPrefix = (() => {
		if (template === "react") return path.join("apps", "frontend", "react");
		if (template === "vue") return path.join("apps", "frontend", "vue");
		if (template === "mobile") return path.join("apps", "frontend", "mobile");
		return "";
	})();
	const dirInput = await text({
		message: "请输入构建目录前缀:",
		placeholder: defaultDirPrefix,
		initialValue: defaultDirPrefix,
	});

	if (isCancel(dirInput)) {
		cancel("操作已取消");
		throw new Error("操作已取消");
	}

	// 步骤4: 选择是否安装依赖
	const shouldInstall = await confirm({
		message: "是否立即安装依赖?",
		initialValue: true,
	});

	if (isCancel(shouldInstall)) {
		cancel("操作已取消");
		throw new Error("操作已取消");
	}

	// 步骤5: 选择可选功能 - Axios
	const hasAxios = await confirm({
		message: "是否启用 HTTP 请求库 (Axios)?",
		initialValue: false,
	});

	if (isCancel(hasAxios)) {
		cancel("操作已取消");
		throw new Error("操作已取消");
	}

	// 步骤6: 选择可选功能 - Router
	const hasRouter = await confirm({
		message: "是否启用路由 (Router)?",
		initialValue: false,
	});

	if (isCancel(hasRouter)) {
		cancel("操作已取消");
		throw new Error("操作已取消");
	}

	// 步骤7: 选择可选功能 - Store
	const hasStore = await confirm({
		message: "是否启用状态管理 (Store)?",
		initialValue: false,
	});

	if (isCancel(hasStore)) {
		cancel("操作已取消");
		throw new Error("操作已取消");
	}

	// 步骤8: 选择 UI 库
	const runtimeDir = path.dirname(fileURLToPath(import.meta.url));
	const tplRoot = path.resolve(runtimeDir, "../templates", template);
	const uiDir = path.join(tplRoot, "optional", "ui");
	let selectedUi: string | undefined;

	if (fs.existsSync(uiDir)) {
		const uiOptions = await fsp.readdir(uiDir, { withFileTypes: true });
		const availableUis = uiOptions.filter((u) => u.isDirectory()).map((u) => ({ value: u.name, label: u.name }));

		if (availableUis.length > 0) {
			const uiChoice = await select<string>({
				message: "请选择 UI 库 (可选):",
				options: [ { value: "", label: "不使用 UI 库" }, ...availableUis ],
			});

			if (isCancel(uiChoice) || typeof uiChoice !== "string") {
				cancel("操作已取消");
				throw new Error("操作已取消");
			}

			selectedUi = uiChoice === "" ? undefined : uiChoice;
		}
	}

	// 步骤9: 选择 ESLint
	const hasEslint = await confirm({
		message: "是否启用 ESLint?",
		initialValue: false,
	});

	if (isCancel(hasEslint)) {
		cancel("操作已取消");
		throw new Error("操作已取消");
	}

	// 返回配置
	return {
		name: String(projectName),
		template: String(template),
		dir: dirInput ? String(dirInput) : undefined,
		noInstall: !shouldInstall,
		axios: Boolean(hasAxios),
		router: Boolean(hasRouter),
		store: Boolean(hasStore),
		ui: selectedUi,
		eslint: Boolean(hasEslint),
	};
}