bunny-cli/bin/utils.js

87 lines
2.3 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.

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('✅删除成功'));
return true;
} else {
console.log(chalk.yellow('❌程序退出'));
return false;
}
}
},
/**
* 创建文项目
*/
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项目
*/
async downloadProject(targetPath, key, name) {
const spinner = ora('下载中...\n').start();
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...
📄 Generating README.md...
🎉 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('😭下载失败,请稍后重试');
}
},
};