aichat-antdesignx-app-cli
Version:
借助antd design x及开源包aichat-core封装的web端AI聊天助手App(TS版本)
91 lines (81 loc) • 2.44 kB
JavaScript
const { promisify } = require("util");
const figlet = promisify(require("figlet"));
const clear = require("clear");
const chalk = require("chalk");
// 粉笔着色
const log = (content) => console.log(chalk.green(content));
const { gitClone } = require("./gitclone");
const open = require("open");
const path = require("path");
const { existsSync } = require("fs");
const spawn = async (...args) => {
// spawn()用于使用提供的命令集启动新进程 语法:child_process.spawn(command[,args][,options])
const { spawn } = require("child_process");
return new Promise((resolve) => {
const proc = spawn(...args);
if (proc.stdout) {
proc.stdout.pipe(process.stdout);
}
if (proc.stderr) {
proc.stderr.pipe(process.stderr);
}
proc.on("close", () => {
resolve();
});
});
};
/**
* @param {*} name 项目名称
* @returns
*/
module.exports = async (name) => {
// 先清屏
clear();
// 打印欢迎界面
const banner = await figlet("AIChat-AntDesignX-App-Cli Welcome !");
log(banner);
// 打印项目名(也即是当前文件夹)
log(`创建项目:${name}`);
// clone代码前先判断下当前cli命令执行的目录下是否有该项目,如果有,报错提示
const absPath = path.resolve("./", name);
const exists = existsSync(absPath, (e) => { });
if (exists) {
console.log(
chalk.red(
`destination path ${absPath} already exists and may is not an empty directory.`
)
);
return;
}
// 克隆项目
let dStatus = await gitClone("aichat-core-antdesignx-template", name,);
if ("ok" !== dStatus) {
console.log(chalk.red(dStatus));
return;
}
// 自动安装依赖
log("安装依赖...");
await spawn("yarn", [], {
stdio: "inherit",
cwd: `./${name}`,
// 仅在当前运行环境为 Windows 时,才使用 shell
shell: process.platform === "win32",
});
log(`
安装完成:
To get start:
=========================
cd ${name}
npm run start
=========================
`);
// 打开浏览器,等着项目启动完
open(`http://localhost:3066`);
// 启动
await spawn("npm", ["run", "start"], {
stdio: "inherit",
cwd: `./${name}`,
// 仅在当前运行环境为 Windows 时,才使用 shell
shell: process.platform === "win32",
});
};