koa-program/bin/index.ts
2021-02-10 03:40:48 +08:00

128 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#! node
import program from "inquirer";
import { file as fs } from "../extend/file";
import * as os from "os";
import ejs from "ejs";
import chalk from "chalk";
import { exec } from "child_process";
// ------------------------------------------------
// cli结果
let answers: { appName: string; pkgManager: number; author: string };
// 脚本运行目录
const path: string = process.cwd() + "/";
// 模版所在目录
const tpl: string = `${path}/template/`;
// ========================================================
// 1、CLI 交互
program
.prompt([
{
name: "appName",
message: chalk.cyan("🌈 App Name:"),
type: "input",
validate(input) {
if (input.trim().length <= 0 || !input) return chalk.red("‼️ 必填项");
else return true;
},
},
{
name: "pkgManager",
message: chalk.yellow("🥓 Package Manager:"),
type: "list",
choices: [
{ name: "Yarn", value: 0, checked: true },
{ name: "Npm", value: 1 },
],
},
{
name: "author",
message: chalk.green("🥤 Your Name:"),
type: "input",
default: os.userInfo().username,
},
])
.then(async answer => {
answers = answer;
// --------------------------------
// 调用2
await writing();
});
// ========================================================
// 2、读写操作
async function writing() {
console.log(chalk.blue("\r\n ---------------- WRITING... ----------------"));
// Static 文件
copyStatic([
[`controller/index.ts`, `controller/index.ts`],
[`extend/logger.ts`, `extend/logger.ts`],
[`extend/lucky.ts`, `extend/lucky.ts`],
[`route/route.ts`, `route/route.ts`],
[`view/index.njk`, `view/index.njk`],
[`tsconfig.json`, `tsconfig.json`],
[`Dockerfile`, `Dockerfile`],
[`yarn.lock`, `yarn.lock`],
[`app.ts`, `app.ts`],
]);
// template that uses the EJS engine
await copyTemplate([
[`docker-compose.yml`, `docker-compose.yml`],
[`package.json`, `package.json`],
[`LICENSE`, `LICENSE`],
]);
// --------------------------------
// 调用3
installed();
}
// ------------------------------------------------
// Copy Static 文件
function copyStatic(files: string[][]) {
files.forEach(file => {
fs.copy(`${tpl}${file[0]}`, `${path}${answers.appName}/${file[1]}`);
console.log(
chalk.green("+") +
` ${chalk.grey(answers.appName + "/" + file[1])} ${chalk.green(
"SUCCESS.👌"
)}`
);
});
}
// Copy template that uses the EJS engine
// 使用EJS解析模版并将解析后的内容存入新文件中
function copyTemplate(files: string[][]) {
files.forEach(async file => {
let compileContent = await ejs.renderFile(`${tpl}${file[0]}`, answers);
fs.write(`${path}${answers.appName}/${file[1]}`, compileContent);
console.log(
chalk.green("+") +
` ${chalk.grey(answers.appName + "/" + file[1])} ${chalk.green(
"SUCCESS.👌"
)}`
);
});
}
// ========================================================
// 3、依赖安装
function installed() {
console.log(
chalk.blue("\r\n ---------------- Installing... ----------------")
);
if (answers.pkgManager == 0) {
exec(`cd ${answers.appName} && yarn install`, err => {
console.log(chalk.green("SUCCESS.👌"));
});
} else {
exec(`cd ${answers.appName} && npm install`, err => {
console.log(chalk.green("SUCCESS.👌"));
});
}
}