import { ICreateOptions } from '../intf/IOptions' import { prompt } from 'enquirer' import SimpleGit from 'simple-git' import { logger } from '../utils/logger' import { loadingRunner } from '../utils/thread' import del from 'del' import { existsSync } from 'fs' const tmeplates: Array<{ name: string; repo: string }> = [ { name: 'vue2', repo: 'http://gitlab.yslocal.com/fe-basic/templates/vue2' }, { name: 'npm', repo: 'http://gitlab.yslocal.com/fe-basic/templates/npm' }, { name: 'simple-html', repo: 'http://gitlab.yslocal.com/fe-basic/templates/simple-html' } ] const select = async (message: string = '请选择', choices: Array = []) => { try { const { name } = (await prompt({ type: 'select', name: 'name', message, choices })) as any return name } catch (error) { return null } } const input = async (message: string = '请输入') => { try { const { text } = (await prompt({ type: 'input', name: 'text', message })) as any return text } catch (error) { return null } } /** 创建项目解决方案 */ export const createPreset = async (options: ICreateOptions) => { // ? 选择 git 模板 const type: string = options.type ?? (await select( '请选择应用类型?', tmeplates.map((t) => t.name) )) if (!type) return // ? 输入本次存储目录 const project: string = options.project ?? (await input('请输入项目名:')) // ? 输入git地址 const cGit: string = (options.git ? '是' : null) ?? (await select('是否初始化git?', ['是', '否'])) let git: string = options.git if (!git && cGit === '是') { git = await input('请输入仓库地址:') } const localPath: string = `./${project}` // ? check params logger.info('检查输入参数:', !project || (cGit && !git) ? '未通过' : '通过') if (!project || (cGit === '是' && !git)) return if (existsSync(localPath)) throw new Error('目录已存在') const repo: string = tmeplates.find((t) => t.name === type).repo // > 拉取仓库代码 const sg = SimpleGit() await loadingRunner( async () => { await sg.clone(repo, localPath) }, { message: '拉取仓库代码', minTime: 500 } ) logger.info('删除模板 ./.git') await del(localPath + '/.git', {}) if (cGit === '是') { const sg1 = SimpleGit(localPath) try { await loadingRunner( async () => { await sg1.init() await sg1.addRemote('origin', git) await sg1.add('.') await sg1.commit('初始化项目') await sg1.push('origin') }, { message: '初始化', minTime: 500 } ) logger.info('git remote:', git) } catch (error) { logger.error(error) } } logger.warn('模板参数功能, 下版本实现.') }