forked from vikejs/vike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·222 lines (199 loc) · 5.17 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env node
// @ts-check
const fs = require('fs')
const path = require('path')
const argv = require('minimist')(process.argv.slice(2))
const { prompt } = require('enquirer')
const { green, cyan, bold } = require('picocolors')
const { execSync } = require('child_process')
const BOILERPLATES = [
{
name: 'vue',
color: green
},
{
name: 'vue-ts',
color: green
},
{
name: 'react',
color: cyan
},
{
name: 'react-ts',
color: cyan
}
]
const IGNORE_FILES = ['.prettierrc', '.test-dev.test.ts', '.test-prod.test.ts', '.testCiJob.json']
//const IGNORE_PACKAGE_JSON = ['name', 'version', '// Needed for Yarn workspaces']
const IGNORE_PACKAGE_JSON = []
const RENAME_FILES = {
_gitignore: '.gitignore'
}
const cwd = process.cwd()
async function init() {
let targetDir = argv._[0]
if (!targetDir) {
/**
* @type {{ name: string }}
*/
const { name } = await prompt({
type: 'input',
name: 'name',
message: `Project name:`,
initial: 'vite-ssr-project'
})
targetDir = name
}
const root = path.join(cwd, targetDir)
console.log(`\nScaffolding project in ${root}...`)
if (!fs.existsSync(root)) {
fs.mkdirSync(root, { recursive: true })
} else {
const existing = fs.readdirSync(root)
if (existing.length) {
/**
* @type {{ yes: boolean }}
*/
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)
} else {
return
}
}
}
// determine boilerplate
let boilerplate = argv.t || argv.boilerplate
let message = 'Select a boilerplate:'
let isValidBoilerplate = false
// --boilerplate expects a value
if (typeof boilerplate === 'string') {
const availableBoilerplates = BOILERPLATES.map((b) => b.name)
isValidBoilerplate = availableBoilerplates.includes(boilerplate)
message = `${boilerplate} isn't a valid boilerplate. Please choose from below:`
}
if (!boilerplate || !isValidBoilerplate) {
/**
* @type {{ t: string }}
*/
const { t } = await prompt({
type: 'select',
name: 't',
message,
choices: BOILERPLATES.map((b) => ({
message: b.color(b.name),
name: b.name
}))
})
boilerplate = t
}
const boilerplateDir = path.join(__dirname, `boilerplate-${boilerplate}`)
const write = (file) => {
const targetPath = RENAME_FILES[file] ? path.join(root, RENAME_FILES[file]) : path.join(root, file)
if (file === 'package.json') {
let content = JSON.parse(fs.readFileSync(path.join(boilerplateDir, file)).toString())
IGNORE_PACKAGE_JSON.forEach((key) => {
delete content[key]
})
content = JSON.stringify(content, null, 2)
fs.writeFileSync(targetPath, content + '\n')
} else {
copy(path.join(boilerplateDir, file), targetPath)
}
}
const files = fs.readdirSync(boilerplateDir)
for (const file of files.filter((f) => !IGNORE_FILES.includes(f))) {
write(file)
}
if (!argv['skip-git']) {
initGitRepo(root)
}
console.log(`\nDone. Now run:\n`)
if (root !== cwd) {
console.log(cmd(` cd ${path.relative(cwd, root)}/`))
}
console.log(` ${cmd('npm install')} (or ${cmd('pnpm install')} / ${cmd('yarn install')})`)
console.log(` ${cmd('npm run dev')} (or ${cmd('pnpm run dev')} / ${cmd('yarn dev')})`)
console.log()
}
function cmd(str) {
return bold(str)
}
function copy(src, dest) {
const stat = fs.statSync(src)
if (stat.isDirectory()) {
copyDir(src, dest)
} else {
fs.copyFileSync(src, dest)
}
}
function copyDir(srcDir, destDir) {
fs.mkdirSync(destDir, { recursive: true })
for (const file of fs.readdirSync(srcDir)) {
const srcFile = path.resolve(srcDir, file)
const destFile = path.resolve(destDir, file)
copy(srcFile, destFile)
}
}
function emptyDir(dir) {
if (!fs.existsSync(dir)) {
return
}
for (const file of fs.readdirSync(dir)) {
const abs = path.resolve(dir, file)
// baseline is Node 12 so can't use rmSync :(
if (fs.lstatSync(abs).isDirectory()) {
emptyDir(abs)
fs.rmdirSync(abs)
} else {
fs.unlinkSync(abs)
}
}
}
function initGitRepo(cwd) {
if (!isGitInstalled()) {
return
}
try {
execSync('git init', {
cwd,
// See https://github.com/brillout/vite-plugin-ssr/issues/478
stdio: 'ignore'
})
execSync('git add .', { cwd, stdio: 'ignore' })
execSync(
[
'git',
'-c user.name="Romuald Brillout"',
'-c user.email="vite-plugin-ssr@brillout.com"',
'commit',
'--no-gpg-sign',
'--message="boilerplate Vite w/ vite-plugin-ssr"'
].join(' '),
{ cwd, stdio: 'ignore' }
)
} catch {
try {
fs.rmSync(path.join(cwd, '.git'), { recursive: true, force: true })
} catch {}
console.warn('Could not initialize a git repository.')
}
}
function isGitInstalled() {
let stdout
try {
stdout = execSync('git --version')
} catch (err) {
return false
}
return !!stdout
}
init().catch((e) => {
console.error(e)
})