ifweb
Version:
后台管理系统脚手架
122 lines (108 loc) • 3.22 kB
JavaScript
const { getRepoList, getTagList } = require('./http')
const ora = require('ora')
const inquirer = require('inquirer')
const util = require('util')
const path = require('path')
const downloadGit = require('download-git-repo')
const chalk = require('chalk')
/**
* 添加动画
* @param {*} fn
* @param {*} message
* @param {...any} args
* @returns
*/
const drawLoading = async (fn, message, ...args) => {
// 使用 ora 初始化,传入提示信息 message
const spinner = ora(message)
console.log('---spinner--', spinner)
// 开始加载动画
spinner.start()
try {
// 执行传入方法 fn
const result = await fn(...args)
console.log('###try--result---', result)
// 状态修改为成功
spinner.succeed()
return result
} catch (error) {
// 状态修为失败
spinner.fail('Request fail, refetch ....', error)
}
}
class Generator {
constructor(name, targetDir) {
console.log('----11Generator---', name, targetDir)
// 目录名称
this.name = name
// // 创建位置
this.targetDir = targetDir
// 对 download-git-repo 进行 promise 化改造
this.downloadGit = util.promisify(downloadGit)
}
// 获取用户选择的模板
// 1)从远程拉取模板数据
// 2)用户选择自己新下载的模板名称
// 3)return 用户选择的名称
async getRepo() {
const repoList = await drawLoading(getRepoList, 'wait fetch template')
if (!repoList) return
const repos = repoList.map((item) => item.name)
const { repo } = await inquirer.prompt({
name: 'repo',
type: 'list',
choices: repos,
message: 'Please choose a template to create project'
})
return repo
}
/**
* 获取用户选择的版本
* 基于 repo 结果,远程拉取对应的 tag 列表
* 用户选择自己需要下载的 tag
* return 用户选择的 tag
*/
async getTag(repo) {
// 基于 repo 结果,远程拉取对应的 tag 列表
const tags = await drawLoading(getTagList, 'waiting fetch tag', repo)
if (!tags) return
// 过滤需要的 tag 名称
const tagsList = tags.map((item) => item.name)
// 用户选择自己需要下载的 tag
const { tag } = await inquirer.prompt({
name: 'tag',
type: 'list',
choices: tagsList,
message: 'Place choose a tag to create project'
})
return tag
}
/**
* 下载远程模板
* 拼接下载地址
* 调用下载方法
*/
async download(repo, tag) {
// 下载地址
const requestUrl = 'dc0517/bweb'
// 调用下载方法
await drawLoading(
this.downloadGit,
'waiting download template',
requestUrl,
path.resolve(process.cwd(), this.targetDir)
)
}
/**
* 核心创建逻辑
*/
async create() {
// 下载模板到模板目录
await this.download()
// 模板使用提示
console.log(`\r\nSuccessfully created project ${chalk.cyan(this.name)}`)
console.log(`\r\n cd ${chalk.cyan(this.name)}`)
console.log(' npm run dev\r\n')
}
}
module.exports = Generator