forked from nuxt/create-nuxt-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaofile.js
203 lines (180 loc) · 6.3 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
const { dirname, join, relative } = require('path')
const fs = require('fs')
const spawn = require('cross-spawn')
const validate = require('validate-npm-package-name')
const pkg = require('./package')
const cnaTemplateDir = join(dirname(require.resolve('cna-template/package.json')))
const templateDir = join(cnaTemplateDir, 'template')
const frameworksDir = join(templateDir, 'frameworks')
const addExecutable = filename => new Promise(
resolve => fs.chmod(filename, 0o755, resolve)
)
module.exports = {
prompts: require('./prompts'),
templateData () {
const typescript = this.answers.language.includes('ts')
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 stylelint = this.answers.linter.includes('stylelint')
const commitlint = this.answers.linter.includes('commitlint')
const axios = this.answers.features.includes('axios')
const content = this.answers.features.includes('content')
const pm = this.answers.pm === 'yarn' ? 'yarn' : 'npm'
const pmRun = this.answers.pm === 'yarn' ? 'yarn' : 'npm run'
const { cliOptions = {} } = this.sao.opts
const edge = cliOptions.edge ? '-edge' : ''
return {
typescript,
pwa,
eslint,
prettier,
lintStaged,
stylelint,
commitlint,
axios,
edge,
pm,
pmRun,
content
}
},
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 { linter } = this.answers
const eslint = linter.includes('eslint')
const lintStaged = eslint && linter.includes('lintStaged')
const commitlint = linter.includes('commitlint')
const husky = lintStaged || commitlint
const actions = [{
type: 'add',
files: '**',
templateDir: join(templateDir, 'nuxt'),
filters: {
'static/icon.png': 'features.includes("pwa")',
'content/hello.md': 'features.includes("content")',
'pages/content.vue': 'features.includes("content")',
'.husky/.gitignore': husky,
'.husky/commit-msg': commitlint,
'.husky/pre-commit': lintStaged,
'.husky/common.sh': husky && this.answers.pm === 'yarn'
}
}]
if (this.answers.ui !== 'none') {
actions.push({
type: 'add',
files: '**',
templateDir: join(frameworksDir, this.answers.ui)
})
}
if (this.answers.test !== 'none') {
actions.push({
type: 'add',
files: '**',
templateDir: join(frameworksDir, this.answers.test)
})
}
if (this.answers.ci && this.answers.ci !== 'none') {
actions.push({
type: 'add',
files: '**',
templateDir: join(frameworksDir, this.answers.ci)
})
}
actions.push({
type: 'add',
files: '*',
filters: {
'_.eslintrc.js': 'linter.includes("eslint")',
'_.prettierignore': 'linter.includes("prettier")',
'_.prettierrc': 'linter.includes("prettier")',
'_jsconfig.json': 'devTools.includes("jsconfig.json")',
'tsconfig.json': 'language.includes("ts")',
'semantic.yml': 'devTools.includes("semantic-pull-requests")',
'_stylelint.config.js': 'linter.includes("stylelint")',
'_commitlint.config.js': 'linter.includes("commitlint")',
'dependabot.yml': 'devTools.includes("dependabot")'
},
templateDir
})
actions.push({
type: 'move',
patterns: {
gitignore: '.gitignore',
'_package.json': 'package.json',
'_.prettierignore': '.prettierignore',
'_.prettierrc': '.prettierrc',
'_.eslintrc.js': '.eslintrc.js',
'_jsconfig.json': 'jsconfig.json',
'_stylelint.config.js': 'stylelint.config.js',
'_commitlint.config.js': 'commitlint.config.js',
'semantic.yml': '.github/semantic.yml',
'dependabot.yml': '.github/dependabot.yml'
}
})
const generator = this
actions.push({
type: 'modify',
files: 'package.json',
handler (data) {
return { ...data, ...pkg.load(generator) }
}
})
// For compiling package.json
actions.push({
type: 'add',
files: 'package.json',
templateDir: this.outDir
})
actions.push({
type: 'remove',
files: 'package.js'
})
return actions
},
async completed () {
if (this.answers.vcs === 'git') {
this.gitInit()
}
const huskyDir = join(this.outDir, '.husky')
if (this.answers.linter.includes('lintStaged')) {
await addExecutable(join(huskyDir, 'pre-commit'))
}
if (this.answers.linter.includes('commitlint')) {
await addExecutable(join(huskyDir, 'commit-msg'))
}
await this.npmInstall({ npmClient: this.answers.pm })
if (['eslint', 'stylelint', 'prettier'].some(linter => this.answers.linter.includes(linter))) {
spawn.sync(this.answers.pm, ['run', 'lintfix'], {
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`)
}
if (this.answers.language.includes('ts')) {
console.log(chalk`\n {bold For TypeScript users.} \n\n See : https://typescript.nuxtjs.org/cookbook/components/`)
}
}
}