|
@@ -1,7 +1,18 @@
|
|
|
import { Command, Flags } from '@oclif/core'
|
|
|
-import { TEMPLATES, TemplateNames } from '../../template'
|
|
|
+import {TEMPLATES, TemplateNames, ComponentPrompt, PackagePrompt, PACKAGE } from '../../template'
|
|
|
import DownloadRemoteProjectTemplate, { FormatParams } from '@jianyu/dl-template'
|
|
|
-const path = require('path')
|
|
|
+import render from '../../utils/render'
|
|
|
+import * as inquirer from 'inquirer'
|
|
|
+import * as path from 'node:path'
|
|
|
+import * as fs from 'node:fs'
|
|
|
+
|
|
|
+function isDirEmpty(dir: string) {
|
|
|
+ return fs.promises.readdir(dir).then(files => {
|
|
|
+ return files.length === 0
|
|
|
+ }).catch(() => {
|
|
|
+ return true
|
|
|
+ })
|
|
|
+}
|
|
|
|
|
|
export default class Init extends Command {
|
|
|
static description = '初始化项目'
|
|
@@ -20,30 +31,53 @@ export default class Init extends Command {
|
|
|
|
|
|
async run(): Promise<void> {
|
|
|
const { args, flags } = await this.parse(Init)
|
|
|
-
|
|
|
const canNext = TemplateNames.includes(flags.from)
|
|
|
|
|
|
if (!canNext) {
|
|
|
- this.error(`没有找到对应的模版配置, 可用的模版为 [${TemplateNames.join(',')}]`)
|
|
|
+ this.error(`🚨 [template error] 没有找到对应的模版配置, 可用的模版为 [${TemplateNames.join(',')}]`)
|
|
|
}
|
|
|
|
|
|
+ const config = await inquirer.prompt([
|
|
|
+ ...ComponentPrompt,
|
|
|
+ ...PackagePrompt
|
|
|
+ ], {
|
|
|
+ name: args.dir,
|
|
|
+ template: flags.from
|
|
|
+ })
|
|
|
+
|
|
|
const { repo, assets, title } = TEMPLATES[flags.from]
|
|
|
const destinationFolder = path.resolve(process.cwd(), args.dir)
|
|
|
|
|
|
this.log(`🗃 项目初始化目录路径: ${args.dir}`)
|
|
|
|
|
|
+ const isEmpty = await isDirEmpty(destinationFolder)
|
|
|
+ if (!isEmpty) {
|
|
|
+ const canCover = await inquirer.prompt( {
|
|
|
+ type: 'input',
|
|
|
+ name: 'confirm',
|
|
|
+ message: '目录不为空,是否覆盖 (no / yes)',
|
|
|
+ default: 'no'
|
|
|
+ })
|
|
|
+ if (canCover.confirm === 'yes') {
|
|
|
+ this.log('💀 清空目录中')
|
|
|
+ fs.rmSync(destinationFolder, { recursive: true, force: true })
|
|
|
+ } else {
|
|
|
+ return this.log('🤚 任务停止')
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
const dl = new DownloadRemoteProjectTemplate({
|
|
|
getTransformLinks: (params: FormatParams) => {
|
|
|
let url = ''
|
|
|
switch (params.type) {
|
|
|
- case 'gitlab-jh': {
|
|
|
- url = `http://192.168.3.207:20080/${params.group}/${params.name}/-/archive/${params.checkout}/${params.name}-${params.checkout}.zip`
|
|
|
- if (params.path) {
|
|
|
- url += `?path=${params.path}`
|
|
|
- }
|
|
|
+ case 'gitlab-jh': {
|
|
|
+ url = `http://192.168.3.207:20080/${params.group}/${params.name}/-/archive/${params.checkout}/${params.name}-${params.checkout}.zip`
|
|
|
+ if (params.path) {
|
|
|
+ url += `?path=${params.path}`
|
|
|
+ }
|
|
|
|
|
|
- break
|
|
|
- }
|
|
|
+ break
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return url
|
|
@@ -55,10 +89,26 @@ export default class Init extends Command {
|
|
|
remoteFolder: assets,
|
|
|
destinationFolder,
|
|
|
}).then(() => {
|
|
|
- this.log(`🎉 初始化 ${flags.from} ${title}项目模版创建成功`)
|
|
|
+ this.log(`🪜 模板拉取成功`)
|
|
|
+ this.log(`🎨 准备中`)
|
|
|
+ })
|
|
|
+ await render(destinationFolder, {
|
|
|
+ package: {
|
|
|
+ name: config.packageName,
|
|
|
+ describe: config.packageDescribe,
|
|
|
+ lib: config.packageLib,
|
|
|
+ author: config.packageAuthor
|
|
|
+ }
|
|
|
+ }).then(() => {
|
|
|
+ this.log(`🎉 初始化 ${flags.from} ${title} 成功`)
|
|
|
this.log('👉 使用下面这些命令开始项目:')
|
|
|
- this.log(`$ cd ${args.dir}`)
|
|
|
- this.log('$ yarn')
|
|
|
+ this.log(` $ \x1B[36m cd ${args.dir} \x1B[0m`)
|
|
|
+ this.log(' $ \x1B[36m yarn \x1B[0m')
|
|
|
+ }).catch((err: Error) => {
|
|
|
+ this.warn(`🚨 [template error] 请提供错误截图及描述至 issues: ${PACKAGE.bugs} `)
|
|
|
+ this.log('💀 撤销任务操作 清空目录')
|
|
|
+ fs.rmSync(destinationFolder, { recursive: true, force: true })
|
|
|
+ this.error(err)
|
|
|
})
|
|
|
}
|
|
|
}
|