Skip to content

Commit

Permalink
feat: parse argument
Browse files Browse the repository at this point in the history
  • Loading branch information
FliPPeDround committed Nov 1, 2023
1 parent 73fe7f8 commit 39280ad
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 34 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@antfu/ni": "^0.21.5",
"@antfu/utils": "^0.7.5",
"@types/ejs": "^3.1.2",
"@types/minimist": "^1.2.4",
"@types/node": "^18.17.3",
"@types/prompts": "^2.4.4",
"@vue/create-eslint-config": "^0.3.1",
Expand All @@ -51,6 +52,7 @@
"eslint": "^8.46.0",
"kolorist": "^1.8.0",
"lint-staged": "^13.2.3",
"minimist": "^1.2.8",
"pnpm": "^8.6.12",
"prompts": "^2.4.2",
"simple-git-hooks": "^2.9.0",
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 38 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import {
writeFileSync,
} from 'node:fs'
import ejs from 'ejs'
import minimist from 'minimist'
import prompts from 'prompts'
import { bold, red } from 'kolorist'
import figures from 'prompts/lib/util/figures.js'
import {
canSkipEmptying,
dowloadTemplate,
preOrderDirectoryTraverse,
printBanner,
Expand All @@ -23,10 +28,21 @@ import {
import { question } from './question'
import type { BaseTemplateList } from './question/template/type'
import { postOrderDirectoryTraverse } from './utils/directoryTraverse'
import filePrompt from './question/file'
import { templateList } from './question/template/templateDate'

async function init() {
printBanner()

const argv = minimist(process.argv.slice(2), {
alias: {
templateType: ['t'],
},
string: ['_'],
})
const projectName = argv._[0]
const templateType = templateList.find(item => item.value.type === argv?.t)?.value

let result: {
projectName?: string
shouldOverwrite?: boolean
Expand All @@ -41,13 +57,29 @@ async function init() {
needsUnocss?: boolean
} = {}

try {
result = await question()
}
catch (cancelled) {
if (!projectName) {
try {
result = await question()
}
catch (cancelled) {
// eslint-disable-next-line no-console
console.log((<{ message: string }>cancelled).message)
process.exit(1)
console.log((<{ message: string }>cancelled).message)
process.exit(1)
}
}
else {
if (!templateType) {
// eslint-disable-next-line no-console
console.log(`${red(figures.cross)} ${bold('未获取到指定模板')}`)
process.exit(1)
}
result = {
projectName,
shouldOverwrite: canSkipEmptying(projectName)
? true
: (await prompts(filePrompt(projectName))).shouldOverwrite,
templateType,
}
}

const cwd = process.cwd()
Expand Down
31 changes: 31 additions & 0 deletions src/question/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { PromptObject } from 'prompts'
import { bold, red } from 'kolorist'
import figures from 'prompts/lib/util/figures.js'
import { canSkipEmptying } from '../utils'

export default (targetDir: string): PromptObject<string>[] => {
return [
{
name: 'shouldOverwrite',
type: () => (canSkipEmptying(targetDir) ? null : 'toggle'),
message: () => {
const dirForPrompt
= targetDir === '.' ? '当前文件' : `目标文件"${targetDir}"`

return `${dirForPrompt}不是空的。要删除现有文件并继续吗?`
},
initial: false,
active: 'Yes',
inactive: 'No',
},
{
name: 'overwriteChecker',
type: (_prev, values) => {
if (values.shouldOverwrite === false)
throw new Error(`${red(figures.cross)} ${bold('操作已取消')}`)

return null
},
},
]
}
27 changes: 2 additions & 25 deletions src/question/name.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { PromptObject } from 'prompts'
import { bold, red } from 'kolorist'
import figures from 'prompts/lib/util/figures.js'
import { canSkipEmptying } from '../utils'
import filePrompt from './file'

export default (): PromptObject<string>[] => {
let targetDir = 'uni-app'
Expand All @@ -13,27 +11,6 @@ export default (): PromptObject<string>[] => {
initial: targetDir,
onState: state => (targetDir = String(state.value).trim() || targetDir),
},
{
name: 'shouldOverwrite',
type: () => (canSkipEmptying(targetDir) ? null : 'toggle'),
message: () => {
const dirForPrompt
= targetDir === '.' ? '当前文件' : `目标文件"${targetDir}"`

return `${dirForPrompt}不是空的。要删除现有文件并继续吗?`
},
initial: false,
active: 'Yes',
inactive: 'No',
},
{
name: 'overwriteChecker',
type: (_prev, values) => {
if (values.shouldOverwrite === false)
throw new Error(`${red(figures.cross)} ${bold('操作已取消')}`)

return null
},
},
...filePrompt(targetDir),
]
}
4 changes: 1 addition & 3 deletions src/question/template/templateDate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { readFileSync, rmSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { bold, dim, green, link } from 'kolorist'
import { dim, green, link } from 'kolorist'
import type { TemplateList } from './type'

export const templateList: TemplateList[] = [
Expand Down

0 comments on commit 39280ad

Please sign in to comment.