转换为es6语法

This commit is contained in:
Titor 2021-02-25 18:10:50 +08:00
parent 9b957651b4
commit 52e806413d
5 changed files with 150 additions and 286 deletions

View File

@ -1,17 +1,4 @@
"use strict"; "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) { var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k; if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[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()); 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) { var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.Koa = void 0; exports.Koa = void 0;
var cli_1 = require("../extend/cli"); const cli_1 = require("../extend/cli");
var os = __importStar(require("os")); const os = __importStar(require("os"));
var console_1 = require("../extend/console"); const console_1 = require("../extend/console");
var child_process_1 = require("child_process"); const child_process_1 = require("child_process");
var ora_1 = __importDefault(require("ora")); const ora_1 = __importDefault(require("ora"));
var path_1 = require("path"); const path_1 = require("path");
// Koa 命令行 // Koa 命令行
var Koa = /** @class */ (function (_super) { class Koa extends cli_1.Cli {
__extends(Koa, _super); constructor() {
function Koa() { super();
var _this = _super.call(this) || this; this.command = ``;
_this.command = "";
return _this;
} }
// 初始化 // 初始化
Koa.prototype.Initialize = function () { Initialize() {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function* () {
return __generator(this, function (_a) { console.log(console_1.info(`\n\r---------------- 🎨 KOA CLI ----------------`));
console.log(console_1.info("\n\r---------------- \uD83C\uDFA8 KOA CLI ----------------"));
return [2 /*return*/];
});
}); });
}; }
// 交互阶段 // 交互阶段
Koa.prototype.Prompting = function () { Prompting() {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function* () {
return __generator(this, function (_a) { yield this.prompt([
switch (_a.label) { {
case 0: return [4 /*yield*/, this.prompt([ name: "appName",
{ message: console_1.readonly("🎉App Name:"),
name: "appName", type: "text",
message: console_1.readonly("🎉App Name:"), validate: (values) => values.length <= 0 ? `Input your project name, Please !` : true,
type: "text", },
validate: function (values) { return values.length <= 0 ? "Input your project name, Please !" : true; }, {
}, name: "pkgManager",
{ message: console_1.readonly("🎰Package Manager:"),
name: "pkgManager", type: "toggle",
message: console_1.readonly("🎰Package Manager:"), active: "Yarn",
type: "toggle", inactive: "Npm",
active: "Yarn", initial: "Yarn",
inactive: "Npm", },
initial: "Yarn", {
}, name: "author",
{ message: console_1.readonly("🥤Author Name:"),
name: "author", type: "text",
message: console_1.readonly("🥤Author Name:"), initial: os.userInfo().username,
type: "text", },
initial: os.userInfo().username, ]);
},
])];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
}; }
// 项目初始化 // 项目初始化
Koa.prototype.Writing = function () { Writing() {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function* () {
var files; console.log(console_1.info(`---------------- 🧩 WRITING ----------------`));
var _this = this; const files = {
return __generator(this, function (_a) { static: [
console.log(console_1.info("---------------- \uD83E\uDDE9 WRITING ----------------")); `controller/index.ts`,
files = { `extend/logger.ts`,
static: [ `extend/lucky.ts`,
"controller/index.ts", `route/route.ts`,
"extend/logger.ts", `view/index.njk`,
"extend/lucky.ts", `tsconfig.json`,
"route/route.ts", `Dockerfile`,
"view/index.njk", `app.ts`,
"tsconfig.json", ],
"Dockerfile", tpl: [
"app.ts", `docker-compose.yml`,
], `package.json`,
tpl: [ `LICENSE`,
"docker-compose.yml", ],
"package.json", };
"LICENSE", files.static.forEach(file => {
], let notice = this.easyCopy(file);
}; if (notice)
files.static.forEach(function (file) { console.log(console_1.success("+"), console_1.readonly(path_1.join(file)));
var notice = _this.easyCopy(file); else
if (notice) console.log(console_1.error("x"), console_1.error(path_1.join(file)));
console.log(console_1.success("+"), console_1.readonly(path_1.join(file))); });
else files.tpl.forEach(file => {
console.log(console_1.error("x"), console_1.error(path_1.join(file))); let notice = this.easyTpl(file);
}); if (notice)
files.tpl.forEach(function (file) { console.log(console_1.success("+"), console_1.readonly(path_1.join(file)));
var notice = _this.easyTpl(file); else
if (notice) console.log(console_1.error("x"), console_1.error(path_1.join(file)));
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*/];
}); });
}); });
}; }
// 依赖安装 // 依赖安装
Koa.prototype.Installing = function () { Installing() {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function* () {
return __generator(this, function (_a) { console.log(console_1.info(`---------------- 🎰 INSTALL ----------------`));
switch (_a.label) { if (this.answers.pkgManager) {
case 0: this.command += `cd ${this.answers.appName} && yarn install`;
console.log(console_1.info("---------------- \uD83C\uDFB0 INSTALL ----------------")); }
if (this.answers.pkgManager) { else {
this.command += "cd " + this.answers.appName + " && yarn install"; this.command += `cd ${this.answers.appName} && npm install`;
} }
else { yield this.easyInstall();
this.command += "cd " + this.answers.appName + " && npm install";
}
return [4 /*yield*/, this.easyInstall()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
}; }
// 安装 // 安装
Koa.prototype.easyInstall = function () { easyInstall() {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function* () {
var oraObj; let oraObj = ora_1.default();
return __generator(this, function (_a) { oraObj.start(console_1.readonly("Install..."));
switch (_a.label) { yield child_process_1.exec(this.command, (res, stdout, stderr) => {
case 0: if (res == null) {
oraObj = ora_1.default(); return oraObj.succeed(console_1.success("依赖安装完成...")).stop();
oraObj.start(console_1.readonly("Install...")); }
return [4 /*yield*/, child_process_1.exec(this.command, function (res, stdout, stderr) { else {
if (res == null) { return oraObj.fail(console_1.error("依赖安装失败,请手动安装")).stop();
return oraObj.succeed(console_1.success("依赖安装完成...")).stop();
}
else {
return oraObj.fail(console_1.error("依赖安装失败,请手动安装")).stop();
}
})];
case 1:
_a.sent();
return [2 /*return*/];
} }
}); });
}); });
}; }
return Koa; }
}(cli_1.Cli));
exports.Koa = Koa; exports.Koa = Koa;

View File

@ -8,127 +8,70 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next()); 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) { var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.Cli = void 0; exports.Cli = void 0;
var prompts_1 = __importDefault(require("prompts")); const prompts_1 = __importDefault(require("prompts"));
var file_1 = require("./file"); const file_1 = require("./file");
var ejs_1 = __importDefault(require("ejs")); const ejs_1 = __importDefault(require("ejs"));
var path_1 = require("path"); const path_1 = require("path");
var Cli = /** @class */ (function () { class Cli {
/** /**
* @constructor * @constructor
* @protected * @protected
*/ */
function Cli() { constructor() {
this.RUN = path_1.join(path_1.resolve(process.cwd()), path_1.sep); this.RUN = path_1.join(path_1.resolve(process.cwd()), path_1.sep);
this.answers = null; this.answers = null;
this.dir = path_1.join(path_1.resolve(__dirname)); this.dir = path_1.join(path_1.resolve(__dirname));
this.PATH = this.dir.substring(0, this.dir.lastIndexOf("extend")); this.PATH = this.dir.substring(0, this.dir.lastIndexOf("extend"));
this.TPL = path_1.join(this.PATH, "template/"); 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大阶段 * @description 自动调用 CLI 过程中的5大阶段
*/ */
Cli.prototype.run = function () { run() {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function* () {
return __generator(this, function (_a) { yield this.Initialize();
switch (_a.label) { yield this.Prompting();
case 0: return [4 /*yield*/, this.Initialize()]; yield this.Writing();
case 1: yield this.Installing();
_a.sent(); yield this.Ending();
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*/];
}
});
}); });
}; }
Cli.prototype.Initialize = function () { Initialize() {
}; }
Cli.prototype.Prompting = function () { Prompting() {
}; }
Cli.prototype.Writing = function () { Writing() {
}; }
Cli.prototype.Installing = function () { Installing() {
}; }
Cli.prototype.Ending = function () { Ending() {
}; }
// TODO: Copy Files // TODO: Copy Files
// PATH: 当前项目绝对路径 // PATH: 当前项目绝对路径
Cli.prototype.easyCopy = function (file) { easyCopy(file) {
// @ts-ignore // @ts-ignore
return file_1.file.copy(path_1.join(this.TPL, file), path_1.join(this.RUN, this.answers.appName, file)); 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) { easyTpl(file) {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function* () {
var compileContent; // @ts-ignore
return __generator(this, function (_a) { const [compileContent] = yield Promise.all([ejs_1.default.renderFile(path_1.join(this.TPL, file), this.answers)]);
switch (_a.label) { // @ts-ignore
case 0: return [4 /*yield*/, Promise.all([ejs_1.default.renderFile(path_1.join(this.TPL, file), this.answers)])]; return file_1.file.write(path_1.join(this.RUN, this.answers.appName, file), compileContent);
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)];
}
});
}); });
}; }
// 提问题,并传回问题答案 // 提问题,并传回问题答案
Cli.prototype.prompt = function (question) { prompt(question) {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function* () {
var _a; return this.answers = yield prompts_1.default(question);
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()];
}
});
}); });
}; }
return Cli; }
}());
exports.Cli = Cli; exports.Cli = Cli;

