-
Notifications
You must be signed in to change notification settings - Fork 1
/
instructions.ts
194 lines (174 loc) · 5.15 KB
/
instructions.ts
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
import { join } from 'path'
import * as sinkStatic from '@adonisjs/sink'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
/**
* Prompt choices for the frontend framework selection
*/
const JS_PROMPT_CHOICES = [
{
name: 'vue' as const,
message: 'Vue',
hint: ' (Install the vue-loader)',
},
{
name: 'react' as const,
message: 'React',
hint: ' (Install Babel React Preset)',
},
{
name: 'none' as const,
message: 'None',
hint: '',
},
]
/**
* Prompt choices for the frontend CSS system selection
*/
const CSS_PROMPT_CHOICES = [
{
name: 'scss' as const,
message: 'Sass / Scss',
hint: ' (Install the sass-loader)',
},
{
name: 'less' as const,
message: 'LESS',
hint: ' (Install the less-loader)',
},
{
name: 'styl' as const,
message: 'Stylus',
hint: ' (Install the stylus-loader)',
},
{
name: 'css' as const,
message: 'None',
hint: '',
},
]
function getFrontendFramework (sink: typeof sinkStatic) {
return sink.getPrompt().choice('Which frontend framework do you want to use?', JS_PROMPT_CHOICES)
}
function getCssLang (sink: typeof sinkStatic) {
return sink.getPrompt().choice('Which CSS language do you want to use?', CSS_PROMPT_CHOICES)
}
/**
* Returns absolute path to the stub relative from the templates
* directory
*/
function getStub (...relativePaths: string[]) {
return join(__dirname, 'templates', ...relativePaths)
}
/**
* Instructions to be executed when setting up the package.
*/
export default async function instructions (
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
) {
const frontendFramework = await getFrontendFramework(sink)
const cssLang = await getCssLang(sink)
const typescript = await sink.getPrompt().confirm('Do you want to use TypeScript?')
const postCss = await sink.getPrompt().confirm('Do you want to use PostCSS?')
const pkg = new sink.files.PackageJsonFile(projectRoot)
switch (frontendFramework) {
case 'vue': {
pkg.install('vue-loader', undefined, true)
pkg.install('vue', undefined, true)
break
}
case 'react': {
pkg.install('@babel/preset-react', undefined, true)
pkg.install('react', undefined, true)
pkg.install('react-dom', undefined, true)
pkg.install('prop-types', undefined, true)
break
}
default: {}
}
switch (cssLang) {
case 'scss': {
pkg.install('sass-loader', undefined, true)
pkg.install('sass', undefined, true)
break
}
case 'less': {
pkg.install('less-loader', undefined, true)
break
}
case 'styl': {
pkg.install('stylus-loader', undefined, true)
break
}
default: {}
}
if (postCss) {
pkg.install('postcss-loader', undefined, true)
pkg.install('autoprefixer', undefined, true)
pkg.set('browserslist', [
'> 0.5%',
'last 2 versions',
'Firefox ESR',
'not dead',
])
sink.utils.copyFiles(join(__dirname, 'templates'), projectRoot, ['postcss.config.js'])
}
if (typescript) {
pkg.install('ts-loader', undefined, true)
sink.utils.copyFiles(join(__dirname, 'templates'), app.resourcesPath('ts'), ['tsconfig.json'])
}
/**
* Scripts to run encore
*/
pkg.appendScript('build:front', 'npx encore dev --watch')
pkg.appendScript('build:front:prod', 'NODE_ENV=production npx encore production')
/**
* Copy webpack config file
*/
const configPath = join(projectRoot, 'webpack.config.js')
const webpackConfig = new sink.files.MustacheFile(projectRoot, configPath, getStub('webpack.config.txt'))
webpackConfig.apply({
typescript,
postCss,
jsFile: typescript ? 'ts' : 'js',
cssFile: cssLang,
sass: cssLang === 'scss',
less: cssLang === 'less',
stylus: cssLang === 'styl',
react: frontendFramework === 'react',
vue: frontendFramework === 'vue',
}).commit()
sink.logger.action('create').succeeded(configPath)
const cssFilePath = app.resourcesPath(`${cssLang}/app.${cssLang}`)
const cssFile = new sink.files.MustacheFile(projectRoot, cssFilePath, getStub('app.txt'))
cssFile.apply({
lang: cssLang,
}).commit()
sink.logger.action('create').succeeded(cssFilePath)
const jsFilePath = app.resourcesPath(`${typescript ? 'ts' : 'js'}/app.${typescript ? 'ts' : 'js'}`)
const jsFile = new sink.files.MustacheFile(projectRoot, jsFilePath, getStub('app.txt'))
jsFile.apply({
lang: typescript ? 'TypeScript' : 'JavaScript',
}).commit()
sink.logger.action('create').succeeded(jsFilePath)
pkg.install('@symfony/webpack-encore', undefined, true)
pkg.install('webpack-notifier', undefined, true)
/**
* Install required dependencies
*/
const spinner = sink.logger.await(`Installing packages: ${pkg.getInstalls().list.join(', ')}...`)
await pkg.commitAsync()
spinner.stop()
sink.logger.success('Packages installed!')
console.log(' ')
/**
* Usage instructions
*/
sink
.instructions()
.heading('Run following commands to get started')
.add(`${sink.logger.colors.cyan('npm run build:front')} to start the build`)
.add(`${sink.logger.colors.cyan('npm run build:front:prod')} to build for production`)
.render()
}