/********************************************************************* * * * 初始化命令 * 创建时间: 2021-05-31 * 作者: 孟凯伦 * * **********************************************************************/ import path from "path"; import ICommand from "../core/iCommand"; import Command from "../core/commandDecorate"; import ReadLineUtils from "../utils/readLineUtils"; import FilesUtils from "../utils/filesUtils"; import TimeUtils from "../utils/timeUtils"; @Command("--init", "-i") export default class implements ICommand { public Description(): string { return "Initialize the project"; } public async Action(): Promise { const option = await this.CreateOption(); console.log("option:" + option); await this.CreateFiles(option); } /** * 获取工作路径 */ private get cwdPath(): string { return process.cwd(); } /** * 创建项目文件 */ private async CreateFiles(option: InitOption): Promise { // 防止标签为空 if (option.label.trim() === "") { option.label = "x-new-component"; } // 新建文件夹路径 const tempDirPath = path.join(this.cwdPath, option.label); // 使用组件标签名创建组件文件夹 await FilesUtils.CreateDir(tempDirPath) console.log(`-->Create ${option.label} folder.`); await this.CreateFile(tempDirPath, option); await this.CopyFileTo(tempDirPath); } /** * 创建文件到新建文件夹 * @param dirPath 文件夹路径 */ private async CreateFile(dirPath: string, option: InitOption): Promise { await FilesUtils.CreateAndWriteFile(path.join(dirPath, "build.json"), JSON.stringify({ ...option, entry: "index.js" }, null, 4)); console.log(`-->Create build.json file to ${dirPath}.`); await FilesUtils.CreateAndWriteFile(path.join(dirPath, "changelog.md"), this.getReadMimeContent(option)); console.log(`-->Create changelog.md file to ${dirPath}.`); } /** * 获取md文件类容 */ private getReadMimeContent(option: InitOption): string { let result = "# 组件更新说明\n\n"; result += `## ${option.version}\n\n`; result += `==========================\nupdate:${TimeUtils.GetLocalDate()}\n==========================\nfeat: 创建${option.name}`; return result; } /** * 拷贝文件到新建文件夹 * @param dirPath 文件夹路径 */ private async CopyFileTo(dirPath: string): Promise { const rootPath = path.join(__dirname, "../../template"); for (const fileName of this.getCopyFileList()) { await FilesUtils.CopyFile(path.join(rootPath, fileName + ".temp"), path.join(dirPath, fileName + ".ts")); console.log(`-->Copy ${fileName}.ts file to ${dirPath}.`); } } /** * 创建配置信息 */ private async CreateOption(): Promise { const option: InitOption = {} as InitOption; for (const item of this.getQuestionList()) { option[item.mapItem] = await ReadLineUtils.Input(item.question.padEnd(30, ' ')); } ReadLineUtils.Close(); return option; } /** * 获取拷贝文件列表 */ private getCopyFileList(): string[] { return [ "control", "data", "element", "events", "functions", "index", "properties", ] } /** * 获取问题列表 */ private getQuestionList(): InitQuestionItem[] { return [ { question: "Component Name?", mapItem: "name" }, { question: "Label Name?", mapItem: "label" }, { question: "Version Number?", mapItem: "version" }, { question: "Category Name?", mapItem: "categoryName" }, { question: "Category Key?", mapItem: "categoryKey" }, { question: "Component Type?", mapItem: "componentType" }, { question: "Author?", mapItem: "developer" }, ] } }