forked from alex8088/quick-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
207 lines (171 loc) · 5.27 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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const minimist = require('minimist')
const prompts = require('prompts')
const { red, reset } = require('kolorist')
const { copy, emptyDir, readJsonFile, writeJsonFile, writeFile } = require('./utils/fsExtra')
const getCommand = require('./utils/getCommand')
const DEFAULT_PRO_NAME = 'my-docs'
async function init() {
const argv = minimist(process.argv.slice(2), { string: [0] })
const cwd = process.cwd()
let targetDir = argv._[0]
let ts = argv.ts
let i18n = argv.i18n || false
let locale = argv.locale || ''
let skip = argv.skip || false
const defaultProjectName = !targetDir ? DEFAULT_PRO_NAME : targetDir
let result = {}
try {
result = await prompts([
{
name: 'projectName',
type: targetDir ? null : 'text',
message: reset('Project name:'),
initial: defaultProjectName,
onState: (state) => (targetDir = state.value.trim() || defaultProjectName)
},
{
name: 'shouldOverwrite',
type: () => (canSafelyOverwrite(targetDir) || skip ? null : 'confirm'),
message: () =>
(targetDir === '.' ? 'Current directory' : `Target directory "${targetDir}"`) +
` is not empty. Remove existing files and continue?`
},
{
name: 'overwriteChecker',
type: (_, { shouldOverwrite } = {}) => {
if (shouldOverwrite === false) {
throw new Error(red('✖') + ' Operation cancelled')
}
return null
}
},
{
name: 'packageName',
type: () => (isValidPackageName(targetDir) ? null : 'text'),
message: 'Package name:',
initial: () => toValidPackageName(targetDir),
validate: (dir) => isValidPackageName(dir) || 'Invalid package.json name'
},
{
name: 'needsTypeScript',
type: () => (skip || ts ? null : 'toggle'),
message: 'Add TypeScript?',
initial: false,
active: 'Yes',
inactive: 'No'
},
{
name: 'needsI18n',
type: () => (skip || i18n ? null : 'toggle'),
message: 'Need i18n?',
initial: false,
active: 'Yes',
inactive: 'No'
},
{
name: 'defaultLocale',
type: (needsI18n) =>
skip || needsI18n || (locale && ['zh'].includes(locale)) ? null : 'select',
message: 'Select a locale',
initial: 0,
choices: [
{
title: 'default',
value: ''
},
{
title: 'zh',
value: 'zh'
}
]
}
])
} catch (cancelled) {
console.log(cancelled.message)
return
}
let {
shouldOverwrite = skip,
packageName = targetDir,
needsTypeScript = ts,
needsI18n = i18n,
defaultLocale = locale
} = result
const root = path.join(cwd, targetDir)
if (fs.existsSync(root) && shouldOverwrite) {
emptyDir(root)
} else if (!fs.existsSync(root)) {
fs.mkdirSync(root)
}
const localePrefix = defaultLocale === 'zh' ? '-zh' : ''
const template = `docs${needsI18n ? '-i18n' : localePrefix}${needsTypeScript ? '-ts' : ''}`
console.log(`\nScaffolding project in ${root}...`)
const templateRoot = path.join(__dirname, 'template')
const render = function render(templateName, dir = '') {
const templateDir = path.resolve(templateRoot, templateName)
copy(templateDir, dir || root)
}
// Render base template
render('base')
render(`base-${needsTypeScript ? 'ts' : 'js'}`)
// Render i18n template
if (needsI18n) {
render('i18n/en', path.join(root, 'docs'))
render('i18n/zh', path.join(root, 'docs/zh'))
} else {
render(`i18n/${defaultLocale ? defaultLocale : 'en'}`, path.join(root, 'docs'))
}
// Render variant template
render(template)
fs.renameSync(path.resolve(root, '_gitignore'), path.resolve(root, '.gitignore'))
const packageFile = path.join(root, `package.json`)
const pkg = readJsonFile(packageFile)
pkg.name = packageName
writeJsonFile(packageFile, pkg)
const userAgent = process.env.npm_config_user_agent ?? ''
const pkgManager = /pnpm/.test(userAgent) ? 'pnpm' : /yarn/.test(userAgent) ? 'yarn' : 'npm'
const readme = `# ${packageName}
This site is built with [VitePress](https://vitepress.dev).
## Install
\`\`\`bash
$ ${getCommand(pkgManager, 'install')}
\`\`\`
## Development
\`\`\`bash
$ ${getCommand(pkgManager, 'dev')}
\`\`\`
## Build
\`\`\`bash
$ ${getCommand(pkgManager, 'build')}
\`\`\`
`
writeFile(path.resolve(root, 'README.md'), readme)
console.log(`\nDone. Now run:\n`)
if (root !== cwd) {
const dir = path.relative(cwd, root)
console.log(` cd ${dir.includes(' ') ? `"${dir}"` : dir}`)
}
console.log(` ${getCommand(pkgManager, 'install')}`)
console.log(` ${getCommand(pkgManager, 'dev')}`)
console.log()
}
function canSafelyOverwrite(dir) {
return !fs.existsSync(dir) || fs.readdirSync(dir).length === 0
}
function isValidPackageName(projectName) {
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(projectName)
}
function toValidPackageName(projectName) {
return projectName
.trim()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/^[._]/, '')
.replace(/[^a-z0-9-~]+/g, '-')
}
init().catch((e) => {
console.error(e)
})