2023-12-22 17:38:13 +08:00
|
|
|
|
const chalk = require('chalk'); // 终端输出颜色变化
|
|
|
|
|
const inquirer = require('inquirer'); // 交互选项
|
|
|
|
|
const ora = require('ora'); // loading效果
|
|
|
|
|
const gitClone = require('git-clone/promise'); // 拉取项目
|
|
|
|
|
const fs = require('fs-extra'); // 操作文件
|
|
|
|
|
const path = require('path'); // 引入操作路径
|
|
|
|
|
const { question, projectList } = require('./config');
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
/**
|
|
|
|
|
* 判断文件是否存在
|
|
|
|
|
* 是否需要覆盖
|
|
|
|
|
*/
|
|
|
|
|
async fileIsExist(targetPath, name) {
|
|
|
|
|
if (fs.existsSync(targetPath, name)) {
|
|
|
|
|
const awsaner = await inquirer.prompt({
|
|
|
|
|
type: 'confirm',
|
|
|
|
|
message: '是否覆盖文件夹❓',
|
|
|
|
|
default: false,
|
|
|
|
|
name: 'overWrite',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 用户选择是则删除文件夹
|
|
|
|
|
if (awsaner.overWrite) {
|
|
|
|
|
fs.remove(targetPath);
|
|
|
|
|
console.log(chalk.green('✅删除成功'));
|
2023-12-22 21:46:45 +08:00
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
console.log(chalk.yellow('❌程序退出'));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-12-22 17:38:13 +08:00
|
|
|
|
}
|
2023-12-22 22:48:42 +08:00
|
|
|
|
return true;
|
2023-12-22 17:38:13 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建文项目
|
|
|
|
|
*/
|
|
|
|
|
async createProject() {
|
|
|
|
|
// 选择框架和语言
|
|
|
|
|
const frame = await inquirer.prompt(question.frame);
|
|
|
|
|
const frame_isTypeScript = `${frame.type}&${frame.isTypeScript}`;
|
|
|
|
|
|
|
|
|
|
// 如果选择的react框架
|
|
|
|
|
if (frame.type === 'react') {
|
|
|
|
|
const awsaner = await inquirer.prompt(question.react);
|
|
|
|
|
const key = `${frame_isTypeScript}&${awsaner.router}`;
|
|
|
|
|
return projectList[key];
|
|
|
|
|
}
|
|
|
|
|
// 如果选择是vue框架
|
|
|
|
|
else {
|
|
|
|
|
const awsaner = await inquirer.prompt(question.vue);
|
|
|
|
|
const key = `${frame_isTypeScript}&${awsaner.router}`;
|
|
|
|
|
return projectList[key];
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* 从仓库clone项目
|
|
|
|
|
*/
|
2023-12-22 21:46:45 +08:00
|
|
|
|
async downloadProject(targetPath, key, name) {
|
|
|
|
|
const spinner = ora('下载中...\n').start();
|
2023-12-22 17:38:13 +08:00
|
|
|
|
|
2023-12-22 21:46:45 +08:00
|
|
|
|
try {
|
|
|
|
|
await gitClone(key, name, { checkout: 'master' });
|
|
|
|
|
spinner.succeed('😄下载成功');
|
|
|
|
|
// 如果有git,删除原有的git
|
|
|
|
|
if (fs.existsSync(targetPath, '.git')) {
|
|
|
|
|
fs.remove(path.join(targetPath, '.git'));
|
|
|
|
|
}
|
|
|
|
|
const overText = `
|
|
|
|
|
⚓ Running completion hooks...
|
2023-12-22 17:38:13 +08:00
|
|
|
|
|
2023-12-22 21:46:45 +08:00
|
|
|
|
📄 Generating README.md...
|
2023-12-22 17:38:13 +08:00
|
|
|
|
|
2023-12-22 21:46:45 +08:00
|
|
|
|
🎉 Successfully created project ${name}
|
|
|
|
|
👉 Get started with the following commands:
|
|
|
|
|
|
|
|
|
|
cd vue_js_vuex
|
|
|
|
|
npm run serve
|
|
|
|
|
`;
|
|
|
|
|
console.log(overText);
|
|
|
|
|
console.log(chalk.whiteBright('Happy hacking!'));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
spinner.fail('😭下载失败,请稍后重试');
|
|
|
|
|
}
|
2023-12-22 17:38:13 +08:00
|
|
|
|
},
|
|
|
|
|
};
|