|
| 1 | +import type { CAC } from "cac"; |
| 2 | + |
| 3 | +import path from "node:path"; |
| 4 | +import { randomUUID } from "node:crypto"; |
| 5 | + |
| 6 | +import enquirer from "enquirer"; |
| 7 | + |
| 8 | +import { |
| 9 | + getVariantByFramework, |
| 10 | + isEmptyDir, |
| 11 | + isValidFramework, |
| 12 | + isValidPackageName, |
| 13 | + isValidVariant, |
| 14 | + loggerInfo, |
| 15 | + toValidPackageName, |
| 16 | +} from "@/shared/index"; |
| 17 | +import { ACTIVATION, FRAMEWORKS } from "@/shared/config"; |
| 18 | +import { TemplateOptions } from "@/shared/types"; |
| 19 | + |
| 20 | +interface PromptResult { |
| 21 | + projectName: string; |
| 22 | + framework: string; |
| 23 | + variant: string; |
| 24 | + overwrite: boolean; |
| 25 | + packageName: string; |
| 26 | +} |
| 27 | + |
| 28 | +export const template = async (options: TemplateOptions) => { |
| 29 | + if (ACTIVATION) { |
| 30 | + loggerInfo("template 参数信息: \n"); |
| 31 | + console.table(options); |
| 32 | + } |
| 33 | + |
| 34 | + const { projectName } = await enquirer.prompt<PromptResult>({ |
| 35 | + name: "projectName", |
| 36 | + type: "text", |
| 37 | + message: "请输入项目名称", |
| 38 | + initial: options.projectName, |
| 39 | + }); |
| 40 | + |
| 41 | + const { overwrite } = await enquirer.prompt<PromptResult>({ |
| 42 | + name: "overwrite", |
| 43 | + type: "confirm", |
| 44 | + skip: () => isEmptyDir(projectName), |
| 45 | + message: `${path.join( |
| 46 | + process.cwd(), |
| 47 | + projectName, |
| 48 | + )} \n 文件夹非空,继续操作会丢失现有的文件?`, |
| 49 | + result: (value) => { |
| 50 | + if (!isEmptyDir(projectName) && !value) { |
| 51 | + process.exit(1); |
| 52 | + } |
| 53 | + return value; |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + const { packageName } = await enquirer.prompt<PromptResult>({ |
| 58 | + name: "packageName", |
| 59 | + type: "text", |
| 60 | + skip: () => isValidPackageName(projectName), |
| 61 | + message: "请输入 package name", |
| 62 | + initial: projectName, |
| 63 | + validate: (value) => { |
| 64 | + return isValidPackageName(value) || "输入的 package name 不符合规范"; |
| 65 | + }, |
| 66 | + result: (value) => { |
| 67 | + return toValidPackageName(value); |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + const { framework } = await enquirer.prompt<PromptResult>({ |
| 72 | + name: "framework", |
| 73 | + type: "select", |
| 74 | + skip: () => isValidFramework(options.framework), |
| 75 | + message: "请选择下列的有效模板", |
| 76 | + choices: FRAMEWORKS.map((framework) => { |
| 77 | + const frameworkColor = framework.color; |
| 78 | + return { |
| 79 | + message: frameworkColor(framework.display || framework.name), |
| 80 | + name: framework.name, |
| 81 | + }; |
| 82 | + }), |
| 83 | + }); |
| 84 | + |
| 85 | + const { variant } = await enquirer.prompt<PromptResult>({ |
| 86 | + name: "variant", |
| 87 | + type: "select", |
| 88 | + skip: () => !isValidVariant(framework), |
| 89 | + message: "请选择下列的有效变体", |
| 90 | + choices: getVariantByFramework(framework).map((variant) => { |
| 91 | + const variantColor = variant.color; |
| 92 | + return { |
| 93 | + message: variantColor(variant.display || variant.name), |
| 94 | + name: variant.name, |
| 95 | + }; |
| 96 | + }), |
| 97 | + }); |
| 98 | + |
| 99 | + console.log(projectName, framework, variant, overwrite, packageName); |
| 100 | + |
| 101 | + const root = path.join(process.cwd(), projectName); |
| 102 | + // if (overwrite) emptyDir(root); |
| 103 | + // const template: string = variant || framework; |
| 104 | + // const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent); |
| 105 | + // const pkgManager = pkgInfo ? pkgInfo.name : "npm"; |
| 106 | + |
| 107 | + console.log(`\nScaffolding project in ${root}...`); |
| 108 | + // const templateDir = path.resolve( |
| 109 | + // fileURLToPath(import.meta.url), |
| 110 | + // "../../../template", |
| 111 | + // `template-${template}` |
| 112 | + // ); |
| 113 | + // console.log(templateDir); |
| 114 | + // fs.copySync(templateDir, projectName, { |
| 115 | + // filter: (src: string) => { |
| 116 | + // return !fileIgnore.find( |
| 117 | + // (f) => f === `${path.parse(src).name}${path.parse(src).ext}` |
| 118 | + // ); |
| 119 | + // }, |
| 120 | + // }); |
| 121 | +}; |
| 122 | + |
| 123 | +export default function templateInstaller(cli: CAC) { |
| 124 | + return { |
| 125 | + name: "templateInstaller", |
| 126 | + setup: () => { |
| 127 | + cli |
| 128 | + .command("template", "快速创建CodeGenius基础项目") |
| 129 | + .option("-n, --project-name <project-name>", "项目名称", { |
| 130 | + default: `project-${randomUUID().slice(0, 8)}`, |
| 131 | + }) |
| 132 | + .option("-f, --framework <framework>", "项目框架") |
| 133 | + .action(async (options) => { |
| 134 | + const { projectName, framework } = options; |
| 135 | + await template({ |
| 136 | + projectName, |
| 137 | + framework, |
| 138 | + }); |
| 139 | + }); |
| 140 | + }, |
| 141 | + }; |
| 142 | +} |
0 commit comments