Skip to content

Commit 477dfa9

Browse files
authored
feat(vite): mention <x-vite-tags /> and documentation link in post-install instructions (#1473)
1 parent a5b4212 commit 477dfa9

File tree

9 files changed

+97
-39
lines changed

9 files changed

+97
-39
lines changed

packages/support/src/JavaScript/DependencyInstaller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function installDependencies(string $cwd, string|array $dependencies, boo
4141
/**
4242
* Installs dependencies without interacting with the console.
4343
*/
44-
public function silentlyInstallDependencies(string $cwd, string|array $dependencies, bool $dev = false, ?PackageManager $defaultPackageManager = null): void
44+
public function silentlyInstallDependencies(string $cwd, string|array $dependencies, bool $dev = false, PackageManager $defaultPackageManager = PackageManager::NPM): void
4545
{
4646
$install = $this->getInstallProcess(
4747
packageManager: PackageManager::detect($cwd) ?? $defaultPackageManager,

packages/vite/src/Installer/ViteInstaller.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(
2929

3030
private function shouldInstallTailwind(): bool
3131
{
32-
$argument = $this->consoleArgumentBag->get('tailwind');
32+
$argument = $this->consoleArgumentBag->get('tailwindcss');
3333

3434
if ($argument === null || ! is_bool($argument->value)) {
3535
return $this->console->confirm('Install Tailwind CSS as well?', default: true);
@@ -42,8 +42,8 @@ public function install(): void
4242
{
4343
$shouldInstallTailwind = $this->shouldInstallTailwind();
4444
$templateDirectory = $shouldInstallTailwind
45-
? 'Tailwind'
46-
: 'Vanilla';
45+
? 'tailwindcss'
46+
: 'vanilla';
4747

4848
// Installs the dependencies
4949
$this->javascript->installDependencies(
@@ -63,7 +63,12 @@ public function install(): void
6363
? $this->publish(__DIR__ . "/{$templateDirectory}/main.css", destination: src_path('main.entrypoint.css'))
6464
: null;
6565

66-
// Install package.json scripts
66+
// Adds `package.json` if it does not exist
67+
if (! is_file(root_path('package.json'))) {
68+
$this->publish(__DIR__ . '/package.json', root_path('package.json'));
69+
}
70+
71+
// Installs package.json scripts
6772
$this->updateJson(root_path('package.json'), function (array $json) {
6873
$json['type'] = 'module';
6974
$json['scripts'] ??= [];
@@ -104,12 +109,13 @@ public function install(): void
104109
? '<strong>Vite and Tailwind CSS are now installed in your project</strong>!'
105110
: '<strong>Vite is now installed in your project</strong>!',
106111
PHP_EOL,
112+
'Add <code><x-vite-tags /></code> to the <code><head></code> of your template',
107113
"Run <code>{$packageManager->getRunCommand('dev')}</code> to start the <strong>development server</strong>",
108114
$mainTs || $mainCss
109115
? 'Update <code>*.entrypoint.{ts,css}</code> files and observe changes in your browser'
110116
: 'Create a <code>src/main.entrypoint.ts</code> file to get started with front-end code',
111117
PHP_EOL,
112-
// '<style="fg-green">→</style> Read the <href="https://tempestphp.com/main/framework/vite">documentation</href>', // TODO: update when we have Vite docs
118+
'<style="fg-green">→</style> Read the <href="https://tempestphp.com/docs/features/asset-bundling">documentation</href>',
113119
'<style="fg-green">→</style> Join the <href="https://tempestphp.com/discord">Discord server</href>',
114120
]);
115121
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

rector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Rector\Php82\Rector\Param\AddSensitiveParameterAttributeRector;
2020
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
2121
use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector;
22+
use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector;
2223
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector;
2324
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
2425
use Rector\TypeDeclaration\Rector\Closure\ClosureReturnTypeRector;
@@ -56,6 +57,7 @@
5657
ClosureReturnTypeRector::class,
5758
EncapsedStringsToSprintfRector::class,
5859
AddArrowFunctionReturnTypeRector::class,
60+
PrivatizeFinalClassMethodRector::class,
5961
])
6062
->withParallel(300, 10, 10)
6163
->withPreparedSets(

src/Tempest/Framework/Testing/InstallerTester.php

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
use RecursiveIteratorIterator;
1010
use Tempest\Container\Container;
1111
use Tempest\Core\Composer;
12-
use Tempest\Core\ComposerNamespace;
1312
use Tempest\Core\FrameworkKernel;
1413
use Tempest\Core\ShellExecutors\NullShellExecutor;
14+
use Tempest\Support\Arr;
15+
use Tempest\Support\Filesystem;
1516
use Tempest\Support\Namespace\Psr4Namespace;
1617

1718
use function Tempest\Support\arr;
@@ -39,13 +40,8 @@ public function configure(string $root, Psr4Namespace $namespace): self
3940
->setNamespaces($namespace)
4041
->setShellExecutor($this->executor);
4142

42-
if (! is_dir($this->root)) {
43-
mkdir($this->root, recursive: true);
44-
}
45-
46-
if (! is_dir($namespace->path)) {
47-
mkdir($namespace->path, recursive: true);
48-
}
43+
Filesystem\ensure_directory_exists($this->root);
44+
Filesystem\ensure_directory_exists($namespace->path);
4945

5046
return $this;
5147
}
@@ -66,20 +62,14 @@ public function put(string $path, string $contents): self
6662
{
6763
$path = $this->path($path);
6864

69-
$dir = dirname($path);
70-
71-
if (! is_dir($dir)) {
72-
mkdir($dir, recursive: true);
73-
}
74-
75-
file_put_contents($path, $contents);
65+
Filesystem\write_file($path, $contents);
7666

7767
return $this;
7868
}
7969

8070
public function get(string $path): string
8171
{
82-
return file_get_contents($this->path($path));
72+
return Filesystem\read_file($this->path($path));
8373
}
8474

8575
public function assertFileExists(string $path, ?string $content = null): self
@@ -96,17 +86,31 @@ public function assertFileExists(string $path, ?string $content = null): self
9686
return $this;
9787
}
9888

99-
public function assertFileContains(string $path, string $search): self
89+
public function assertDirectoryExists(string $path): self
10090
{
101-
Assert::assertStringContainsString(
102-
needle: $search,
103-
haystack: $this->get($path),
104-
message: sprintf("File %s does not contain:\n %s", $path, $search),
91+
Assert::assertDirectoryExists(
92+
directory: $this->path($path),
93+
message: sprintf('Directory "%s" does not exist', $path),
10594
);
10695

10796
return $this;
10897
}
10998

99+
public function assertFileContains(string $path, string|iterable $search): self
100+
{
101+
$content = $this->get($path);
102+
103+
foreach (Arr\wrap($search) as $item) {
104+
Assert::assertStringContainsString(
105+
needle: $item,
106+
haystack: $content,
107+
message: sprintf("File %s does not contain:\n %s", $path, $item),
108+
);
109+
}
110+
111+
return $this;
112+
}
113+
110114
public function assertFileNotContains(string $path, string $search): self
111115
{
112116
Assert::assertStringNotContainsString(
@@ -130,17 +134,6 @@ public function assertCommandExecuted(string $command): self
130134

131135
public function clean(): void
132136
{
133-
$files = new RecursiveIteratorIterator(
134-
new RecursiveDirectoryIterator($this->root, RecursiveDirectoryIterator::SKIP_DOTS),
135-
RecursiveIteratorIterator::CHILD_FIRST,
136-
);
137-
138-
foreach ($files as $file) {
139-
$file->isDir()
140-
? @rmdir($file->getRealPath())
141-
: @unlink($file->getRealPath());
142-
}
143-
144-
@rmdir($this->root);
137+
Filesystem\delete_directory($this->root);
145138
}
146139
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Integration\Vite;
4+
5+
use PHPUnit\Framework\Attributes\After;
6+
use PHPUnit\Framework\Attributes\PreCondition;
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Tempest\Core\Commands\InstallCommand;
9+
use Tempest\Support\JavaScript\DependencyInstaller;
10+
use Tempest\Support\Namespace\Psr4Namespace;
11+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
12+
13+
final class ViteInstallerTest extends FrameworkIntegrationTestCase
14+
{
15+
#[PreCondition]
16+
protected function configure(): void
17+
{
18+
$this->installer->configure(__DIR__ . '/install', new Psr4Namespace('App\\', __DIR__ . '/install/app'));
19+
20+
// force usage of npm because bun will mutate Tempest's root install otherwise
21+
touch(__DIR__ . '/install/package-lock.json');
22+
}
23+
24+
#[After]
25+
protected function after(): void
26+
{
27+
$this->installer->clean();
28+
}
29+
30+
#[Test]
31+
public function intalls_vite(): void
32+
{
33+
$this->console->call(InstallCommand::class, ['vite', '--force']);
34+
35+
$this->installer->assertFileExists('vite.config.ts');
36+
$this->installer->assertFileExists('app/main.entrypoint.ts');
37+
$this->installer->assertFileExists('app/main.entrypoint.css');
38+
39+
$this->installer->assertFileContains('package.json', '"vite"');
40+
$this->installer->assertFileContains('package.json', '"vite build"');
41+
}
42+
43+
#[Test]
44+
public function intalls_tailwindcss(): void
45+
{
46+
$this->console->call(InstallCommand::class, ['vite', 'tailwindcss', '--force']);
47+
48+
$this->installer->assertFileExists('app/main.entrypoint.ts');
49+
50+
$this->installer->assertFileContains('app/main.entrypoint.css', '@import');
51+
$this->installer->assertFileContains('package.json', ['"vite"', '"vite build"']);
52+
$this->installer->assertFileContains('vite.config.ts', 'vite-plugin-tempest');
53+
}
54+
}

0 commit comments

Comments
 (0)