View File

@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.readonly = exports.success = exports.error = exports.warning = exports.info = exports.log = void 0; 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) { function log(information) {
return chalk_1.default.white(information); return chalk_1.default.white(information);
} }

View File

@ -1,15 +1,13 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.file = void 0; exports.file = void 0;
var fs_1 = require("fs"); const fs_1 = require("fs");
var path_1 = require("path"); const path_1 = require("path");
/** /**
* 文件操作类 * 文件操作类
* @description 更加语义化的简便文件操作 * @description 更加语义化的简便文件操作
*/ */
var file = /** @class */ (function () { class file {
function file() {
}
/** /**
* 创建目录 * 创建目录
* @description 如果目录不存在则自动创建目录并返回结果 * @description 如果目录不存在则自动创建目录并返回结果
@ -18,26 +16,26 @@ var file = /** @class */ (function () {
* @param {string} filePath 要操作复制剪切的目录名带文件名和后缀的完整目录 * @param {string} filePath 要操作复制剪切的目录名带文件名和后缀的完整目录
* @return {boolean} 操作状态 * @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) if (dir == "" || dir == null)
dir = "." + path_1.sep; // 不写目录名 => 根目录 dir = `.${path_1.sep}`; // 不写目录名 => 根目录
// 文件夹不存在 // 文件夹不存在
if (!fs_1.existsSync(dir)) { if (!fs_1.existsSync(dir)) {
var actionNotice = fs_1.mkdirSync(dir, { recursive: true }); let actionNotice = fs_1.mkdirSync(dir, { recursive: true });
return !!actionNotice; return !!actionNotice;
} }
else else
return true; return true;
}; }
/** /**
* 复制文件 * 复制文件
* @param {string} src 源文件地址 * @param {string} src 源文件地址
* @param {string} dest 文件去向地址 * @param {string} dest 文件去向地址
* @return {boolean} 操作状态 * @return {boolean} 操作状态
*/ */
file.copy = function (src, dest) { static copy(src, dest) {
if (!fs_1.existsSync(src)) { if (!fs_1.existsSync(src)) {
// console.log("源文件不存在"); // console.log("源文件不存在");
return false; return false;
@ -49,22 +47,20 @@ var file = /** @class */ (function () {
} }
else else
return false; return false;
}; }
/*** /***
* 文件写入保存文件 * 文件写入保存文件
* @param {string} filePath 文件去向地址 * @param {string} filePath 文件去向地址
* @param data 写入数据 * @param data 写入数据
* @return {boolean} 操作状态 * @return {boolean} 操作状态
*/ */
file.write = function (filePath, data) { static write(filePath, data = "") {
if (data === void 0) { data = ""; }
if (file.createPath(filePath)) { if (file.createPath(filePath)) {
fs_1.writeFileSync(filePath, data); fs_1.writeFileSync(filePath, data);
return fs_1.existsSync(filePath); return fs_1.existsSync(filePath);
} }
else else
return false; return false;
}; }
return file; }
}());
exports.file = file; exports.file = file;

View File

@ -5,7 +5,7 @@
/* Basic Options */ /* Basic Options */
// "incremental": true, /* Enable incremental compilation */ // "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'. */ "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. */ // "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */ // "allowJs": true, /* Allow javascript files to be compiled. */