Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a configuration option to disable emptying of the output directory #136

Merged
merged 6 commits into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ However, if you are a package developer, or if you have published Blade views or
- Moved `Hyde\Framework\Models\MarkdownPost` to new namespace `Hyde\Framework\Models\Pages\MarkdownPost`
- Moved `Hyde\Framework\Models\DocumentationPage` to new namespace `Hyde\Framework\Models\Pages\DocumentationPage`
- Improves how the site output directory is emptied, helping prevent accidental deletion of files https://github.com/hydephp/develop/pull/135
- The emptying of the site output directory can now be disabled by setting the new config option `hyde.empty_output_directory` to false https://github.com/hydephp/develop/pull/136

### Deprecated
- Deprecated Hyde::titleFromSlug(), use Hyde::makeTitle() instead
Expand Down
22 changes: 12 additions & 10 deletions packages/framework/src/Commands/HydeBuildStaticSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function handle(): int

$this->runPreBuildActions();

$this->purge();
$this->cleanOutputDirectory();

$this->transferMediaAssets();

Expand Down Expand Up @@ -171,18 +171,20 @@ protected function printFinishMessage(float $time_start): void
*
* @return void
*/
public function purge(): void
public function cleanOutputDirectory(): void
{
$this->warn('Removing all files from build directory.');
if (! in_array(basename(Hyde::getSiteOutputPath()), config('hyde.safe_output_directories', ['_site', 'docs', 'build']))) {
if (! $this->confirm('The configured output directory ('.Hyde::getSiteOutputPath().') is potentially unsafe to empty. Are you sure you want to continue?')) {
$this->info('Output directory will not be emptied.');

return;
if (config('hyde.empty_output_directory', true)) {
$this->warn('Removing all files from build directory.');
if (! in_array(basename(Hyde::getSiteOutputPath()), config('hyde.safe_output_directories', ['_site', 'docs', 'build']))) {
if (! $this->confirm('The configured output directory ('.Hyde::getSiteOutputPath().') is potentially unsafe to empty. Are you sure you want to continue?')) {
$this->info('Output directory will not be emptied.');

return;
}
}
array_map('unlink', glob(Hyde::getSiteOutputPath('*.{html,json}'), GLOB_BRACE));
File::cleanDirectory(Hyde::getSiteOutputPath('media'));
}
array_map('unlink', glob(Hyde::getSiteOutputPath('*.{html,json}'), GLOB_BRACE));
File::cleanDirectory(Hyde::getSiteOutputPath('media'));
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ public function test_print_initial_information_allows_api_to_be_disabled()
->assertExitCode(0);
}

public function test_site_directory_is_emptied_before_build()
{
touch(Hyde::path('_site/foo.html'));
$this->artisan('build')
->expectsOutput('Removing all files from build directory.')
->assertExitCode(0);
$this->assertFileDoesNotExist(Hyde::path('_site/foo.html'));
}

public function test_node_action_outputs()
{
$this->artisan('build --run-prettier --run-dev --run-prod')
Expand Down Expand Up @@ -155,6 +146,28 @@ public function test_generates_search_files_when_conditions_are_met()
->assertExitCode(0);
}

public function test_site_directory_is_emptied_before_build()
{
touch(Hyde::path('_site/foo.html'));
$this->artisan('build')
->expectsOutput('Removing all files from build directory.')
->assertExitCode(0);
$this->assertFileDoesNotExist(Hyde::path('_site/foo.html'));
}

public function test_output_directory_is_not_emptied_if_disabled_in_config()
{
config(['hyde.empty_output_directory' => false]);
touch(Hyde::path('_site/keep.html'));

$this->artisan('build')
->doesntExpectOutput('Removing all files from build directory.')
->assertExitCode(0);

$this->assertFileExists(Hyde::path('_site/keep.html'));
unlink(Hyde::path('_site/keep.html'));
}

public function test_aborts_when_non_standard_directory_is_emptied()
{
StaticPageBuilder::$outputPath = 'foo';
Expand Down