Skip to content

Commit

Permalink
Merge pull request #40 from fly-apps/remove_node_installation_if_node…
Browse files Browse the repository at this point in the history
…_config_not_available

Remove asset build stage when no package.json exists
  • Loading branch information
KTanAug21 authored May 27, 2024
2 parents e7e7285 + d03be4f commit 1f642e5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
3 changes: 2 additions & 1 deletion app/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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 );
Expand Down
23 changes: 22 additions & 1 deletion app/Services/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
17 changes: 17 additions & 0 deletions app/Services/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified builds/dockerfile-laravel
Binary file not shown.
7 changes: 6 additions & 1 deletion tests/Feature/GenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function ignoreFiles( )
{
return ['composer.json','frankenphp','rr','.rr.yaml'];
return ['composer.json','frankenphp','rr','.rr.yaml','package.json'];
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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' );
}
});

0 comments on commit 1f642e5

Please sign in to comment.