Skip to content

Commit fee1230

Browse files
authored
feat(http)!: add --crawl flag to static:generate command (#1656)
1 parent 1dab1c7 commit fee1230

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

docs/2-features/13-static-pages.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ The only thing left to do is to generate the static pages:
111111
<comment>…</comment>
112112
```
113113

114+
## Crawling for dead links
115+
116+
Optionally, you can instruct the static generate to crawl your pages to scan for dead links. This is done by passing the `--crawl` option to the `static:generate` command:
117+
118+
```console
119+
<dim>./tempest static:generate --crawl</dim>
120+
```
121+
122+
By default, the crawler will only check for internal dead links. If you want to check for external links as well, you can pass the `--external` option:
123+
124+
```console
125+
<dim>./tempest static:generate --crawl --external</dim>
126+
```
127+
114128
## Production
115129

116130
Static pages are generated in the `/public` directory, as `index.html` files. Most web servers will automatically serve these static pages for you without any additional setup.

packages/router/src/Static/StaticGenerateCommand.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ public function __construct(
6060
#[ConsoleCommand(name: 'static:generate', description: 'Compiles static pages')]
6161
public function __invoke(
6262
?string $filter = null,
63-
bool $allowDeadLinks = false,
64-
bool $allowExternalDeadLinks = true,
63+
#[ConsoleArgument(description: 'Crawl the site for dead links')]
64+
bool $crawl = false,
65+
#[ConsoleArgument(description: 'Crawl external links, only works when the --crawl flag is set')]
66+
bool $external = false,
6567
#[ConsoleArgument(aliases: ['v'])]
6668
bool $verbose = false,
6769
): ExitCode {
@@ -150,9 +152,13 @@ public function __invoke(
150152
mkdir($directory->toString(), recursive: true);
151153
}
152154

153-
if (! $allowDeadLinks && count($links = $this->detectDeadLinks($uri, $content, checkExternal: ! $allowExternalDeadLinks)) > 0) {
154-
$deadlinks[$uri] = $links;
155-
throw new DeadLinksDetectedException($uri, $links);
155+
if ($crawl) {
156+
$deadLinks = $this->detectDeadLinks($uri, $content, checkExternal: $external);
157+
158+
if ($deadLinks !== []) {
159+
$deadlinks[$uri] = $deadLinks;
160+
throw new DeadLinksDetectedException($uri, $deadLinks);
161+
}
156162
}
157163

158164
Filesystem\write_file($file->toString(), $content);
@@ -184,8 +190,8 @@ public function __invoke(
184190
if ($deadlinks) {
185191
$this->console->header('Dead links');
186192

187-
foreach ($deadlinks as $uri => $links) {
188-
foreach ($links as $link) {
193+
foreach ($deadlinks as $uri => $deadLinks) {
194+
foreach ($deadLinks as $link) {
189195
$this->keyValue("<style='fg-gray'>{$uri}</style>", "<style='fg-red'>{$link}</style>");
190196
}
191197
}

tests/Integration/Http/Static/StaticGenerateCommandTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function test_dead_link(): void
9797
$this->container->config(new AppConfig(baseUri: 'https://test.com'));
9898

9999
$this->console
100-
->call(StaticGenerateCommand::class)
100+
->call(StaticGenerateCommand::class, ['--crawl' => true])
101101
->assertSee('1 DEAD LINK')
102102
->assertSee('https://test.com/404')
103103
->assertExitCode(ExitCode::ERROR);
@@ -112,7 +112,7 @@ public function test_dead_link_with_redirect(): void
112112
$this->container->config(new AppConfig(baseUri: 'https://test.com'));
113113

114114
$this->console
115-
->call(StaticGenerateCommand::class, ['--allow-external-dead-links' => false])
115+
->call(StaticGenerateCommand::class, ['--crawl' => true])
116116
->assertExitCode(ExitCode::SUCCESS);
117117
}
118118

@@ -124,7 +124,7 @@ public function test_allow_dead_links(): void
124124
$this->container->config(new AppConfig(baseUri: 'https://test.com'));
125125

126126
$this->console
127-
->call(StaticGenerateCommand::class, ['--allow-dead-links' => true])
127+
->call(StaticGenerateCommand::class, ['--crawl' => false])
128128
->assertExitCode(ExitCode::SUCCESS);
129129
}
130130

@@ -136,7 +136,7 @@ public function test_external_dead_links(): void
136136
$this->container->config(new AppConfig(baseUri: 'https://test.com'));
137137

138138
$this->console
139-
->call(StaticGenerateCommand::class, ['--allow-external-dead-links' => false])
139+
->call(StaticGenerateCommand::class, ['--crawl' => true, '--external' => true])
140140
->assertSee('2 DEAD LINKS')
141141
->assertSee('https://test.com/404')
142142
->assertSee('https://google.com/404')

0 commit comments

Comments
 (0)