forked from nuxt/create-nuxt-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saofile.js
256 lines (237 loc) · 6.82 KB
/
saofile.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const { join, relative } = require('path')
const { random } = require('superb')
const glob = require('glob')
const spawn = require('cross-spawn')
const validate = require('validate-npm-package-name')
const rootDir = __dirname
module.exports = {
prompts: [
{
name: 'name',
message: 'Project name',
default: '{outFolder}'
},
{
name: 'description',
message: 'Project description',
default: `My ${random()} Nuxt.js project`
},
{
name: 'author',
type: 'string',
message: 'Author name',
default: '{gitUser.name}',
store: true
},
{
name: 'pm',
message: 'Choose the package manager',
choices: [
{ name: 'Yarn', value: 'yarn' },
{ name: 'Npm', value: 'npm' }
],
type: 'list',
default: 'yarn'
},
{
name: 'ui',
message: 'Choose UI framework',
type: 'list',
pageSize: 10,
choices: [
{ name: 'None', value: 'none' },
{ name: 'Ant Design Vue', value: 'ant-design-vue' },
{ name: 'Bootstrap Vue', value: 'bootstrap' },
{ name: 'Buefy', value: 'buefy' },
{ name: 'Bulma', value: 'bulma' },
{ name: 'Element', value: 'element-ui' },
{ name: 'iView', value: 'iview' },
{ name: 'Tachyons', value: 'tachyons' },
{ name: 'Tailwind CSS', value: 'tailwind' },
{ name: 'Vuetify.js', value: 'vuetify' }
],
default: 'none'
},
{
name: 'server',
message: 'Choose custom server framework',
type: 'list',
pageSize: 10,
choices: [
{ name: 'None (Recommended)', value: 'none' },
{ name: 'AdonisJs', value: 'adonis' },
{ name: 'Express', value: 'express' },
{ name: 'Fastify', value: 'fastify' },
{ name: 'Feathers', value: 'feathers' },
{ name: 'hapi', value: 'hapi' },
{ name: 'Koa', value: 'koa' },
{ name: 'Micro', value: 'micro' }
],
default: 'none'
},
{
name: 'features',
message: 'Choose Nuxt.js modules',
type: 'checkbox',
pageSize: 10,
choices: [
{ name: 'Axios', value: 'axios' },
{ name: 'Progressive Web App (PWA) Support', value: 'pwa' }
],
default: []
},
{
name: 'linter',
message: 'Choose linting tools',
type: 'checkbox',
pageSize: 10,
choices: [
{ name: 'ESLint', value: 'eslint' },
{ name: 'Prettier', value: 'prettier' },
{ name: 'Lint staged files', value: 'lintStaged' }
],
default: []
},
{
name: 'test',
message: 'Choose test framework',
type: 'list',
choices: [
{ name: 'None', value: 'none' },
{ name: 'Jest', value: 'jest' },
{ name: 'AVA', value: 'ava' }
],
default: 'none'
},
{
name: 'mode',
message: 'Choose rendering mode',
type: 'list',
choices: [
{ name: 'Universal (SSR)', value: 'universal' },
{ name: 'Single Page App', value: 'spa' }
],
default: 'universal'
}
],
templateData () {
const pwa = this.answers.features.includes('pwa')
const eslint = this.answers.linter.includes('eslint')
const prettier = this.answers.linter.includes('prettier')
const lintStaged = eslint && this.answers.linter.includes('lintStaged')
const axios = this.answers.features.includes('axios')
const esm = this.answers.server === 'none'
const pmRun = this.answers.pm === 'yarn' ? 'yarn' : 'npm run'
const { cliOptions = {} } = this.sao.opts
const edge = cliOptions.edge ? '-edge' : ''
return {
pwa,
eslint,
prettier,
lintStaged,
axios,
esm,
edge,
pmRun
}
},
actions () {
const validation = validate(this.answers.name)
validation.warnings && validation.warnings.forEach((warn) => {
console.warn('Warning:', warn)
})
validation.errors && validation.errors.forEach((err) => {
console.error('Error:', err)
})
validation.errors && validation.errors.length && process.exit(1)
const actions = [{
type: 'add',
files: '**',
templateDir: 'template/nuxt',
filters: {
'static/icon.png': 'features.includes("pwa")'
}
}]
if (this.answers.ui !== 'none') {
actions.push({
type: 'add',
files: '**',
templateDir: `template/frameworks/${this.answers.ui}`
})
}
if (this.answers.test !== 'none') {
actions.push({
type: 'add',
files: '**',
templateDir: `template/frameworks/${this.answers.test}`
})
}
if (this.answers.server !== 'none') {
if (this.answers.server === 'adonis') {
const files = {}
for (const action of actions) {
const options = { cwd: join(rootDir, action.templateDir), dot: true }
for (const file of glob.sync(`*`, options)) {
files[file] = `resources/${file}`
}
}
files['nuxt.config.js'] = 'config/nuxt.js'
actions.push({
type: 'move',
patterns: files
})
}
actions.push({
type: 'add',
files: '**',
templateDir: `template/frameworks/${this.answers.server}`
})
}
actions.push({
type: 'add',
files: '*',
filters: {
'_.eslintrc.js': 'linter.includes("eslint")',
'.prettierrc': 'linter.includes("prettier")'
}
})
actions.push({
type: 'move',
patterns: {
gitignore: '.gitignore',
'_package.json': 'package.json',
'_.eslintrc.js': '.eslintrc.js'
}
})
return actions
},
async completed () {
this.gitInit()
await this.npmInstall({ npmClient: this.answers.pm })
if (this.answers.linter.includes('eslint')) {
const options = ['run', 'lint', '--', '--fix']
if (this.answers.pm === 'yarn') {
options.splice(2, 1)
}
spawn.sync(this.answers.pm, options, {
cwd: this.outDir,
stdio: 'inherit'
})
}
const chalk = this.chalk
const isNewFolder = this.outDir !== process.cwd()
const relativeOutFolder = relative(process.cwd(), this.outDir)
const cdMsg = isNewFolder ? chalk`\t{cyan cd ${relativeOutFolder}}\n` : ''
const pmRun = this.answers.pm === 'yarn' ? 'yarn' : 'npm run'
console.log(chalk`\n🎉 {bold Successfully created project} {cyan ${this.answers.name}}\n`)
console.log(chalk` {bold To get started:}\n`)
console.log(chalk`${cdMsg}\t{cyan ${pmRun} dev}\n`)
console.log(chalk` {bold To build & start for production:}\n`)
console.log(chalk`${cdMsg}\t{cyan ${pmRun} build}`)
console.log(chalk`\t{cyan ${pmRun} start}\n`)
if (this.answers.test !== 'none') {
console.log(chalk` {bold To test:}\n`)
console.log(chalk`${cdMsg}\t{cyan ${pmRun} test}\n`)
}
}
}