diff --git a/src/Console/Commands/StackCommand.php b/src/Console/Commands/StackCommand.php index 60d8eff..736d821 100644 --- a/src/Console/Commands/StackCommand.php +++ b/src/Console/Commands/StackCommand.php @@ -119,6 +119,7 @@ private function runDevMode(string $framework): int $this->writeProjectJson($framework); $this->skipGeneratedFiles($framework); + $this->writeCoreTsconfig($framework); if (! $this->option('no-install')) { $this->runNpmInstall(); @@ -188,6 +189,11 @@ private function runReset(): int $this->files->delete($lockfile); } + $this->files->delete($this->coreTsconfigPath($framework)); + if (is_link($this->corePackagesLink($framework))) { + unlink($this->corePackagesLink($framework)); + } + if ($kept !== []) { $this->files->put( $this->basePath.'/saucebase.json', @@ -301,6 +307,68 @@ private function copyConfigFiles(string $framework): void } } + /** + * A symlinked core's components live outside the app, where no tsconfig reaches them: + * Storybook's docgen and editors find a file's tsconfig by walking up from its real + * path. This one, beside the stack's code, extends the app's and points the aliases + * back at the app; a node_modules link beside it resolves packages as from inside the + * app, with their `exports` and type references (`vite/client`). Core git-ignores + * both: the paths are this machine's. + */ + private function writeCoreTsconfig(string $framework): void + { + if (! is_link($this->basePath.'/vendor/saucebase/core')) { + return; + } + + $app = (string) realpath($this->basePath); + $appConfig = json_decode($this->files->get($app.'/tsconfig.json'), true); + + // `paths` replaces the extended config's paths rather than merging, so all of them, + // made absolute. + $paths = array_map( + fn (array $targets) => array_map(fn (string $target) => $app.'/'.preg_replace('#^\./#', '', $target), $targets), + $appConfig['compilerOptions']['paths'] ?? [], + ); + + $config = [ + 'extends' => $app.'/tsconfig.json', + 'compilerOptions' => [ + 'paths' => $paths, + // The files are read at their real path; following the vendor/ link back + // would load each one twice. + 'preserveSymlinks' => false, + ], + 'include' => ['**/*.ts', $framework === 'react' ? '**/*.tsx' : '**/*.vue'], + ]; + + $this->files->ensureDirectoryExists(dirname($this->coreTsconfigPath($framework))); + $this->files->put( + $this->coreTsconfigPath($framework), + json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES).PHP_EOL, + ); + + // Another workspace on the same core may have linked its own: follow this app. A + // real folder there is someone's own install, left alone. + $link = $this->corePackagesLink($framework); + if (is_link($link) && readlink($link) !== $app.'/node_modules') { + unlink($link); + } + if (! file_exists($link) && ! is_link($link)) { + symlink($app.'/node_modules', $link); + } + } + + private function coreTsconfigPath(string $framework): string + { + return $this->stubDir($framework).'/js/tsconfig.json'; + } + + private function corePackagesLink(string $framework): string + { + return $this->stubDir($framework).'/js/node_modules'; + } + private function copyLockFile(string $framework): void { $source = $this->stubDir($framework).'/config/'.self::LOCKFILE; diff --git a/tests/Feature/StackCommandTest.php b/tests/Feature/StackCommandTest.php index 681776b..b4ca4c2 100644 --- a/tests/Feature/StackCommandTest.php +++ b/tests/Feature/StackCommandTest.php @@ -53,6 +53,7 @@ protected function runNpmInstall(): void {} protected function tearDown(): void { $this->files->deleteDirectory($this->tmpDir); + $this->files->deleteDirectory($this->tmpDir.'-core'); parent::tearDown(); } @@ -298,6 +299,90 @@ public function test_no_install_option_skips_npm_install(): void } /** Bind a StackCommand that counts runNpmInstall() calls instead of shelling out. */ + // ------------------------------------------------------------------------- + // Contributor checkout: core symlinked + // ------------------------------------------------------------------------- + + /** Storybook's docgen and editors find core's components by their real path: they need a tsconfig there. */ + public function test_dev_mode_gives_a_symlinked_core_a_tsconfig_that_extends_the_app(): void + { + $core = $this->linkCore(); + $this->seedFakeStubs('vue'); + + $this->artisan('stack vue --dev')->assertSuccessful(); + + $app = realpath($this->tmpDir); + $config = json_decode(file_get_contents($core.'/stubs/stack/vue/js/tsconfig.json'), true); + $this->assertSame($app.'/tsconfig.json', $config['extends']); + $this->assertSame([$app.'/vendor/saucebase/core/stubs/stack/vue/js/*'], $config['compilerOptions']['paths']['@/*']); + $this->assertSame(['**/*.ts', '**/*.vue'], $config['include']); + } + + /** + * Packages resolve from core's real path through a link to the app's node_modules, as + * from inside the app: a `*` path mapping would skip packages' `exports` + * (`storybook/test`) and never reach type references (`vite/client`). + */ + public function test_dev_mode_links_a_symlinked_core_to_the_app_packages(): void + { + $core = $this->linkCore(); + $this->seedFakeStubs('vue'); + + $this->artisan('stack vue --dev')->assertSuccessful(); + + $link = $core.'/stubs/stack/vue/js/node_modules'; + $this->assertTrue(is_link($link)); + $this->assertSame(realpath($this->tmpDir).'/node_modules', readlink($link)); + $config = json_decode(file_get_contents($core.'/stubs/stack/vue/js/tsconfig.json'), true); + $this->assertArrayNotHasKey('*', $config['compilerOptions']['paths']); + } + + /** Another workspace on the same core (a template's app) ran it last: follow this app. */ + public function test_dev_mode_repoints_a_package_link_left_by_another_workspace(): void + { + $core = $this->linkCore(); + $this->seedFakeStubs('vue'); + $this->files->ensureDirectoryExists($core.'/stubs/stack/vue/js'); + symlink($this->tmpDir.'-elsewhere/node_modules', $core.'/stubs/stack/vue/js/node_modules'); + + $this->artisan('stack vue --dev')->assertSuccessful(); + + $this->assertSame(realpath($this->tmpDir).'/node_modules', readlink($core.'/stubs/stack/vue/js/node_modules')); + } + + public function test_dev_mode_react_includes_tsx_in_the_core_tsconfig(): void + { + $core = $this->linkCore(); + $this->seedFakeStubs('react'); + + $this->artisan('stack react --dev')->assertSuccessful(); + + $config = json_decode(file_get_contents($core.'/stubs/stack/react/js/tsconfig.json'), true); + $this->assertSame(['**/*.ts', '**/*.tsx'], $config['include']); + } + + /** Core from Packagist sits inside the app, where the app's tsconfig already reaches. */ + public function test_dev_mode_leaves_a_core_inside_vendor_alone(): void + { + $this->seedFakeStubs('vue'); + + $this->artisan('stack vue --dev')->assertSuccessful(); + + $this->assertFileDoesNotExist($this->tmpDir.'/vendor/saucebase/core/stubs/stack/vue/js/tsconfig.json'); + } + + public function test_reset_removes_the_core_tsconfig(): void + { + $core = $this->linkCore(); + $this->seedFakeStubs('vue'); + $this->artisan('stack vue --dev')->assertSuccessful(); + + $this->artisan('stack --reset')->assertSuccessful(); + + $this->assertFileDoesNotExist($core.'/stubs/stack/vue/js/tsconfig.json'); + $this->assertFalse(is_link($core.'/stubs/stack/vue/js/node_modules')); + } + private function bindNpmSpy(): StackCommand { $tmpDir = $this->tmpDir; @@ -616,6 +701,17 @@ public function test_a_plain_checkout_is_not_told_to_restore_stubs(): void // Helpers // ------------------------------------------------------------------------- + /** Core as a contributor has it: a clone outside the app, linked into vendor/. */ + private function linkCore(): string + { + $core = $this->tmpDir.'-core'; + $this->files->makeDirectory($core, 0755, true); + $this->files->deleteDirectory($this->tmpDir.'/vendor/saucebase/core'); + symlink($core, $this->tmpDir.'/vendor/saucebase/core'); + + return $core; + } + private function seedFakeTemplate(): void { file_put_contents($this->tmpDir.'/saucebase.json', json_encode(['framework' => null, 'template' => 'uptime']).PHP_EOL);