Skip to content

Commit 57d7063

Browse files
committed
feat: add create command walkthrough
1 parent 590ad3a commit 57d7063

File tree

1 file changed

+97
-19
lines changed

1 file changed

+97
-19
lines changed

src/CreateCommand.php

Lines changed: 97 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ class CreateCommand extends Command
3333
{--basic? : Create a raw leaf project}
3434
{--api? : Create a new Leaf MVC project for APIs}
3535
{--mvc? : Create a new Leaf MVC project}
36+
{--custom? : Scaffold a personalized Leaf app}
3637
{--docker? : Scaffold a docker environment}
3738
{--force? : Forces install even if the directory already exists}';
3839

40+
protected $description = 'Create a new Leaf project';
41+
3942
protected function handle(): int
4043
{
4144
$needsUpdate = Package::updateAvailable();
@@ -56,11 +59,19 @@ protected function handle(): int
5659
$this->projectName = $this->argument('project-name');
5760
$this->projectType = $this->option('basic') ? 'basic' : ($this->option('api') ? 'api' : ($this->option('mvc') ? 'mvc' : null));
5861

59-
$selections = sprout()->prompt([
62+
$this->writeln("\033[32m
63+
_ __ _ _ ___
64+
| | ___ __ _ / _| | || | / _ \
65+
| | / _ \/ _` | |_ | || |_| | | |
66+
| |__| __/ (_| | _| |__ _| |_| |
67+
|_____\___|\__,_|_| |_|(_)___/
68+
\033[0m\n");
69+
70+
$scaffoldOptions = sprout()->prompt([
6071
[
6172
'type' => $this->projectName ? null : 'text',
6273
'name' => 'name',
63-
'message' => 'What is your project name?',
74+
'message' => 'Project name',
6475
'default' => 'my-leaf-project',
6576
'validate' => function ($value) {
6677
if (empty($value)) {
@@ -73,18 +84,18 @@ protected function handle(): int
7384
[
7485
'type' => $this->projectType ? null : 'select',
7586
'name' => 'type',
76-
'message' => 'What type of project do you want?',
87+
'message' => 'Select a preset',
7788
'default' => 0,
7889
'choices' => [
7990
['title' => 'Basic Leaf app', 'value' => 'basic'],
80-
['title' => 'Leaf MVC app', 'value' => 'mvc'],
81-
['title' => 'Leaf MVC app for APIs', 'value' => 'api'],
91+
['title' => 'Full-stack MVC app', 'value' => 'mvc'],
92+
['title' => 'Leaf MVC API app', 'value' => 'api'],
8293
],
83-
]
94+
],
8495
]);
8596

86-
$this->projectName ??= $selections['name'];
87-
$this->projectType ??= $selections['type'];
97+
$this->projectName ??= $scaffoldOptions['name'];
98+
$this->projectType ??= $scaffoldOptions['type'];
8899

89100
$commands = [];
90101
$directory = $this->projectName !== '.' ? getcwd() . '/' . $this->projectName : getcwd();
@@ -113,7 +124,7 @@ protected function handle(): int
113124
$commands[] = 'cd ' . basename($directory);
114125
$commands[] = 'composer install --ansi';
115126
} else {
116-
$commands[] = 'composer create-project leafs/mvc ' . basename($directory) . ' --ansi';
127+
$commands[] = 'composer create-project leafs/mvc:v4.x-dev ' . basename($directory) . ' --ansi';
117128
$commands[] = 'cd ' . basename($directory);
118129
}
119130

@@ -134,7 +145,7 @@ protected function handle(): int
134145
if (\Leaf\FS\File::exists("$directory/vite.config.js")) {
135146
\Leaf\FS\File::delete("$directory/vite.config.js");
136147
}
137-
148+
138149
if (\Leaf\FS\File::exists("$directory/package.json")) {
139150
\Leaf\FS\File::delete("$directory/package.json");
140151
}
@@ -149,17 +160,84 @@ protected function handle(): int
149160
\Leaf\FS\Directory::copy(__DIR__ . '/themes/api/index.php', "$directory/public/index.php");
150161
}
151162

152-
$this->writeln("\n🚀 Successfully created project <info>" . basename($directory) . '</info>');
153-
$this->writeln('👉 Get started with the following commands:');
154-
$this->writeln("\n <info>cd</info> " . basename($directory));
155-
$this->writeln(' <info>leaf serve</info>');
163+
$this->writeln("\n🚀 Successfully created project <info>" . basename($directory) . "</info>\n");
156164

157-
// if ($testing) {
158-
// $this->writeln("\n👉 You can run tests with:");
159-
// $this->writeln("\n <info>leaf test</info>");
160-
// }
165+
$extraOptions = sprout()->prompt([
166+
[
167+
'type' => $this->projectType !== 'basic' ? 'confirm' : null,
168+
'name' => 'auth',
169+
'message' => 'Scaffold auth flow?',
170+
'default' => true,
171+
],
172+
[
173+
'type' => $this->projectType === 'mvc' ? 'select' : null,
174+
'name' => 'view',
175+
'message' => 'Select a view engine',
176+
'default' => 0,
177+
'choices' => [
178+
['title' => 'Default', 'value' => 'blade only'],
179+
['title' => 'Blade + Alpine', 'value' => '--tailwind'],
180+
['title' => 'React JS', 'value' => '--react --tailwind'],
181+
['title' => 'Vue JS', 'value' => '--vue --tailwind'],
182+
['title' => 'Svelte', 'value' => '--svelte --tailwind'],
183+
],
184+
],
185+
[
186+
'type' => 'confirm',
187+
'name' => 'tests',
188+
'message' => 'Set up tests?',
189+
'default' => true,
190+
],
191+
[
192+
'type' => 'confirm',
193+
'name' => 'docker',
194+
'message' => 'Set up docker?',
195+
'default' => true,
196+
],
197+
]);
198+
199+
$extraCommands = ['cd ' . basename($directory)];
200+
201+
if ($extraOptions['view'] ?? false) {
202+
$extraCommands[] = 'php leaf view:install ' . $extraOptions['view'];
203+
}
204+
205+
if ($extraOptions['auth'] ?? false) {
206+
$extraCommands[] = 'php leaf scaffold:auth' . ($this->projectType === 'api' ? ' --api' : '');
207+
}
161208

162-
$this->writeln("\n🍁 Happy gardening!");
209+
if ($extraOptions['tests'] ?? false) {
210+
$extraCommands[] = 'composer require --dev --ansi leafs/alchemy && ./vendor/bin/alchemy install --ansi';
211+
}
212+
213+
$this->write("\n");
214+
215+
if ($extraOptions['docker']) {
216+
\Leaf\FS\Directory::copy(__DIR__ . '/themes/docker', $directory, [
217+
'recursive' => true,
218+
]);
219+
}
220+
221+
if (sprout()->process(implode(' && ', $extraCommands))->setTimeout(null)->run() === 0) {
222+
$this->writeln("\n🚀 Application scaffolded successfully");
223+
$this->writeln('👉 Get started with the following commands:');
224+
$this->writeln("\n <info>cd</info> " . basename($directory));
225+
$this->writeln(' <info>leaf serve</info>');
226+
227+
if ($extraOptions['tests']) {
228+
$this->writeln("\n👉 You can run tests with:");
229+
$this->writeln("\n <info>leaf run test</info>");
230+
}
231+
232+
$this->writeln("\n🍁 Happy gardening!");
233+
} else {
234+
$this->writeln("\n❌ Could not scaffold extra options for <info>" . basename($directory) . '</info>');
235+
$this->writeln('👉 Get started with the following commands:');
236+
$this->writeln("\n <info>cd</info> " . basename($directory));
237+
$this->writeln(' <info>leaf serve</info>');
238+
239+
$this->writeln("\n🍁 Happy gardening!");
240+
}
163241
}
164242

165243
return 0;

0 commit comments

Comments
 (0)