This repository was archived by the owner on Jul 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreset.ts
95 lines (81 loc) · 2.8 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
import { Preset, color } from 'apply';
interface Context {
presetName: string;
presetTitle: string;
install: string[];
command: string[];
uninstall: string[];
options: string[];
}
const formatPackageList = (packages: string[]) => {
return packages
.map((name) => color.magenta(name))
.join(', ')
.replace(/,(?!.*,)/gim, ' and');
};
// Sets the name.
Preset.setName<Context>(({ context }) => context.presetTitle);
// Sets up the context with the arguments and options given.
Preset.hook<Context>(({ context, args, options }) => {
context.install = [];
context.uninstall = [];
context.options = Object.keys(options)
.filter((option) => ['auth', 'extra'].includes(option))
.map((option) => `--${option}`);
if (options.breeze) {
context.presetName = 'breeze';
} else if (options.tall) {
context.presetName = 'tall';
} else {
context.presetName = args[2];
}
if (!context.presetName) {
throw new Error(`The preset name is missing.`);
}
switch (context.presetName) {
// Actual laravel/ui presets
case 'bootstrap':
case 'vue':
case 'react':
context.presetTitle = context.presetName;
context.install.push('laravel/ui');
context.command = ['artisan', 'ui', context.presetName, ...context.options];
break;
// Support for Breeze
case 'breeze':
context.presetTitle = `laravel/breeze`;
context.install.push('laravel/breeze');
context.uninstall.push('laravel/breeze');
context.command = ['artisan', 'breeze:install'];
break;
// Special TALL handling
case 'tall':
context.install.push('livewire/livewire');
context.uninstall.push('laravel/ui');
// Common to all laravel-frontend-presets
default:
const packageName = `laravel-frontend-presets/${context.presetName}`;
context.presetTitle = packageName;
context.install.push('laravel/ui', packageName);
context.uninstall.push(packageName);
context.command = ['artisan', 'ui', context.presetName, ...context.options];
}
}).withoutTitle();
// Requires the packages.
Preset.execute<Context>('composer')
.withArguments(({ context }) => ['require', ...context.install])
.withTitle(({ context }) => `Installing ${formatPackageList(context.install)}...`);
// Executes the Artisan commands.
Preset.execute<Context>('php')
.withArguments(({ context }) => context.command)
.withTitle(({ context }) => `Applying ${color.magenta(context.presetTitle)}...`);
// Removes the packages.
Preset.execute<Context>('composer')
.withArguments(({ context }) => ['remove', ...context.uninstall])
.withTitle(({ context }) => `Removing ${formatPackageList(context.uninstall)}...`)
.if(({ context }) => context.uninstall.length > 0);
// Displays instructions.
Preset.instruct([
`Install dependencies ${color.magenta('npm install')} or ${color.magenta('yarn')}`,
`Run ${color.magenta('npm run dev')} or ${color.magenta('yarn dev')}`,
]);