-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
171 lines (138 loc) · 4.49 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
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
167
168
169
170
171
/* eslint-disable no-console */
import * as path from 'node:path'
import * as fs from 'fs-extra'
import prompts from 'prompts'
import { bold, cyan, green, red } from 'kolorist'
import Mustache from 'mustache'
import minimist from 'minimist'
import greet from './helpers/greet'
import generateTemplate from './helpers/generateTemplate'
import renderCommand from './helpers/renderCommand'
import type { PromptResult } from './types'
function canSkipEmpty(dir: string) {
if (!fs.existsSync(dir))
return true
const files = fs.readdirSync(dir)
if (files.length === 0)
return true
// if only .git folder, skip
if (files.length === 1 && files[0] === '.git')
return true
return false
}
(async () => {
const DEFAULT_PROJECT_NAME = 'nuxt3-app'
// valid options
// --default
// --pinia
// --vueuse / vu
const argv = minimist(process.argv.slice(2), {
alias: {
vueuse: ['vu'],
},
boolean: true,
})
const isArgvUsed = typeof (argv.default ?? argv.vueuse ?? argv.pinia) === 'boolean'
try {
let result: PromptResult = {}
console.log(`\n${greet}\n`)
const cwd = process.cwd()
let targetDir = cwd
try {
result = await prompts(
[
{
name: 'projectName',
type: 'text',
message: 'Project name: ',
initial: DEFAULT_PROJECT_NAME,
onState: state => (targetDir = String(state.value).trim()),
},
{
name: 'shouldOverwrite',
type: () => (canSkipEmpty(targetDir) ? null : 'confirm'),
message: () => {
const overwritePrompt
= targetDir === '.' ? 'Current directory' : `Target directory "${targetDir}"`
return `${overwritePrompt} is not empty. Remove existing files and continue?`
},
},
{
name: 'overwriteChecker',
type: (_, values) => {
if (values.shouldOverwrite === false)
throw new Error(`${red('✖')} Operation cancelled`)
return null
},
},
{
name: 'needPinia',
type: () => (isArgvUsed ? null : 'toggle'),
message: 'Add Pinia?',
initial: false,
active: 'Y',
inactive: 'N',
},
{
name: 'needVueuse',
type: () => (isArgvUsed ? null : 'toggle'),
message: 'Add Vueuse?',
initial: false,
active: 'Y',
inactive: 'N',
},
],
{
onCancel: () => {
throw new Error(`${red('✖')} Operation cancelled`)
},
},
)
}
catch (cancelled) {
console.log(cancelled.message)
process.exit(1)
}
const {
projectName, shouldOverwrite, needPinia = argv.pinia, needVueuse = argv.vueuse,
} = result
const projectDir = path.join(cwd, targetDir)
if (fs.existsSync(projectDir) && shouldOverwrite)
fs.emptyDirSync(projectDir)
else if (!fs.existsSync(projectDir))
fs.mkdirSync(projectDir)
console.log(`\nScaffolding project in ${projectDir}...`)
const pkg = { name: projectName, version: '0.0.0' }
fs.writeFileSync(path.resolve(projectDir, 'package.json'), JSON.stringify(pkg, null, 2))
const templateRoot = path.resolve(__dirname, 'template')
const generate = (templateName: string) => {
const templateDir = path.resolve(templateRoot, templateName)
generateTemplate(templateDir, projectDir)
}
generate('base')
const modules = []
if (needPinia) {
generate('modules/pinia')
modules.push('@pinia/nuxt')
}
if (needVueuse) {
generate('modules/vueuse')
modules.push('@vueuse/nuxt')
}
if (modules.length > 0) {
const nuxtConfig = fs.readFileSync(path.resolve(projectDir, 'nuxt.config.ts'))
const output = Mustache.render(nuxtConfig.toString(), { modules })
fs.writeFileSync(path.resolve(projectDir, 'nuxt.config.ts'), output)
}
const userAgent = process.env.npm_config_user_agent ?? ''
const pkgManager = /pnpm/.test(userAgent) ? 'pnpm' : /yarn/.test(userAgent) ? 'yarn' : 'npm'
console.log(green('\nDone. Now run:\n'))
if (projectDir !== cwd)
console.log(` ${bold(cyan(`cd ${path.relative(cwd, projectDir)}`))}`)
console.log(` ${bold(cyan(renderCommand(pkgManager, 'install')))}`)
console.log(` ${bold(cyan(renderCommand(pkgManager, 'dev')))}`)
}
catch (error) {
console.log(error)
}
})()