diff --git a/README.md b/README.md index 2af5bd1..ec437cd 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ And that should generate a Dockerfile at the current directory the command was r * `--force` - Overwrite existing files * `--skip` - Keep existing files * `--dev` - Include dev dependencies in the creation of the image, i.e. the local .env file + * `--frankenphp-binary` - Generate a single file binary of the app via frankenphp The dockerfile generator accepts the following options: diff --git a/app/Commands/GenerateCommand.php b/app/Commands/GenerateCommand.php index 7de0626..1630668 100644 --- a/app/Commands/GenerateCommand.php +++ b/app/Commands/GenerateCommand.php @@ -49,7 +49,7 @@ public function handle() // Define the options available to the templates. $options = [ - 'build_assets' => ! $this->option('no-assets'), + 'build_assets' => $scan->shouldBuildAssets( $this->options() ), 'dev' => $this->option('dev'), 'laravel_version' => $scan->laravelVersion( $this->options() ), 'fly' => $scan->isForFly(), @@ -58,6 +58,7 @@ public function handle() 'frankenphp_binary' => $this->option('frankenphp-binary') ]; + // Define the list of templates to render. // The key is the template name, and the value is the output file name. $templates = $scan->templates( $options ); diff --git a/app/Services/File.php b/app/Services/File.php index 8d88285..dc039a6 100644 --- a/app/Services/File.php +++ b/app/Services/File.php @@ -26,7 +26,28 @@ public function createFile( $output, $result ) return file_put_contents($output, implode("\n", $result) . "\n"); } - public function composerJsonContent( $directory ) + /** + * Check if package.json exists in the directory + * + * @param string $directory - string used as directory where the package.json file is scanned in + */ + public function packageJsonExists( string $directory ) + { + $path = $directory.'/package.json'; + + if( file_exists( $path ) ) + return true; + else{ + return false; + } + } + + /** + * Get array of key value pairs from a composer.json file + * + * @param string $directory - string used as directory where the composer.json file is scanned in + */ + public function composerJsonContent( string $directory ) { $path = $directory.'/composer.json'; diff --git a/app/Services/Scanner.php b/app/Services/Scanner.php index 878ef98..b93e55f 100644 --- a/app/Services/Scanner.php +++ b/app/Services/Scanner.php @@ -82,6 +82,23 @@ public function isForFly() return false; } + /** + * Determines whether to build assets or not + */ + public function shouldBuildAssets( array $options ) + { + $shouldBuild = !$options['no-assets']; + $packageJsonExists = (new \App\Services\File())->packageJsonExists( $options['path'] ); + + if( $shouldBuild && $packageJsonExists ) { + // If want to build assets, make sure package.json exists + return true; + }else{ + // Otherwise don't build + return false; + } + } + /** * Lists templates to generate based on options passed diff --git a/builds/dockerfile-laravel b/builds/dockerfile-laravel index 4342f56..ba5eaac 100755 Binary files a/builds/dockerfile-laravel and b/builds/dockerfile-laravel differ diff --git a/tests/Feature/GenerateCommandTest.php b/tests/Feature/GenerateCommandTest.php index 6bcb3e9..84d68ea 100644 --- a/tests/Feature/GenerateCommandTest.php +++ b/tests/Feature/GenerateCommandTest.php @@ -2,7 +2,7 @@ function ignoreFiles( ) { - return ['composer.json','frankenphp','rr','.rr.yaml']; + return ['composer.json','frankenphp','rr','.rr.yaml','package.json']; } /** @@ -69,6 +69,8 @@ function setFlags( $extCompArr, $pathToFiles ): string $directories = \File::directories( 'tests/Feature/Supported' ); foreach($directories as $dir) { #if( $dir != "tests/Feature/Supported/10_base" ) continue;//-- revise and uncomment this line if you want to test out a specific Support subfolder + // package.json is needed to generate a Dockerfile with asset build stage, will be deleted in second test below + file_put_contents( $dir.'/package.json','{}' ); // Generate Dockerfile, by scanning contents of files in the current directory, set through --path // FIRST assert: command successfully runs and exits @@ -186,6 +188,9 @@ function setFlags( $extCompArr, $pathToFiles ): string $fh->deleteDir('tests/Feature/Combination'); } } + + // Delete the generated package.json file from "generates proper templates for each supported base" + unlink( $base.'/package.json' ); } });