Skip to content

Commit

Permalink
Centralize the definition of the manifet location path
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Feb 13, 2022
1 parent 7c16381 commit 3dc1c19
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
16 changes: 14 additions & 2 deletions config/importmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@

return [
/*
|----------------------------------------------------------------
|-------------------------------------------------------------------
| Whether to use the shim or not.
|----------------------------------------------------------------
|-------------------------------------------------------------------
|
| In some environments, such as when running browser testing, for instance,
| you may be running on a controller environment so the shim may slow down
| your tests, so you may prefer to not use the shim in such situations.
|
*/
'use_shim' => true,

/*
|------------------------------------------------------------------
| The path to the location where the manifest file will be created.
|------------------------------------------------------------------
|
| The manifest file will be used to store the optimized JSON file containing the import
| maps JSON map so we don't have to always generate it on the fly. That manifest is
| for internal usage only. It will be created by the `importmap:optimize` command.
|
*/
'manifest_location_path' => public_path('.importmap-manifest.json'),
];
5 changes: 3 additions & 2 deletions src/Commands/ClearCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Tonysm\ImportmapLaravel\Importmap;
use Tonysm\ImportmapLaravel\Manifest;

class ClearCacheCommand extends Command
{
Expand All @@ -16,11 +17,11 @@ public function handle(Importmap $importmap): int
{
$this->info('Clearing cached manifest...');

if (File::exists($manifest = $importmap->rootPath . '/public/importmap-manifest.json')) {
if (File::exists($manifest = Manifest::path())) {
File::delete($manifest);
}

$this->info('Done!');
$this->info('Manifest file cleared!');

return self::SUCCESS;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/OptimizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Str;
use Tonysm\ImportmapLaravel\FileDigest;
use Tonysm\ImportmapLaravel\Importmap;
use Tonysm\ImportmapLaravel\Manifest;

class OptimizeCommand extends Command
{
Expand Down Expand Up @@ -55,7 +56,7 @@ public function handle(Importmap $importmap): int
->values()
->all();

File::put($importmap->rootPath . '/public/.importmap-manifest.json', json_encode($optmizedJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
File::put(Manifest::path(), json_encode($optmizedJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

$this->info('Done!');
Expand Down
2 changes: 1 addition & 1 deletion src/Importmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function hasManifest(): bool

private function manifestPath(): string
{
return $this->rootPath . '/public/.importmap-manifest.json';
return Manifest::path();
}

private function resolvePreloadedModulesFromManifest(): array
Expand Down
16 changes: 16 additions & 0 deletions src/Manifest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tonysm\ImportmapLaravel;

class Manifest
{
public static function path(): string
{
return config('importmap.manifest_location_path');
}

public static function filename(): string
{
return basename(static::path());
}
}
5 changes: 4 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
use Illuminate\Support\Facades\File;
use Orchestra\Testbench\TestCase as Orchestra;
use Tonysm\ImportmapLaravel\ImportmapLaravelServiceProvider;
use Tonysm\ImportmapLaravel\Manifest;

class TestCase extends Orchestra
{
protected function setUp(): void
{
parent::setUp();

config()->set('importmap.manifest_location_path', __DIR__ . '/stubs/public/.importmap-manifest.json');

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Tonysm\\ImportmapLaravel\\Database\\Factories\\'.class_basename($modelName).'Factory'
);

if (File::exists($stubManifest = __DIR__ . '/stubs/public/.importmap-manifest.json')) {
if (File::exists($stubManifest = Manifest::path())) {
File::delete($stubManifest);
}
}
Expand Down

0 comments on commit 3dc1c19

Please sign in to comment.