-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (148 loc) · 3.92 KB
/
index.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
/* eslint-disable no-console */
// @ts-check
import path from 'path'
import fs from 'fs'
import prompts from 'prompts'
import { Command } from 'commander/esm.mjs'
import degit from 'degit'
const defaultProjectName = 'ou-app'
const templates = [
{
id: 'vue',
name: 'vue template (vue3 + ts)',
url: 'ouduidui/vue-template',
},
{
id: 'react',
name: 'react template (react18 + ts)',
url: 'ouduidui/react-template',
},
{
id: 'ts',
name: 'typescript template',
url: 'ouduidui/typescript-template',
},
{
id: 'nuxt',
name: 'nuxt template (nuxt3 + ts)',
url: 'ouduidui/nuxt-template',
},
{
id: 'uniapp',
name: 'uniapp template (uniapp v3 + ts)',
url: 'ouduidui/uniapp-template',
},
]
const canSafelyOverwrite = dir => !fs.existsSync(dir) || fs.readdirSync(dir).length === 0
const getCommandOptions = () => {
return new Promise((resolve, reject) => {
try {
new Command()
.argument('[project-name]', 'project name', defaultProjectName)
.option('-t, --template <template>', `choose a template (${templates.map(t => t.id).join('|')})`)
.option('-r, --root', 'create in the current directory', false)
.option('-f, --force', 'for force overwriting', false)
.action((name, opts) => {
resolve({ name, opts })
}).parse()
}
catch (err) {
reject(err)
console.log(err.message)
}
})
}
const emptyDir = (dir) => {
if (!fs.existsSync(dir)) return
postOrderDirectoryTraverse(
dir,
dir => fs.rmdirSync(dir),
file => fs.unlinkSync(file),
)
}
function postOrderDirectoryTraverse(dir, dirCallback, fileCallback) {
for (const filename of fs.readdirSync(dir)) {
const fullPath = path.resolve(dir, filename)
if (fs.lstatSync(fullPath).isDirectory()) {
postOrderDirectoryTraverse(fullPath, dirCallback, fileCallback)
dirCallback(fullPath)
continue
}
fileCallback(fullPath)
}
}
const init = async() => {
const cwd = process.cwd()
const { name, opts } = await getCommandOptions()
if (!name || !opts)
process.exit(1)
let template = opts.template && templates.find(t => t.id === opts.template) ? opts.template : ''
const root = opts.root
let force = opts.force
// error template
if (template === '') {
try {
const res = await prompts({
name: 'template',
type: 'select',
choices: templates.map(t => ({ title: t.name, value: t.id })),
message: 'Choose a template:',
})
template = res.template
}
catch (err) {
console.log(err.message)
process.exit(1)
}
finally {
if (!template)
process.exit(1)
}
}
// cannot safe overwrite
if (!force && !canSafelyOverwrite(root ? '.' : name)) {
try {
const { shouldOverwrite } = await prompts({
name: 'shouldOverwrite',
type: 'toggle',
initial: true,
active: 'Yes',
inactive: 'No',
message: () => {
const dirForPrompt = root ? 'Current directory' : `Target directory "${name}"`
return `${dirForPrompt} is not empty. Remove existing files and continue?`
},
})
if (shouldOverwrite)
force = true
else
process.exit(1)
}
catch (err) {
console.log(err.message)
process.exit(1)
}
}
const dir = root ? cwd : path.join(cwd, name)
if (!root) {
if (fs.existsSync(dir) && force) { emptyDir(dir) }
else if (fs.existsSync(dir) && !force) {
console.log(`Directory "${dir}" already exists.`)
process.exit(1)
}
else {
fs.mkdirSync(dir)
}
}
console.log(`\nScaffolding project in ${dir}...`)
const temp = templates.find(t => t.id === template)
const emitter = degit(temp.url, {
force: true,
})
emitter.on('warn', err => console.log(err))
emitter.clone(dir).then(() => console.log('done'))
}
init().catch((e) => {
console.error(e)
})