-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·223 lines (196 loc) · 5.47 KB
/
index.js
File metadata and controls
executable file
·223 lines (196 loc) · 5.47 KB
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
223
#!/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 {
green,
blue,
yellow,
gray,
stripColors
} = require('kolorist')
const cwd = process.cwd()
const TEMPLATES = [
['🦀', yellow('wasm-react')],
['🦀', yellow('wasm-vue')],
['⬢ ', green('react-dva-ts')],
['🦕', blue('deno-oak')],
['🦕', blue('deno-vscode-cmd')],
]
const renameFiles = {
_gitignore: '.gitignore'
}
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-project'
})
targetDir = name
}
const pkgName = await getValidPackageName(targetDir)
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 template
let template = argv.t || argv.template
let message = 'Select a template:'
let isValidTemplate = false
let templateSymbol = ''
// --template expects a value
if (typeof template === 'string') {
const availableTemplates = TEMPLATES.map((template) => stripColors(template[1]))
isValidTemplate = availableTemplates.includes(template)
message = `${template} isn't a valid template. Please choose from below:`
}
if (!template || !isValidTemplate) {
/**
* @type {{ t: string }}
*/
const { t } = await prompt({
type: 'select',
name: 't',
message,
choices: TEMPLATES.map(i => i.join(' ~> '))
})
const temp = t.split(' ~> ')
template = stripColors(temp[1])
templateSymbol = temp[0]
}
const templateDir = path.join(__dirname, `template-${template}`)
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)
}
}
const cmdCd = () => {
console.log(`\nDone. Now run:\n`)
if (root !== cwd) {
console.log(` cd ${path.relative(cwd, root)}`)
}
}
const files = fs.readdirSync(templateDir)
for (const file of files) {
write(file)
if (file === 'package.json') {
const pkg = require(path.join(templateDir, `package.json`))
pkg.name = pkgName
write('package.json', JSON.stringify(pkg, null, 2))
const isWasm = ['🦀'].includes(templateSymbol) || /wasm-/.test(template)
cmdCd()
cmdNode(isWasm)
}
}
}
function cmdNode(isWasm) {
const pkgManager = /yarn/.test(process.env.npm_execpath) ? 'yarn' : 'npm'
console.log(` ${pkgManager === 'yarn' ? `yarn` : `npm install`}`)
console.log(` ${pkgManager === 'yarn' ? `yarn dev` : `npm run dev`}`)
isWasm && console.log(` ${pkgManager === 'yarn' ? `yarn rsw:build` : `npm run rsw:build`}`)
console.log()
isWasm && wasmLink()
}
function wasmLink() {
console.log(gray(`---------------------------------------
[Rust]: https://www.rust-lang.org
[wasm-pack]: https://github.com/rustwasm/wasm-pack
[vite-plugin-rsw]: https://github.com/lencx/vite-plugin-rsw
[rsw-node]: https://github.com/lencx/rsw-node
[learn-wasm]: https://github.com/lencx/learn-wasm
[awesome-rsw]: https://github.com/lencx/awesome-rsw
[WebAssembly Series](https://github.com/lencx/z/discussions/22)
`))
}
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)
}
}
}
async function getValidPackageName(projectName) {
const packageNameRegExp = /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/
if (packageNameRegExp.test(projectName)) {
return projectName
} else {
const suggestedPackageName = projectName
.trim()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/^[._]/, '')
.replace(/[^a-z0-9-~]+/g, '-')
/**
* @type {{ inputPackageName: string }}
*/
const { inputPackageName } = await prompt({
type: 'input',
name: 'inputPackageName',
message: `Package name:`,
initial: suggestedPackageName,
validate: (input) =>
packageNameRegExp.test(input) ? true : 'Invalid package.json name'
})
return inputPackageName
}
}
init().catch((e) => {
console.error(e)
})