-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·99 lines (94 loc) · 2.93 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
#!/usr/bin/env node
import path from 'path'
import prompts from 'prompts'
import fs from 'fs-extra'
import chalk from 'chalk'
import {validatePkgName, install} from './utils/index.js'
import { fileURLToPath} from 'node:url'
await init()
async function init () {
const options = await getOptions()
let pkgName = process.argv[2] === undefined ? options.name : process.argv[2]
validatePkgName(pkgName)
const dir = path.resolve(pkgName.startsWith('@') ? pkgName.split('/')[1] : pkgName)
await validateDir(pkgName)
await copyFiles(options, dir, pkgName)
install(options.packageManager, dir)
console.log(`${chalk.green("✔")} Success! Created ${chalk.cyan(pkgName)} at ${chalk.cyan(dir)}`)
console.log('\nDone. Now run: \n')
console.log(` cd ${pkgName}`)
console.log(` ${options.packageManager} run dev\n`)
}
async function getOptions () {
let questions = [
{
message: '项目描述',
name: 'description',
type: 'text',
initial: '这是一个项目模版',
},
{
message: '是否需要使用ElementPlus?',
name: 'useElementPlus',
type: 'confirm',
initial: true,
},
{
name: "packageManager",
type: "select",
choices: ["yarn", "pnpm", "npm"].map((i) => ({ title: i, value: i })),
message: "请选择要使用的包管理工具",
},
]
if (process.argv[2] === undefined) {
questions.unshift({
message: '项目名称',
name: 'name',
type: 'text',
initial: 'vue3-tailwind',
})
}
return await prompts(questions)
}
async function validateDir (dir) {
if (fs.pathExistsSync(dir)) {
console.log(chalk.bold(`目录 ${dir} 已存在`))
const { yes } = await prompts({
name: 'yes',
type: 'confirm',
message: chalk.bold('您是否需要覆盖已存在的目录?'),
initial: true,
})
if (!yes) process.exit(1)
await fs.remove(dir)
}
fs.mkdirpSync(dir, {})
}
async function copyFiles (options, dir, pkgName) {
const {name, description, useElementPlus} = options
const templateDir = useElementPlus ? path.resolve(
fileURLToPath(import.meta.url),
'../',
`template-vue3-tailwind-element`,
) : path.resolve(
fileURLToPath(import.meta.url),
'../',
`template-vue3-tailwind`,
)
fs.copySync(templateDir, dir)
await fs.writeFileSync(path.resolve(dir, 'README.md'), `# ${name === undefined ? pkgName : name}\n\n${description}\n`)
// 修改package.json
const pkgPath = path.resolve(dir, 'package.json')
const json = JSON.parse(fs.readFileSync(pkgPath))
json.name = name === undefined ? pkgName : name
json.description = description
fs.writeFileSync(pkgPath, JSON.stringify(json, null, 2) + '\n')
// 重命名_开头的文件
const renameFiles = {
_gitignore: '.gitignore',
_env: '.env'
}
Object.keys(renameFiles).forEach(key => {
fs.moveSync(path.resolve(dir, key), path.resolve(dir, renameFiles[key]), {overwrite: true})
})
}