Skip to content

Commit af9a579

Browse files
authored
Added Svelte installation option to ViewInstallCommand.php
1 parent 8114bde commit af9a579

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

src/ViewInstallCommand.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ protected function configure()
2323
->addOption('bare-ui', null, InputOption::VALUE_NONE, 'Install bare ui')
2424
->addOption('inertia', null, InputOption::VALUE_NONE, 'Setup inertia files')
2525
->addOption('react', null, InputOption::VALUE_NONE, 'Install react')
26+
->addOption('svelte', null, InputOption::VALUE_NONE, 'Install svelte')
2627
->addOption('tailwind', null, InputOption::VALUE_NONE, 'Install tailwind')
2728
->addOption('vite', null, InputOption::VALUE_NONE, 'Setup vite files')
2829
->addOption('vue', null, InputOption::VALUE_NONE, 'Install vue')
@@ -47,6 +48,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4748
return $this->installReact($input, $output);
4849
}
4950

51+
if ($input->getOption('svelte')) {
52+
return $this->installSvelte($input, $output);
53+
}
54+
5055
if ($input->getOption('tailwind')) {
5156
return $this->installTailwind($input, $output);
5257
}
@@ -320,6 +325,97 @@ protected function installReact($input, $output)
320325
return 0;
321326
}
322327

328+
/**
329+
* Install svelte
330+
*/
331+
protected function installSvelte($input, $output)
332+
{
333+
$output->writeln("📦 <info>Installing svelte...</info>\n");
334+
335+
$directory = getcwd();
336+
$npm = Utils\Core::findNpm($input->getOption('pm'));
337+
$composer = Utils\Core::findComposer();
338+
$success = Utils\Core::run("$npm add @leafphp/vite-plugin svelte @sveltejs/vite-plugin-svelte @inertiajs/svelte", $output);
339+
340+
if (!$success) {
341+
$output->writeln("❌ <error>Failed to install svelte</error>");
342+
return 1;
343+
};
344+
345+
$output->writeln("\n✅ <info>Svelte installed successfully</info>");
346+
$output->writeln("🧱 <info>Setting up Leaf Svelte server bridge...</info>\n");
347+
348+
$success = Utils\Core::run("$composer require leafs/inertia leafs/vite", $output);
349+
350+
if (!$success) {
351+
$output->writeln("❌ <error>Failed to setup Leaf Svelte server bridge</error>");
352+
return 1;
353+
};
354+
355+
$isMVCApp = $this->isMVCApp();
356+
$isBladeProject = $this->isBladeProject();
357+
$ext = $isBladeProject ? 'blade' : 'view';
358+
359+
if (!$isBladeProject) {
360+
$output->writeln("\n🎨 <info>Setting up BareUI as main view engine.</info>\n");
361+
$success = Utils\Core::run("$composer require leafs/bareui", $output);
362+
363+
if (!$success) {
364+
$output->writeln("❌ <error>Could not install BareUI, run leaf install bareui</error>\n");
365+
return 1;
366+
};
367+
}
368+
369+
\Leaf\FS::superCopy(__DIR__ . '/themes/svelte/root', $directory);
370+
371+
if ($isMVCApp) {
372+
$paths = require "$directory/config/paths.php";
373+
$viewsPath = trim($paths['views'] ?? 'app/views', '/');
374+
$routesPath = trim($paths['routes'] ?? 'app/routes', '/');
375+
376+
\Leaf\FS::superCopy(__DIR__ . '/themes/svelte/routes', "$directory/$routesPath");
377+
\Leaf\FS::superCopy(
378+
(__DIR__ . '/themes/svelte/views/' . ($isBladeProject ? 'blade' : 'bare-ui')),
379+
"$directory/$viewsPath"
380+
);
381+
} else {
382+
\Leaf\FS::superCopy(__DIR__ . '/themes/svelte/routes', $directory);
383+
\Leaf\FS::superCopy(
384+
(__DIR__ . '/themes/svelte/views/' . ($isBladeProject ? 'blade' : 'bare-ui')),
385+
$directory
386+
);
387+
388+
$viteConfig = file_get_contents("$directory/vite.config.js");
389+
$viteConfig = str_replace(
390+
"leaf({",
391+
"leaf({\nhotFile: 'hot',",
392+
$viteConfig
393+
);
394+
file_put_contents("$directory/vite.config.js", $viteConfig);
395+
396+
$inertiaView = file_get_contents("$directory/_inertia.$ext.php");
397+
$inertiaView = str_replace(
398+
'<?php echo vite([\'/js/app.jsx\', "/js/Pages/{$page[\'component\']}.jsx"]); ?>',
399+
'<?php echo vite([\'js/app.js\'], \'/\'); ?>',
400+
$inertiaView
401+
);
402+
file_put_contents("$directory/_inertia.$ext.php", $inertiaView);
403+
}
404+
405+
$package = json_decode(file_get_contents("$directory/package.json"), true);
406+
$package['type'] = 'module';
407+
$package['scripts']['dev'] = 'vite';
408+
$package['scripts']['build'] = 'vite build';
409+
file_put_contents("$directory/package.json", json_encode($package, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
410+
411+
$output->writeln("\n⚛️ <info>Svelte setup successfully</info>");
412+
$output->writeln("👉 Get started with the following commands:\n");
413+
$output->writeln(' leaf view:dev <info>- start dev server</info>');
414+
$output->writeln(" leaf view:build <info>- build for production</info>");
415+
416+
return 0;
417+
}
418+
323419
/**
324420
* Install tailwind
325421
*/

0 commit comments

Comments
 (0)