- 降低最低node版本为12
- 增加入口文件 node 启动指令
- 修改ts编译为es5语法
- 修改版本号为2.0.1
This commit is contained in:
Titor-Z 2021-02-17 02:06:16 +08:00
parent d85bcf350a
commit 78536d9d23
9 changed files with 296 additions and 156 deletions

View File

@ -49,7 +49,9 @@ docker-compose up -d
## 版本简述
### 2.0.0 **`latest`**
### 2.0.1 **`latest`**
- 修复脚本缺少 node 启动行的问题
### 2.0.0
- 修复1.0版本中模板生成阶段缺失文件的问题
- 重构了结构代码,做了进一步的系统结构调整
### 1.1.4

View File

@ -1,4 +1,5 @@
#! node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const koa_1 = require("./koa");
var koa_1 = require("./koa");
new koa_1.Koa();

View File

@ -1,3 +1,4 @@
#! node
import { Koa } from "./koa";
new Koa()

View File

@ -1,4 +1,17 @@
"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]; } });
@ -27,38 +40,72 @@ 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;
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");
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");
// Koa 命令行
class Koa extends cli_1.Cli {
constructor() {
super();
this.command = ``;
var Koa = /** @class */ (function (_super) {
__extends(Koa, _super);
function Koa() {
var _this = _super.call(this) || this;
_this.command = "";
return _this;
}
// 初始化
Initialize() {
return __awaiter(this, void 0, void 0, function* () {
console.log(console_1.info(`\n\r---------------- 🎨 KOA CLI ----------------`));
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*/];
});
}
});
};
// 交互阶段
Prompting() {
return __awaiter(this, void 0, void 0, function* () {
yield this.prompt([
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: (values) => values.length <= 0 ? `Input your project name, Please !` : true,
validate: function (values) { return values.length <= 0 ? "Input your project name, Please !" : true; },
},
{
name: "pkgManager",
@ -74,73 +121,101 @@ class Koa extends cli_1.Cli {
type: "text",
initial: os.userInfo().username,
},
]);
});
])];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
// 项目初始化
Writing() {
return __awaiter(this, void 0, void 0, function* () {
console.log(console_1.info(`---------------- 🧩 WRITING ----------------`));
const files = {
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`,
"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`,
"docker-compose.yml",
"package.json",
"LICENSE",
],
};
files.static.forEach(file => {
let notice = this.easyCopy(file);
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(file => {
let notice = this.easyTpl(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*/];
});
}
});
};
// 依赖安装
Installing() {
return __awaiter(this, void 0, void 0, function* () {
console.log(console_1.info(`---------------- 🎰 INSTALL ----------------`));
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`;
this.command += "cd " + this.answers.appName + " && yarn install";
}
else {
this.command += `cd ${this.answers.appName} && npm install`;
this.command += "cd " + this.answers.appName + " && npm install";
}
return [4 /*yield*/, this.easyInstall()];
case 1:
_a.sent();
return [2 /*return*/];
}
yield this.easyInstall();
});
}
});
};
// 安装
easyInstall() {
return __awaiter(this, void 0, void 0, function* () {
let oraObj = ora_1.default();
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..."));
yield child_process_1.exec(this.command, (res, stdout, stderr) => {
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*/];
}
});
});
}
}
};
return Koa;
}(cli_1.Cli));
exports.Koa = Koa;

View File

@ -8,70 +8,127 @@ 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;
const prompts_1 = __importDefault(require("prompts"));
const file_1 = require("./file");
const ejs_1 = __importDefault(require("ejs"));
const path_1 = require("path");
class Cli {
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 () {
/**
* @constructor
* @protected
*/
constructor() {
function Cli() {
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(err => console.log(err));
this.run().then().catch(function (err) { return console.log(err); });
}
/**
* 自动指令中枢
* @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();
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*/];
}
});
}
Initialize() {
}
Prompting() {
}
Writing() {
}
Installing() {
}
Ending() {
}
});
};
Cli.prototype.Initialize = function () {
};
Cli.prototype.Prompting = function () {
};
Cli.prototype.Writing = function () {
};
Cli.prototype.Installing = function () {
};
Cli.prototype.Ending = function () {
};
// TODO: Copy Files
// PATH: 当前项目绝对路径
easyCopy(file) {
Cli.prototype.easyCopy = function (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);
});
}
});
};
// 提问题,并传回问题答案
prompt(question) {
return __awaiter(this, void 0, void 0, function* () {
return this.answers = yield prompts_1.default(question);
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()];
}
});
}
}
});
};
return Cli;
}());
exports.Cli = Cli;

View File

@ -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;
const chalk_1 = __importDefault(require("chalk"));
var chalk_1 = __importDefault(require("chalk"));
function log(information) {
return chalk_1.default.white(information);
}

View File

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

View File

@ -1,11 +1,11 @@
{
"name": "koa-program",
"version": "2.0.0",
"version": "2.0.1",
"bin": {
"koa": "bin/index.js"
},
"engines": {
"node": ">= 15"
"node": ">= 12"
},
"dependencies": {
"chalk": "^4.1.0",

View File

@ -5,7 +5,7 @@
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"target": "ES5", /* 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. */