-
Notifications
You must be signed in to change notification settings - Fork 9
/
preset.ts
388 lines (364 loc) · 8.07 KB
/
preset.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import fs from 'node:fs'
interface Options {
i18n: boolean
pest: boolean
strict: boolean
ide: boolean
}
export default definePreset<Options>({
name: 'laravel:hybridly',
options: {
i18n: false,
pest: true,
strict: true,
ide: true,
},
handler: async ({ options }) => {
if (options.pest && !fs.existsSync('tests/Pest.php')) {
await applyNestedPreset({
title: 'install Pest',
preset: 'laravel-presets/pest',
})
}
await installBase(options)
if (options.i18n) {
await group({
title: 'install i18n',
handler: async () => await installI18n(),
})
}
if (options.strict) {
await group({
title: 'apply strict mode',
handler: async () => await applyStrictMode(),
})
}
if (options.ide) {
await group({
title: 'setup ide helper',
handler: async () => await applyIdeHelper(),
})
}
},
})
async function installBase({ i18n, ide }: Options) {
await deletePaths({
title: 'delete unused project files',
paths: [
'vite.config.js',
'resources',
],
})
await editFiles({
title: 'update .gitignore',
files: '.gitignore',
operations: [
{
type: 'add-line',
position: 'append',
lines: '.hybridly',
},
...(ide
? [{
type: 'add-line' as const,
position: 'append' as const,
lines: ['_ide_helper*', '.phpstorm.meta.php'],
}]
: []),
],
})
await editFiles({
title: 'add middleware',
files: 'bootstrap/app.php',
operations: [
{
type: 'remove-line',
match: /\/\//,
},
{
type: 'add-line',
match: /->withMiddleware/,
position: 'after',
indent: 8,
lines: [
'$middleware->appendToGroup(\'web\', [\\App\\Http\\Middleware\\HandleHybridRequests::class]);',
],
},
],
})
await extractTemplates({
title: 'extract templates',
from: 'base',
})
await editFiles({
title: 'remove unused dependencies',
files: 'package.json',
operations: {
type: 'edit-json',
delete: [
'devDependencies.lodash',
'devDependencies.laravel-vite-plugin',
],
},
})
await installPackages({
title: 'add npm dependencies',
for: 'node',
dev: true,
packages: [
'hybridly',
'axios',
// Other
'@types/node',
'typescript',
// Vue
'vue',
'@vue/runtime-core',
'@vueuse/core',
'@unhead/vue',
// Tailwind CSS
'autoprefixer',
'tailwindcss',
'postcss',
'@tailwindcss/forms',
// i18n
...(i18n
? [
'@intlify/unplugin-vue-i18n',
'vue-i18n',
]
: []),
],
})
await editFiles({
title: 'configure PostCSS',
files: 'package.json',
operations: {
type: 'edit-json',
merge: {
postcss: {
plugins: {
autoprefixer: {},
tailwindcss: {},
},
},
},
},
})
await installPackages({
title: 'add php dependencies',
for: 'php',
packages: [
'hybridly/laravel',
'spatie/laravel-data',
],
additionalArgs: ['-W'],
})
await installPackages({
title: 'add php dev dependencies',
for: 'php',
dev: true,
packages: [
'spatie/laravel-typescript-transformer',
...(ide ? ['barryvdh/laravel-ide-helper'] : []),
],
additionalArgs: ['-W'],
})
await executeCommand({
title: 'publish typescript transformer config',
command: 'php',
arguments: ['artisan', 'vendor:publish', '--tag=typescript-transformer-config'],
})
await editFiles({
title: 'update typescript transformer config',
files: 'config/typescript-transformer.php',
operations: [
{
type: 'remove-line',
match: /Spatie\\TypeScriptTransformer\\Collectors\\DefaultCollector::class,/,
},
{
type: 'add-line',
match: /'collectors' => \[/,
position: 'after',
indent: ' ',
lines: [
'Hybridly\\Support\\TypeScriptTransformer\\DataResourceTypeScriptCollector::class,',
'Spatie\\LaravelData\\Support\\TypeScriptTransformer\\DataTypeScriptCollector::class,',
],
},
{
type: 'remove-line',
match: /Spatie\\LaravelTypeScriptTransformer\\Transformers\\SpatieStateTransformer::class,/,
count: 3,
},
{
type: 'add-line',
match: /'transformers' => \[/,
position: 'after',
indent: ' ',
lines: [
'Spatie\\LaravelData\\Support\\TypeScriptTransformer\\DataTypeScriptTransformer::class,',
'Spatie\\TypeScriptTransformer\\Transformers\\EnumTransformer::class,',
],
},
],
})
await editFiles({
title: 'update welcome route',
files: 'routes/web.php',
operations: {
type: 'update-content',
update: (content) => content.replace('view(', 'hybridly('),
},
})
}
async function installI18n() {
await extractTemplates({
title: 'extract i18n scaffolding',
from: 'i18n',
})
await editFiles({
title: 'register Vue i18n plugin',
files: 'resources/application/main.ts',
operations: [
// add import
{
type: 'add-line',
match: /@unhead\/vue/,
position: 'after',
lines: "import i18n from './i18n'",
},
// add plugin
{
type: 'add-line',
match: /vue\.use\(/,
position: 'before',
lines: 'vue.use(i18n)',
},
],
})
await editFiles({
title: 'register Vite i18n plugin',
files: 'vite.config.ts',
operations: [
// add import
{
type: 'add-line',
match: /import run from 'vite-plugin-run'/,
position: 'after',
lines: "import i18n from '@intlify/unplugin-vue-i18n/vite'",
},
// add plugin
{
type: 'add-line',
match: /vue\(/,
position: 'after',
lines: [
'i18n({',
" include: path.resolve(__dirname, '.hybridly/locales.json'),",
'}),',
],
},
// add auto-imports
{
type: 'add-line',
match: /laravel: {/,
position: 'before',
lines: [
'autoImports: {',
' imports: [',
' { \'@/application/i18n\': [\'t\', \'i18n\'] },',
' ],',
'},',
],
},
],
})
}
async function applyStrictMode() {
await editFiles({
files: 'app/Providers/AppServiceProvider.php',
operations: [
{
type: 'add-line',
match: /use Illuminate\\Support\\ServiceProvider;/,
position: 'before',
lines: [
'use App\\Models\\User;',
'use Carbon\\CarbonImmutable;',
'use Illuminate\\Database\\Eloquent\\Model;',
'use Illuminate\\Database\\Eloquent\\Relations\\Relation;',
'use Illuminate\\Foundation\\Console\\CliDumper;',
'use Illuminate\\Foundation\\Http\\HtmlDumper;',
'use Illuminate\\Support\\Facades\\Date;',
'use Illuminate\\Support\\Facades\\Validator;',
],
},
{
type: 'add-line',
match: /public function boot\(\)/,
position: 'after',
lines: [
'{',
' HtmlDumper::dontIncludeSource();',
' CliDumper::dontIncludeSource();',
' Validator::excludeUnvalidatedArrayKeys();',
' Model::shouldBeStrict();',
' Model::unguard();',
' Relation::enforceMorphMap([',
" 'user' => User::class,",
' ]);',
' Date::use(CarbonImmutable::class);',
],
},
{
type: 'remove-line',
match: /Date::use/,
start: 1,
count: 2,
},
],
})
}
async function applyIdeHelper() {
await editFiles({
title: 'add ide helper scripts',
files: 'composer.json',
operations: [
{
type: 'edit-json',
delete: 'scripts',
},
{
type: 'edit-json',
merge: {
scripts: {
'test': 'pest',
'lint': 'php-cs-fixer fix --allow-risky=yes --dry-run',
'lint:fix': 'php-cs-fixer fix --allow-risky=yes',
'post-update-cmd': '@php artisan vendor:publish --tag=laravel-assets --ansi --force',
'post-root-package-install': "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
'post-autoload-dump': [
'Illuminate\\Foundation\\ComposerScripts::postAutoloadDump',
'@php artisan package:discover --ansi',
'([ $COMPOSER_DEV_MODE -eq 1 ] && composer autocomplete) || true',
],
'autocomplete': [
'@php artisan ide-helper:eloquent || true',
'@php artisan ide-helper:generate || true',
'@php artisan ide-helper:meta || true',
'@php artisan ide-helper:models -M || true',
],
},
},
},
],
})
await executeCommand({
title: 'generate ide helpers',
command: 'composer',
arguments: ['autocomplete'],
ignoreExitCode: true,
})
}