@chenmq/cli
Version:
仿@vue/cli脚手架工具
44 lines (39 loc) • 1.2 kB
JavaScript
const { wrapLoading } = require('../utils')
const { getRepo } = require('./request')
const inquirer = require('inquirer')
const downloadGitRepo = require('download-git-repo')
const util = require('util')
const path = require('path')
class Creator {
constructor(appName) {
this.appName = appName;
this.downloadGitRepo = util.promisify(downloadGitRepo)
}
//请求仓库
async fetchRepo () {
let repos = await wrapLoading(getRepo, '拼命加载中...')
if (!repos) return;
repos = repos.map(v => v.name)
const { repository } = await inquirer.prompt([
{
type: 'list',
name: 'repository',
message: 'please choose a template to create project',
choices: repos,
}
])
this.download(repository)
return repository;
}
//下载仓库
async download (repoName) {
const url = `zhu-cli/${repoName}`
const targetDir = path.resolve(process.cwd(), this.appName)
await wrapLoading(this.downloadGitRepo, '下载模板中...', url, targetDir)
}
async create () {
let repo = await this.fetchRepo()
await this.download(repo)
}
}
module.exports = Creator