Skip to content

Commit 80d1a86

Browse files
committed
Don't create search files when there are no pages
Fix #482: Skip generating search.json and search.html if there are no documentation pages
1 parent 70d1dd9 commit 80d1a86

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/Commands/HydeBuildStaticSiteCommand.php

+24-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Hyde\Framework\Models\DocumentationPage;
1313
use Hyde\Framework\Models\MarkdownPage;
1414
use Hyde\Framework\Models\MarkdownPost;
15+
use Hyde\Framework\Services\CollectionService;
1516
use Hyde\Framework\Services\DiscoveryService;
1617
use Hyde\Framework\Services\RssFeedService;
1718
use Hyde\Framework\Services\SitemapService;
@@ -136,15 +137,15 @@ public function runPostBuildActions(): void
136137
$this->runNodeCommand('npm run prod', 'Building frontend assets for production!');
137138
}
138139

139-
if (SitemapService::canGenerateSitemap()) {
140+
if ($this->canGenerateSitemap()) {
140141
Artisan::call('build:sitemap', outputBuffer: $this->output);
141142
}
142143

143-
if (RssFeedService::canGenerateFeed()) {
144+
if ($this->canGenerateFeed()) {
144145
Artisan::call('build:rss', outputBuffer: $this->output);
145146
}
146147

147-
if (Features::hasDocumentationSearch()) {
148+
if ($this->canGenerateSearch()) {
148149
Artisan::call('build:search', outputBuffer: $this->output);
149150
}
150151
}
@@ -155,14 +156,14 @@ protected function printFinishMessage(float $time_start): void
155156
$time_end = microtime(true);
156157
$execution_time = ($time_end - $time_start);
157158
$this->info('All done! Finished in '.number_format(
158-
$execution_time,
159-
2
160-
).' seconds. ('.number_format(($execution_time * 1000), 2).'ms)');
159+
$execution_time,
160+
2
161+
).' seconds. ('.number_format(($execution_time * 1000), 2).'ms)');
161162

162163
$this->info('Congratulations! 🎉 Your static site has been built!');
163164
$this->line(
164165
'Your new homepage is stored here -> '.
165-
DiscoveryService::createClickableFilepath(Hyde::getSiteOutputPath('index.html'))
166+
DiscoveryService::createClickableFilepath(Hyde::getSiteOutputPath('index.html'))
166167
);
167168
}
168169

@@ -206,4 +207,20 @@ private function runNodeCommand(string $command, string $message, ?string $actio
206207
$output ?? '<fg=red>Could not '.($actionMessage ?? 'run script').'! Is NPM installed?</>'
207208
);
208209
}
210+
211+
protected function canGenerateSitemap(): bool
212+
{
213+
return SitemapService::canGenerateSitemap();
214+
}
215+
216+
protected function canGenerateFeed(): bool
217+
{
218+
return RssFeedService::canGenerateFeed();
219+
}
220+
221+
protected function canGenerateSearch(): bool
222+
{
223+
return Features::hasDocumentationSearch()
224+
&& count(CollectionService::getDocumentationPageList()) > 0;
225+
}
209226
}

tests/Feature/Commands/BuildStaticSiteCommandTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ public function test_rss_feed_is_generated_when_conditions_are_met()
136136
unlink(Hyde::path('_site/feed.xml'));
137137
}
138138

139+
public function test_does_not_generate_search_files_when_conditions_are_not_met()
140+
{
141+
$this->artisan('build')
142+
->doesntExpectOutput('Generating documentation site search index...')
143+
->doesntExpectOutput('Generating search page...')
144+
->assertExitCode(0);
145+
}
146+
147+
public function test_generates_search_files_when_conditions_are_met()
148+
{
149+
touch(Hyde::path('_docs/foo.md'));
150+
151+
$this->artisan('build')
152+
->expectsOutput('Generating documentation site search index...')
153+
->expectsOutput('Generating search page...')
154+
->assertExitCode(0);
155+
}
156+
139157
/**
140158
* Added for code coverage, deprecated as the pretty flag is deprecated.
141159
*

0 commit comments

Comments
 (0)