From 52e806413d441fe3961cc8d1b603920955bdbef7 Mon Sep 17 00:00:00 2001 From: Titor Date: Thu, 25 Feb 2021 18:10:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E4=B8=BAes6=E8=AF=AD?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/koa.js | 267 +++++++++++++++++----------------------------- extend/cli.js | 135 +++++++---------------- extend/console.js | 2 +- extend/file.js | 30 +++--- tsconfig.json | 2 +- 5 files changed, 150 insertions(+), 286 deletions(-) diff --git a/bin/koa.js b/bin/koa.js index eebe54c..d2041ea 100644 --- a/bin/koa.js +++ b/bin/koa.js @@ -1,17 +1,4 @@ "use strict"; -var __extends = (this && this.__extends) || (function () { - var extendStatics = function (d, b) { - extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; - return extendStatics(d, b); - }; - return function (d, b) { - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -})(); var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); @@ -40,182 +27,120 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -var __generator = (this && this.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (_) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Koa = void 0; -var cli_1 = require("../extend/cli"); -var os = __importStar(require("os")); -var console_1 = require("../extend/console"); -var child_process_1 = require("child_process"); -var ora_1 = __importDefault(require("ora")); -var path_1 = require("path"); +const cli_1 = require("../extend/cli"); +const os = __importStar(require("os")); +const console_1 = require("../extend/console"); +const child_process_1 = require("child_process"); +const ora_1 = __importDefault(require("ora")); +const path_1 = require("path"); // Koa 命令行 -var Koa = /** @class */ (function (_super) { - __extends(Koa, _super); - function Koa() { - var _this = _super.call(this) || this; - _this.command = ""; - return _this; +class Koa extends cli_1.Cli { + constructor() { + super(); + this.command = ``; } // 初始化 - Koa.prototype.Initialize = function () { - return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - console.log(console_1.info("\n\r---------------- \uD83C\uDFA8 KOA CLI ----------------")); - return [2 /*return*/]; - }); + Initialize() { + return __awaiter(this, void 0, void 0, function* () { + console.log(console_1.info(`\n\r---------------- 🎨 KOA CLI ----------------`)); }); - }; + } // 交互阶段 - Koa.prototype.Prompting = function () { - return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - switch (_a.label) { - case 0: return [4 /*yield*/, this.prompt([ - { - name: "appName", - message: console_1.readonly("🎉App Name:"), - type: "text", - validate: function (values) { return values.length <= 0 ? "Input your project name, Please !" : true; }, - }, - { - name: "pkgManager", - message: console_1.readonly("🎰Package Manager:"), - type: "toggle", - active: "Yarn", - inactive: "Npm", - initial: "Yarn", - }, - { - name: "author", - message: console_1.readonly("🥤Author Name:"), - type: "text", - initial: os.userInfo().username, - }, - ])]; - case 1: - _a.sent(); - return [2 /*return*/]; - } - }); + Prompting() { + return __awaiter(this, void 0, void 0, function* () { + yield this.prompt([ + { + name: "appName", + message: console_1.readonly("🎉App Name:"), + type: "text", + validate: (values) => values.length <= 0 ? `Input your project name, Please !` : true, + }, + { + name: "pkgManager", + message: console_1.readonly("🎰Package Manager:"), + type: "toggle", + active: "Yarn", + inactive: "Npm", + initial: "Yarn", + }, + { + name: "author", + message: console_1.readonly("🥤Author Name:"), + type: "text", + initial: os.userInfo().username, + }, + ]); }); - }; + } // 项目初始化 - Koa.prototype.Writing = function () { - return __awaiter(this, void 0, void 0, function () { - var files; - var _this = this; - return __generator(this, function (_a) { - console.log(console_1.info("---------------- \uD83E\uDDE9 WRITING ----------------")); - files = { - static: [ - "controller/index.ts", - "extend/logger.ts", - "extend/lucky.ts", - "route/route.ts", - "view/index.njk", - "tsconfig.json", - "Dockerfile", - "app.ts", - ], - tpl: [ - "docker-compose.yml", - "package.json", - "LICENSE", - ], - }; - files.static.forEach(function (file) { - var notice = _this.easyCopy(file); - if (notice) - console.log(console_1.success("+"), console_1.readonly(path_1.join(file))); - else - console.log(console_1.error("x"), console_1.error(path_1.join(file))); - }); - files.tpl.forEach(function (file) { - var notice = _this.easyTpl(file); - if (notice) - console.log(console_1.success("+"), console_1.readonly(path_1.join(file))); - else - console.log(console_1.error("x"), console_1.error(path_1.join(file))); - }); - return [2 /*return*/]; + Writing() { + return __awaiter(this, void 0, void 0, function* () { + console.log(console_1.info(`---------------- 🧩 WRITING ----------------`)); + const files = { + static: [ + `controller/index.ts`, + `extend/logger.ts`, + `extend/lucky.ts`, + `route/route.ts`, + `view/index.njk`, + `tsconfig.json`, + `Dockerfile`, + `app.ts`, + ], + tpl: [ + `docker-compose.yml`, + `package.json`, + `LICENSE`, + ], + }; + files.static.forEach(file => { + let notice = this.easyCopy(file); + if (notice) + console.log(console_1.success("+"), console_1.readonly(path_1.join(file))); + else + console.log(console_1.error("x"), console_1.error(path_1.join(file))); + }); + files.tpl.forEach(file => { + let notice = this.easyTpl(file); + if (notice) + console.log(console_1.success("+"), console_1.readonly(path_1.join(file))); + else + console.log(console_1.error("x"), console_1.error(path_1.join(file))); }); }); - }; + } // 依赖安装 - Koa.prototype.Installing = function () { - return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - console.log(console_1.info("---------------- \uD83C\uDFB0 INSTALL ----------------")); - if (this.answers.pkgManager) { - this.command += "cd " + this.answers.appName + " && yarn install"; - } - else { - this.command += "cd " + this.answers.appName + " && npm install"; - } - return [4 /*yield*/, this.easyInstall()]; - case 1: - _a.sent(); - return [2 /*return*/]; - } - }); + Installing() { + return __awaiter(this, void 0, void 0, function* () { + console.log(console_1.info(`---------------- 🎰 INSTALL ----------------`)); + if (this.answers.pkgManager) { + this.command += `cd ${this.answers.appName} && yarn install`; + } + else { + this.command += `cd ${this.answers.appName} && npm install`; + } + yield this.easyInstall(); }); - }; + } // 安装 - Koa.prototype.easyInstall = function () { - return __awaiter(this, void 0, void 0, function () { - var oraObj; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - oraObj = ora_1.default(); - oraObj.start(console_1.readonly("Install...")); - return [4 /*yield*/, child_process_1.exec(this.command, function (res, stdout, stderr) { - if (res == null) { - return oraObj.succeed(console_1.success("依赖安装完成...")).stop(); - } - else { - return oraObj.fail(console_1.error("依赖安装失败,请手动安装")).stop(); - } - })]; - case 1: - _a.sent(); - return [2 /*return*/]; + easyInstall() { + return __awaiter(this, void 0, void 0, function* () { + let oraObj = ora_1.default(); + oraObj.start(console_1.readonly("Install...")); + yield child_process_1.exec(this.command, (res, stdout, stderr) => { + if (res == null) { + return oraObj.succeed(console_1.success("依赖安装完成...")).stop(); + } + else { + return oraObj.fail(console_1.error("依赖安装失败,请手动安装")).stop(); } }); }); - }; - return Koa; -}(cli_1.Cli)); + } +} exports.Koa = Koa; diff --git a/extend/cli.js b/extend/cli.js index 70b4877..cab24c2 100644 --- a/extend/cli.js +++ b/extend/cli.js @@ -8,127 +8,70 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -var __generator = (this && this.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (_) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Cli = void 0; -var prompts_1 = __importDefault(require("prompts")); -var file_1 = require("./file"); -var ejs_1 = __importDefault(require("ejs")); -var path_1 = require("path"); -var Cli = /** @class */ (function () { +const prompts_1 = __importDefault(require("prompts")); +const file_1 = require("./file"); +const ejs_1 = __importDefault(require("ejs")); +const path_1 = require("path"); +class Cli { /** * @constructor * @protected */ - function Cli() { + constructor() { this.RUN = path_1.join(path_1.resolve(process.cwd()), path_1.sep); this.answers = null; this.dir = path_1.join(path_1.resolve(__dirname)); this.PATH = this.dir.substring(0, this.dir.lastIndexOf("extend")); this.TPL = path_1.join(this.PATH, "template/"); - this.run().then().catch(function (err) { return console.log(err); }); + this.run().then().catch(err => console.log(err)); } /** * 自动指令中枢: * @description 自动调用 CLI 过程中的5大阶段 */ - Cli.prototype.run = function () { - return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - switch (_a.label) { - case 0: return [4 /*yield*/, this.Initialize()]; - case 1: - _a.sent(); - return [4 /*yield*/, this.Prompting()]; - case 2: - _a.sent(); - return [4 /*yield*/, this.Writing()]; - case 3: - _a.sent(); - return [4 /*yield*/, this.Installing()]; - case 4: - _a.sent(); - return [4 /*yield*/, this.Ending()]; - case 5: - _a.sent(); - return [2 /*return*/]; - } - }); + run() { + return __awaiter(this, void 0, void 0, function* () { + yield this.Initialize(); + yield this.Prompting(); + yield this.Writing(); + yield this.Installing(); + yield this.Ending(); }); - }; - Cli.prototype.Initialize = function () { - }; - Cli.prototype.Prompting = function () { - }; - Cli.prototype.Writing = function () { - }; - Cli.prototype.Installing = function () { - }; - Cli.prototype.Ending = function () { - }; + } + Initialize() { + } + Prompting() { + } + Writing() { + } + Installing() { + } + Ending() { + } // TODO: Copy Files // PATH: 当前项目绝对路径 - Cli.prototype.easyCopy = function (file) { + easyCopy(file) { // @ts-ignore return file_1.file.copy(path_1.join(this.TPL, file), path_1.join(this.RUN, this.answers.appName, file)); - }; - Cli.prototype.easyTpl = function (file) { - return __awaiter(this, void 0, void 0, function () { - var compileContent; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: return [4 /*yield*/, Promise.all([ejs_1.default.renderFile(path_1.join(this.TPL, file), this.answers)])]; - case 1: - compileContent = (_a.sent())[0]; - // @ts-ignore - return [2 /*return*/, file_1.file.write(path_1.join(this.RUN, this.answers.appName, file), compileContent)]; - } - }); + } + 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); }); - }; + } // 提问题,并传回问题答案 - Cli.prototype.prompt = function (question) { - return __awaiter(this, void 0, void 0, function () { - var _a; - return __generator(this, function (_b) { - switch (_b.label) { - case 0: - _a = this; - return [4 /*yield*/, prompts_1.default(question)]; - case 1: return [2 /*return*/, _a.answers = _b.sent()]; - } - }); + prompt(question) { + return __awaiter(this, void 0, void 0, function* () { + return this.answers = yield prompts_1.default(question); }); - }; - return Cli; -}()); + } +} exports.Cli = Cli; diff --git a/extend/console.js b/extend/console.js index 45ff2d9..d1e927d 100644 --- a/extend/console.js +++ b/extend/console.js @@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readonly = exports.success = exports.error = exports.warning = exports.info = exports.log = void 0; -var chalk_1 = __importDefault(require("chalk")); +const chalk_1 = __importDefault(require("chalk")); function log(information) { return chalk_1.default.white(information); } diff --git a/extend/file.js b/extend/file.js index 344b9e1..fb15041 100644 --- a/extend/file.js +++ b/extend/file.js @@ -1,15 +1,13 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.file = void 0; -var fs_1 = require("fs"); -var path_1 = require("path"); +const fs_1 = require("fs"); +const path_1 = require("path"); /** * 文件操作类 * @description 更加语义化的简便文件操作。 */ -var file = /** @class */ (function () { - function file() { - } +class file { /** * 创建目录 * @description 如果目录不存在,则自动创建目录,并返回结果 @@ -18,26 +16,26 @@ var file = /** @class */ (function () { * @param {string} filePath 要操作(复制,剪切)的目录名(带文件名和后缀的完整目录) * @return {boolean} 操作状态 */ - file.createPath = function (filePath) { + static createPath(filePath) { // 获取完整目录(去除最后一位的文件字段) - var dir = filePath.substring(0, filePath.lastIndexOf(path_1.sep)); + let dir = filePath.substring(0, filePath.lastIndexOf(path_1.sep)); if (dir == "" || dir == null) - dir = "." + path_1.sep; // 不写目录名 => 根目录 + dir = `.${path_1.sep}`; // 不写目录名 => 根目录 // 文件夹不存在 if (!fs_1.existsSync(dir)) { - var actionNotice = fs_1.mkdirSync(dir, { recursive: true }); + let actionNotice = fs_1.mkdirSync(dir, { recursive: true }); return !!actionNotice; } else return true; - }; + } /** * 复制文件 * @param {string} src 源文件地址 * @param {string} dest 文件去向地址 * @return {boolean} 操作状态 */ - file.copy = function (src, dest) { + static copy(src, dest) { if (!fs_1.existsSync(src)) { // console.log("源文件不存在"); return false; @@ -49,22 +47,20 @@ var file = /** @class */ (function () { } else return false; - }; + } /*** * 文件写入(保存文件) * @param {string} filePath 文件去向地址 * @param data 写入数据 * @return {boolean} 操作状态 */ - file.write = function (filePath, data) { - if (data === void 0) { data = ""; } + static write(filePath, data = "") { if (file.createPath(filePath)) { fs_1.writeFileSync(filePath, data); return fs_1.existsSync(filePath); } else return false; - }; - return file; -}()); + } +} exports.file = file; diff --git a/tsconfig.json b/tsconfig.json index fbf91bc..a191351 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - "target": "ES5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ + "target": "ES6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */