forked from nuxt/create-nuxt-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saofile.js
159 lines (139 loc) · 4.29 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
const { join, relative } = require('path')
const glob = require('glob')
const spawn = require('cross-spawn')
const validate = require('validate-npm-package-name')
const rootDir = __dirname
module.exports = {
prompts: require('./prompts'),
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")',
'jsconfig.json': 'devTools.includes("jsconfig.json")'
}
})
actions.push({
type: 'move',
patterns: {
gitignore: '.gitignore',
'_package.json': 'package.json',
'_.eslintrc.js': '.eslintrc.js'
}
})
actions.push({
type: 'modify',
files: 'package.json',
handler (data) {
delete data.scripts['']
delete data.dependencies['']
delete data.devDependencies['']
return data
}
})
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`)
}
}
}