Skip to content

Commit

Permalink
fix: 🐛 优化模板处理过程
Browse files Browse the repository at this point in the history
  • Loading branch information
MuyianKing committed Jan 17, 2025
1 parent 4785b5d commit e267a66
Show file tree
Hide file tree
Showing 14 changed files with 1,668 additions and 319 deletions.
76 changes: 26 additions & 50 deletions core/bin/command/inquirer.h5.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,38 @@
import path from 'node:path'
import process from 'node:process'
import inquirer from 'inquirer'
import ora from 'ora'
import { checkMkdirExists } from '../utils/copy.js'
import download from '../utils/download.js'
import { handler } from './inquirer.utils.js'

function inquirerPrompt(argv) {
const { name } = argv
return new Promise((resolve, reject) => {
inquirer.prompt([
{
type: 'input',
name: 'name',
message: '项目名称',
default: name,
validate(val) {
if (!val) {
return '请输入项目名称:'
}
return true
},
return inquirer.prompt([
{
type: 'input',
name: 'name',
message: '项目名称',
default: name,
validate(val) {
if (!val) {
return '请输入项目名称:'
}
return true
},
{
type: 'list',
name: 'build_type',
message: '构建工具',
choices: ['rsbuild', 'vite'],
filter(value) {
return {
rsbuild: 'rsbuild',
vite: 'vite',
}[value]
},
},
{
type: 'list',
name: 'build_type',
message: '构建工具',
choices: ['rsbuild', 'vite'],
filter(value) {
return {
rsbuild: 'rsbuild',
vite: 'vite',
}[value]
},
]).then((answers) => {
resolve(answers)
}).catch((error) => {
reject(error)
})
})
},
])
}

export default function (argv) {
inquirerPrompt(argv).then(async (answers) => {
const { name, build_type } = answers
const isMkdirExists = checkMkdirExists(
path.resolve(process.cwd(), `./${name}`),
)

const spinner = ora().start()
if (isMkdirExists) {
spinner.warning(`${name}文件夹已经存在`)
} else {
const _path = ['h5', build_type]

// 下载文件
await download(name, _path)
spinner.succeed(`created successfully`)
}
handler(answers, 'h5')
})
}
76 changes: 25 additions & 51 deletions core/bin/command/inquirer.html.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,37 @@
import path from 'node:path'
import process from 'node:process'
import inquirer from 'inquirer'
import ora from 'ora'
import { checkMkdirExists } from '../utils/copy.js'
import download from '../utils/download.js'
import { handler } from './inquirer.utils.js'

function inquirerPrompt(argv) {
const { name } = argv
return new Promise((resolve, reject) => {
inquirer.prompt([
{
type: 'input',
name: 'name',
message: '项目名称',
default: name,
validate(val) {
if (!val) {
return '请输入项目名称:'
}
return true
},
return inquirer.prompt([
{
type: 'input',
name: 'name',
message: '项目名称',
default: name,
validate(val) {
if (!val) {
return '请输入项目名称:'
}
return true
},
{
type: 'list',
name: 'build_type',
message: '框架',
choices: ['vue'],
filter(value) {
return {
vue: 'vue',
}[value]
},
},
{
type: 'list',
name: 'build_type',
message: '框架',
choices: ['vue'],
filter(value) {
return {
vue: 'vue',
}[value]
},
]).then((answers) => {
resolve(answers)
}).catch((error) => {
reject(error)
})
})
},
])
}

export default function (argv) {
inquirerPrompt(argv).then(async (answers) => {
const { name, build_type } = answers
const isMkdirExists = checkMkdirExists(
path.resolve(process.cwd(), `./${name}`),
)

const spinner = ora().start()

if (isMkdirExists) {
spinner.warning(`${name}文件夹已经存在`)
} else {
const _path = ['html', build_type]

// 下载文件
await download(name, _path)

spinner.succeed(`created successfully`)
}
handler(answers, 'html')
})
}
8 changes: 2 additions & 6 deletions core/bin/command/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ export default function () {

const spinner = ora().start()

if (process.argv.length === 2) {
spinner.fail(`缺少执行命令:mu <command>`)
} else if (process.argv.length === 3 && !cmd.includes(process.argv[2])) {
spinner.fail(`找不到命令:mu <command>`)
} else {
if ((process.argv.length === 3 && cmd.includes(process.argv[2])) || process.argv[2]?.includes('help')) {
spinner.stop()
return
}

spinner.succeed(`Usage:
mu web 创建一个web项目
mu h5 创建一个h5项目
mu lib 创建一个Lib项目
Expand Down
76 changes: 25 additions & 51 deletions core/bin/command/inquirer.lib.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,37 @@
import path from 'node:path'
import process from 'node:process'
import inquirer from 'inquirer'
import ora from 'ora'
import { checkMkdirExists } from '../utils/copy.js'
import download from '../utils/download.js'
import { handler } from './inquirer.utils.js'

function inquirerPrompt(argv) {
const { name } = argv
return new Promise((resolve, reject) => {
inquirer.prompt([
{
type: 'input',
name: 'name',
message: '项目名称',
default: name,
validate(val) {
if (!val) {
return '请输入项目名称:'
}
return true
},
return inquirer.prompt([
{
type: 'input',
name: 'name',
message: '项目名称',
default: name,
validate(val) {
if (!val) {
return '请输入项目名称:'
}
return true
},
{
type: 'list',
name: 'build_type',
message: '组件类型',
choices: ['Vue组件'],
filter(value) {
return {
Vue组件: 'vue-comp',
}[value]
},
},
{
type: 'list',
name: 'build_type',
message: '组件类型',
choices: ['Vue组件'],
filter(value) {
return {
Vue组件: 'vue-comp',
}[value]
},
]).then((answers) => {
resolve(answers)
}).catch((error) => {
reject(error)
})
})
},
])
}

export default function (argv) {
inquirerPrompt(argv).then(async (answers) => {
const { name, build_type } = answers
const isMkdirExists = checkMkdirExists(
path.resolve(process.cwd(), `./${name}`),
)

const spinner = ora().start()

if (isMkdirExists) {
spinner.warning(`${name}文件夹已经存在`)
} else {
const _path = ['lib', build_type]

// 下载文件
await download(name, _path)

spinner.succeed(`created successfully`)
}
handler(answers, 'lib')
})
}
23 changes: 23 additions & 0 deletions core/bin/command/inquirer.utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { existsSync } from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import ora from 'ora'
import download from '../utils/download.js'

export async function handler(answers, type) {
const { name, build_type } = answers
const isMkdirExists = existsSync(path.resolve(process.cwd(), `./${name}`))

const spinner = ora().start()

if (isMkdirExists) {
spinner.warning(`${name}文件夹已经存在`)
} else {
const _path = [type, build_type]

// 下载文件
await download(name, _path)

spinner.succeed(`created successfully`)
}
}
Loading

0 comments on commit e267a66

Please sign in to comment.