diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9acdbc0..1a201d6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,10 +11,6 @@ permissions: jobs: release: uses: saucebase-dev/saucebase/.github/workflows/semantic-release.yml@main - with: - version_file: "module.json" - version_file_path: ".version" - prerelease: false secrets: APP_ID: ${{ secrets.APP_ID }} APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }} diff --git a/CLAUDE.md b/CLAUDE.md index 9c2865c..84c5f9d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -26,7 +26,7 @@ The Saucebase app (`saucebase/composer.json`) uses `module-type: saucebase-modul ## Core Behaviours -**Install** (`install()`): Downloads the package, removes excluded directories, and deploys frontend framework files if `frontend.json` is present. Skips entirely for path-repository packages. +**Install** (`install()`): Downloads the package, removes excluded directories, and deploys frontend framework files if `frontend.json` is present. Skips entirely for path-repository packages, locally tracked dirs, and any install path that already contains files (copy-and-own: a committed `modules/` dir must survive `composer install` on a fresh deploy). **Update** (`update()`): Two strategies: - `merge` (default) — stashes the user's copy, downloads the new version, downloads the original version as a base, runs a 3-way `git merge-file`. Conflicts are staged in the git index (stages 1/2/3) so they appear in `git status`. diff --git a/src/Installer.php b/src/Installer.php index 9dba716..50269dc 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -378,6 +378,15 @@ protected function isLocallyTracked(string $path): bool return is_link($path) || is_dir($path.'/.git'); } + /** + * Returns true when the install path already holds module files. An existing but empty + * directory does not count — that is a leftover, not an installed module. + */ + protected function hasModuleFiles(string $path): bool + { + return is_dir($path) && (new \FilesystemIterator($path))->valid(); + } + /** * Returns true when a package has dist.type=path but its install path is NOT locally * tracked (no symlink, no .git). This identifies a Packagist-installed module that was @@ -479,6 +488,36 @@ protected function copyFrameworkFiles(PackageInterface $package): void } $this->flattenFrameworkFiles($jsRoot, $framework); + $this->rewriteCrossModuleImports($jsRoot); + } + + /** + * Strips any framework subdirectory segment from cross-module import paths in JS/TS/Vue files. + * After flattening, imports like @modules/billing/resources/js/vue/Foo.vue become + * + * @modules/billing/resources/js/Foo.vue regardless of which framework is active. + */ + protected function rewriteCrossModuleImports(string $jsRoot): void + { + $extensions = ['vue', 'ts', 'tsx', 'js']; + $fwPattern = implode('|', array_map('preg_quote', self::KNOWN_FRAMEWORKS)); + + foreach ((new Finder)->files()->in($jsRoot) as $file) { + if (! in_array($file->getExtension(), $extensions, true)) { + continue; + } + + $content = file_get_contents($file->getPathname()); + $rewritten = preg_replace( + "#(@modules/[^/]+/resources/js/)({$fwPattern})/#", + '$1', + $content + ); + + if ($rewritten !== $content) { + file_put_contents($file->getPathname(), $rewritten); + } + } } /** @@ -571,15 +610,13 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa return \React\Promise\resolve(null); } - // Module is already installed (re-resolved by modules/* path repo on a repeat run). - // Files are in place — just register and skip the download. - // Note: if dist.type=path but the dir is absent (fresh clone from a polluted lock), - // this guard does not fire and parentInstall will fail. Fix the lock with composer update. - if ($package->getDistType() === 'path' - && ! $this->isLocallyTracked($this->getInstallPath($package)) - && is_dir($this->getInstallPath($package)) - ) { - $this->io->write(" - Skipping install (module already present, resolved as path by modules/* repo): {$package->getPrettyName()}"); + // Module files are already on disk — copy-and-own means the working tree wins. + // Covers a repeat run re-resolved by the modules/* path repo, and a production deploy + // where modules/ is committed to the app repo but vendor/ is empty, so Composer treats + // every module as a fresh install and the download would clobber the user's edits. + // Version bumps still land: those are update() operations, which merge. + if ($this->hasModuleFiles($this->getInstallPath($package))) { + $this->io->write(" - Skipping install (module files already present): {$package->getPrettyName()}"); if (! $repo->hasPackage($package)) { $repo->addPackage(clone $package); diff --git a/tests/ModuleInstallerTest.php b/tests/ModuleInstallerTest.php index a6675be..dc2951e 100644 --- a/tests/ModuleInstallerTest.php +++ b/tests/ModuleInstallerTest.php @@ -1264,6 +1264,7 @@ public function test_install_skips_download_when_module_dir_exists_with_path_dis { $baseDir = sys_get_temp_dir().'/install-path-exists-'.uniqid('', true); mkdir($baseDir.'/test-module', 0755, true); + file_put_contents($baseDir.'/test-module/composer.json', '{}'); $installer = $this->makeInstallerWithModuleDir($baseDir); @@ -1305,6 +1306,55 @@ public function test_install_falls_through_to_parent_when_module_dir_absent_with (new Filesystem)->remove($baseDir); } + public function test_install_does_not_overwrite_existing_module_dir_from_dist(): void + { + // Production deploy: app repo carries modules/ in git, vendor/ is empty, so composer + // treats every module as a fresh install. Without a guard the dist download would + // restore files the user deleted locally and clobber their edits. + $baseDir = sys_get_temp_dir().'/install-dist-exists-'.uniqid('', true); + mkdir($baseDir.'/test-module', 0755, true); + file_put_contents($baseDir.'/test-module/composer.json', '{}'); + + $installer = $this->makeInstallerWithModuleDir($baseDir); + + $pkg = new Package('saucebase/test-module', '1.0.0.0', '1.0.0'); + $pkg->setDistType('zip'); + + $repo = $this->createMock(InstalledRepositoryInterface::class); + $repo->method('hasPackage')->willReturn(false); + $repo->expects($this->once())->method('addPackage'); + + $resolved = false; + $installer->install($repo, $pkg)->then(function () use (&$resolved) { + $resolved = true; + }); + + $this->assertTrue($resolved); + $this->assertFalse($installer->parentInstallInvoked, 'parentInstall must not run over an existing module dir'); + + (new Filesystem)->remove($baseDir); + } + + public function test_install_downloads_when_existing_module_dir_is_empty(): void + { + $baseDir = sys_get_temp_dir().'/install-dist-empty-'.uniqid('', true); + mkdir($baseDir.'/test-module', 0755, true); + + $installer = $this->makeInstallerWithModuleDir($baseDir); + + $pkg = new Package('saucebase/test-module', '1.0.0.0', '1.0.0'); + $pkg->setDistType('zip'); + + $repo = $this->createStub(InstalledRepositoryInterface::class); + $repo->method('hasPackage')->willReturn(false); + + $installer->install($repo, $pkg); + + $this->assertTrue($installer->parentInstallInvoked, 'an empty leftover dir must not block the download'); + + (new Filesystem)->remove($baseDir); + } + // ------------------------------------------------------------------------- // getSelectedFramework // -------------------------------------------------------------------------