Skip to content

Commit

Permalink
Merge pull request #284 from hydephp/improve-make-page-command
Browse files Browse the repository at this point in the history
Improve the make:page command
  • Loading branch information
caendesilva authored Jul 28, 2022
2 parents 5b6e14d + 69ddb36 commit c321489
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This serves two purposes:

### Added
- Add Laravel Tinker as a development dependency for the Monorepo
- Improved the `hyde make:page` command to add page type selection shorthands

### Changed
- for changes in existing functionality.
Expand Down
2 changes: 2 additions & 0 deletions docs/console-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ php hyde make:page "Photo Gallery" --type=blade
php hyde make:page "Hyde CLI Guide" --type=docs
```

> Tip: You can also use the shorthand `--blade` or `--docs` instead of `--type=blade` or `--type=docs`.
### Publish a default homepage
```bash
php hyde publish:homepage [<name>]
Expand Down
35 changes: 32 additions & 3 deletions packages/framework/src/Commands/HydeMakePageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class HydeMakePageCommand extends Command
protected $signature = 'make:page
{title? : The name of the page file to create. Will be used to generate the slug}
{--type=markdown : The type of page to create (markdown, blade, or docs)}
{--blade : Create a Blade page}
{--docs : Create a Documentation page}
{--force : Overwrite any existing files}';

/**
Expand All @@ -38,6 +40,11 @@ class HydeMakePageCommand extends Command
*/
public string $title;

/**
* The selected page type.
*/
public string $selectedType;

/**
* The page type.
*/
Expand All @@ -59,10 +66,10 @@ public function handle(): int
?? $this->ask('What is the title of the page?')
?? 'My New Page';

$this->line('<info>Creating page with title:</> '.$this->title."\n");

$this->validateOptions();

$this->line('<info>Creating a new '.ucwords($this->selectedType).' page with title:</> '.$this->title."\n");

$this->force = $this->option('force') ?? false;

$creator = new CreatesNewPageSourceFile($this->title, $this->type, $this->force);
Expand All @@ -81,7 +88,7 @@ public function handle(): int
*/
protected function validateOptions(): void
{
$type = strtolower($this->option('type') ?? 'markdown');
$type = $this->getSelectedType();

// Set the type to the fully qualified class name
if ($type === 'markdown') {
Expand All @@ -102,4 +109,26 @@ protected function validateOptions(): void

throw new UnsupportedPageTypeException("Invalid page type: $type");
}

/**
* Get the selected page type.
*
* @return string
*/
protected function getSelectedType(): string
{
$type = 'markdown';

if ($this->option('type') !== null) {
$type = strtolower($this->option('type'));
}

if ($this->option('blade')) {
$type = 'blade';
} elseif ($this->option('docs')) {
$type = 'documentation';
}

return $this->selectedType = $type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function test_command_prompts_for_title_if_it_was_not_specified()
{
$this->artisan('make:page')
->expectsQuestion('What is the title of the page?', 'Test Page')
->expectsOutput("Creating page with title: Test Page\n")
->expectsOutput("Creating a new Markdown page with title: Test Page\n")
->assertExitCode(0);

unlink(Hyde::path('_pages/test-page.md'));
Expand All @@ -137,9 +137,30 @@ public function test_command_falls_back_to_default_title_if_user_enters_nothing(
{
$this->artisan('make:page')
->expectsQuestion('What is the title of the page?', null)
->expectsOutput("Creating page with title: My New Page\n")
->expectsOutput("Creating a new Markdown page with title: My New Page\n")
->assertExitCode(0);

unlink(Hyde::path('_pages/my-new-page.md'));
}

// assert page type shorthand can be used to create blade pages
public function test_page_type_shorthand_can_be_used_to_create_blade_pages()
{
$this->artisan('make:page "foo test page" --blade')
->expectsOutput("Creating a new Blade page with title: foo test page\n")
->assertExitCode(0);

$this->assertFileExists($this->bladePath);
}

// assert page type shorthand can be used to create documentation pages
public function test_page_type_shorthand_can_be_used_to_create_documentation_pages()
{
$this->artisan('make:page "foo test page" --docs')
->expectsOutput("Creating a new Documentation page with title: foo test page\n")
->assertExitCode(0);

$this->assertFileExists(Hyde::path('_docs/foo-test-page.md'));
unlink(Hyde::path('_docs/foo-test-page.md'));
}
}

0 comments on commit c321489

Please sign in to comment.