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

Improves how the site output directory is emptied, helping prevent accidental deletion of files #135

Merged
merged 8 commits into from
Jul 2, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ However, if you are a package developer, or if you have published Blade views or
- Moved `Hyde\Framework\Models\MarkdownPage` to new namespace `Hyde\Framework\Models\Pages\MarkdownPage`
- 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

### Deprecated
- Deprecated Hyde::titleFromSlug(), use Hyde::makeTitle() instead
Expand Down
10 changes: 9 additions & 1 deletion packages/framework/src/Commands/HydeBuildStaticSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,15 @@ protected function printFinishMessage(float $time_start): void
public function purge(): void
{
$this->warn('Removing all files from build directory.');
File::cleanDirectory(Hyde::getSiteOutputPath());
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'));
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Hyde\Framework\Testing\Feature\Commands;

use Hyde\Framework\Hyde;
use Hyde\Framework\StaticPageBuilder;
use Hyde\Testing\ResetsApplication;
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\File;

/**
* @covers \Hyde\Framework\Commands\HydeBuildStaticSiteCommand
Expand Down Expand Up @@ -152,4 +154,21 @@ public function test_generates_search_files_when_conditions_are_met()
->expectsOutput('Generating search page...')
->assertExitCode(0);
}

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

mkdir(Hyde::path('foo'));
touch(Hyde::path('foo/keep.html'));

$this->artisan('build')
->expectsOutput('Removing all files from build directory.')
->expectsQuestion('The configured output directory (foo) is potentially unsafe to empty. Are you sure you want to continue?', false)
->expectsOutput('Output directory will not be emptied.')
->assertExitCode(0);

$this->assertFileExists(Hyde::path('foo/keep.html'));
File::deleteDirectory(Hyde::path('foo'));
}
}