mk-paas-cli
Version:
MK PAAS CLI TOOL
87 lines (81 loc) • 2.3 kB
JavaScript
const { prompt } = require("inquirer");
const fs = require("fs-extra");
const path = require("path");
const chalk = require("chalk");
const download = require("download-git-repo");
const ora = require("ora");
const tplList = require(`${__dirname}/../templates`);
const question = [
{
type: "input",
name: "tplName",
message: "模版名称:",
validate(val) {
if (tplList[val]) {
return true;
} else if (val === "") {
return "模版名称是必填的!";
} else if (!tplList[val]) {
return "该模板名称不存在,您可以用使用 list 命令查看所有模板!";
}
},
},
{
type: "input",
name: "name",
message: "项目名字:",
validate(val) {
if (val !== "") {
return true;
}
return "项目名字是必填的!";
},
},
{
type: "input",
name: "place",
message: "项目初始化路径:",
default: "./",
},
];
module.exports = prompt(question).then(({ tplName, name, place }) => {
const startAt = new Date().getTime();
const cwd = process.cwd();
const targetDir = path.join(cwd, place || "./", name);
const gitPlace = tplList[tplName]["owner/name"];
const gitBranch = tplList[tplName]["branch"];
const spinner = ora("模板下载中,请稍等...");
/**
* 考虑用户可以手动配置模板加入github下载模式
*/
const baseUrl =
tplList[tplName]["type"] === "gitee" ? "gitee.com:" : "github:github.com:";
if (fs.existsSync(targetDir)) {
console.log(
chalk.red(`😵 目录 ${place || "./"}${name} 已存在,将退出初始化!`)
);
process.exit(1);
}
spinner.start();
download(
`${baseUrl}${gitPlace}`,
`${place}/${name}`,
{ clone: true },
(err) => {
if (err) {
console.log(chalk.red(`😵 初始化失败!将退出初始化错误信息:${err}`));
process.exit();
}
spinner.stop();
console.log(
chalk.green(`🎉 ${name} 初始化成功,请运行以下命令开始安装依赖并启动应用
cd ${name}
npm install | yarn
npm start | yarn start
`)
);
const elapsed = new Date().getTime() - startAt;
console.log(`✨ 共耗时 ${(elapsed / 1000).toFixed(2)} 秒`);
}
);
});