forked from ChelesteWang/create-omi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (135 loc) · 4.31 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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const argv = require('minimist')(process.argv.slice(2))
const { prompt } = require('enquirer')
const shell = require('shelljs');
const { log, error, success, info } = require('./log')
const { TEMPLATES } = require('./templates')
const { emptyDir, copy, copyDir } = require('./utils')
const renameFiles = {
_gitignore: '.gitignore'
}
const cwd = process.cwd()
let targetDir = argv._[0]
const defaultProjectName = !targetDir ? 'omi-project' : targetDir
async function init() {
if (!targetDir) {
const { name } = await prompt({
type: "input",
name: "name",
message: "Project name:",
initial: defaultProjectName
})
targetDir = name
} else {
success(`your project will created in ${path.join(cwd, targetDir)}`)
}
const root = path.join(cwd, targetDir)
if (!fs.existsSync(root)) {
fs.mkdirSync(root, { recursive: true })
}
const existing = fs.readdirSync(root)
if (existing.length) {
const { yes } = await prompt({
type: 'confirm',
name: 'yes',
initial: 'Y',
message:
`Target directory ${targetDir} is not empty.\n` +
`Remove existing files and continue?`
})
if (yes) {
emptyDir(root)
log(root)
} else {
return
}
}
chooseTemplate(root)
.then(async (res) => {
const packageManager = await choosePackageManager()
copyTemplate(res)
return packageManager
})
.then((packageManager) => {
installDependencies(root, packageManager).then(() => {
})
})
.catch((err) => { error(err) })
}
async function chooseTemplate(root) {
let template = argv.t || argv.template
if (!template || !TEMPLATES.includes(template)) {
if (!TEMPLATES.includes(template)) {
info("The selected template was not found")
}
const { t } = await prompt({
type: "select",
name: "t",
message: "Select a template:",
choices: TEMPLATES
})
template = t
} else {
log(`Template will choose ${template}`)
}
const templateDir = path.join(__dirname, `template/${template}`)
success(`your project will created ${template} in ${path.join(cwd, targetDir)}`)
return { templateDir, root }
}
async function choosePackageManager() {
const { packageManager } = await prompt({
type: 'select',
name: 'packageManager',
initial: 'npm',
choices: ['npm', 'yarn', 'pnpm'],
message:
`Select a package manager`
})
return packageManager
}
async function copyTemplate({ templateDir, root }) {
const files = fs.readdirSync(templateDir)
const write = (file, content) => {
const targetPath = renameFiles[file]
? path.join(root, renameFiles[file])
: path.join(root, file)
if (content) {
fs.writeFileSync(targetPath, content)
} else {
copy(path.join(templateDir, file), targetPath)
}
}
for (const file of files.filter((f) => f !== 'package.json')) {
write(file)
}
const pkg = require(path.join(templateDir, `package.json`))
pkg.name = path.basename(root)
write('package.json', JSON.stringify(pkg, null, 2))
}
async function installDependencies(root, packageManager) {
prompt({
type: 'confirm',
name: 'isInstall',
initial: 'Y',
message:
`Whether to install dependencies now\n`
}).then((isInstall) => {
if (isInstall) {
shell.cd(`${path.relative(cwd, root)}`);
shell.exec(`${packageManager} install`)
} else {
log(`\nDone. Now run:\n`)
if (root !== cwd) {
log(`cd ${path.relative(cwd, root)}`)
}
log(`npm install (or \`yarn\`)`)
}
log(`\nnpm run dev (or \`yarn start\`)`)
success(`\nThe project has been created in ${root}\n`)
})
}
init().catch((err) => {
error(err)
})