-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
134 lines (112 loc) · 2.93 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env node
import path from 'node:path'
import process from 'node:process'
import { parseArgs } from 'node:util'
import { intro, select, text } from '@clack/prompts'
import color from 'picocolors'
import {
banner,
checkDuplicateDir,
choices,
download,
fixDirectoryPath,
info,
stinger,
useCache,
useCancel,
} from './utils'
async function init() {
console.log()
intro(banner(info))
await stinger()
const args = process.argv.slice(2)
const { positionals } = parseArgs({
args,
strict: false,
})
const projectName = positionals[0]
// Project name
const name = await text({
message: 'Please input your project name:',
placeholder: 'my-project',
initialValue: projectName,
validate: (value) => {
const existDirectoryName: string = path.resolve(process.cwd(), path.join('.', value))
if (!value) {
return 'Input your project name first!'
}
if (checkDuplicateDir(value)) {
return `Target directory ${color.underline(color.cyan(`${fixDirectoryPath(existDirectoryName)}`))} already exists. Pick another name!`
}
},
}) as string
useCancel(name)
// Select template or remote
const operate = await select({
message: 'Select operation:',
options: choices['operate'],
}) as string
useCancel(operate)
// Do different action
operate === 'template' ? defaultAction(name, operate) : remoteRepo(name, operate)
}
/**
* remote repo
*/
async function remoteRepo(projectName: string, clackType: string) {
// Repo link
const repoLink = await text({
message: 'Input the repo link you want:',
placeholder: 'leedom92/vue-h5-template',
validate: (value) => {
if (!value) {
return 'Please input the repo link!'
}
},
}) as string
useCancel(repoLink)
// Use cache?
const cache = await useCache()
useCancel(cache)
const directory: string = path.resolve(process.cwd(), path.join(projectName || '.', ''))
// download
await download({
cache,
url: repoLink,
projectName,
clackType,
message: `Please refer to ${color.underline(color.cyan(`${fixDirectoryPath(directory)}/README.md`))} to start the project.`,
})
}
/**
* default template
*/
async function defaultAction(projectName: string, clackType: string) {
// Select template type
const type = await select({
message: 'Select template type:',
options: choices['type'],
}) as string
useCancel(type)
// Select template
const url = await select({
message: 'Select template:',
options: choices[type],
}) as string
useCancel(url)
// Use cache?
const cache = await useCache()
useCancel(cache)
const target: string = path.join(projectName || '.', '')
// download
await download({
cache,
url,
projectName,
clackType,
message: `cd ${target}\npnpm install\npnpm dev`,
}).catch(() => process.exit(1))
}
init().catch(console.error)
export default init
export * from './utils/types'