From 93b7686887b9f3dbe066aecfdf847702ba78cbc2 Mon Sep 17 00:00:00 2001 From: Titor Date: Wed, 3 Mar 2021 14:41:15 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E8=A7=A3=E5=86=B3=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E8=A1=8C=E6=89=8B=E5=8A=A8=E9=80=80=E5=87=BA=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改手动退出命令时,输出的错误信息 --- extend/generator.js | 73 +++++++++++++++++++++++++++++++++++++++++++++ extend/generator.ts | 5 +++- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 extend/generator.js diff --git a/extend/generator.js b/extend/generator.js new file mode 100644 index 0000000..e1b1118 --- /dev/null +++ b/extend/generator.js @@ -0,0 +1,73 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Generator = void 0; +const prompts_1 = __importDefault(require("prompts")); +const file_1 = require("./file"); +const ejs_1 = __importDefault(require("ejs")); +const path_1 = require("path"); +const console_1 = require("./console"); +class Generator { + constructor() { + this.answers = null; + this.RUN = path_1.join(path_1.resolve(process.cwd()), path_1.sep); + let dir = this.dir = path_1.join(path_1.resolve(__dirname)); + let path = this.PATH = this.dir.substring(0, dir.lastIndexOf("extend")); + this.TPL = path_1.join(path, "template/"); + this.run().catch(() => { + console.log(console_1.warning("🦠 项目创建失败,程序已自动退出")); + }); + } + /* + * 自动指令中枢: + * @description 自动调用 CLI 过程中的5大阶段 + */ + run() { + return __awaiter(this, void 0, void 0, function* () { + yield this.Initialize(); + yield this.Prompting(); + yield this.Writing(); + yield this.Installing(); + yield this.Ending(); + }); + } + // ---------------------------------------------------------------------- + // 5 大阶段 + Initialize() { } + Prompting() { } + Writing() { } + Installing() { } + Ending() { } + // 5 大阶段 + // ---------------------------------------------------------------------- + easyCopy(file) { + // @ts-ignore + return file_1.file.copy(path_1.join(this.TPL, file), path_1.join(this.RUN, this.answers.appName, file)); + } + easyTpl(file) { + return __awaiter(this, void 0, void 0, function* () { + // @ts-ignore + const [compileContent] = yield Promise.all([ejs_1.default.renderFile(path_1.join(this.TPL, file), this.answers)]); + // @ts-ignore + return file_1.file.write(path_1.join(this.RUN, this.answers.appName, file), compileContent); + }); + } + // 提问题,并传回问题答案 + prompt(question) { + return __awaiter(this, void 0, void 0, function* () { + return this.answers = yield prompts_1.default(question); + }); + } +} +exports.Generator = Generator; diff --git a/extend/generator.ts b/extend/generator.ts index ad0d874..aea5814 100644 --- a/extend/generator.ts +++ b/extend/generator.ts @@ -2,6 +2,7 @@ import prompts from "prompts"; import { file as fs } from "./file"; import ejs from "ejs"; import { join, resolve, sep } from "path"; +import { warning } from "./console"; abstract class Generator { @@ -18,7 +19,9 @@ abstract class Generator { let path = this.PATH = this.dir.substring(0, dir.lastIndexOf("extend")); this.TPL = join(path, "template/"); - this.run().catch(err => console.log(err)); + this.run().catch(() => { + console.log(warning("🦠 项目创建失败,程序已自动退出")); + }); } /*