8000 [make:stimulus-controller] Add `classes` support, generate usage code, fix doc, add tests by smnandre · Pull Request #1631 · symfony/maker-bundle · GitHub
[go: up one dir, main page]

Skip to content

[make:stimulus-controller] Add classes support, generate usage code, fix doc, add tests #1631

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

Merged
merged 12 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add --typescript / --ts (non interactive) option (default false)
  • Loading branch information
smnandre committed Dec 25, 2024
commit a61b18e9e86e5f2c31d3a34d359ba262fe12a07c
14 changes: 12 additions & 2 deletions config/help/MakeStimulusController.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
The <info>%command.name%</info> command generates new Stimulus Controller.
The <info>%command.name%</info> command generates a new Stimulus controller.

<info>php %command.full_name% hello</info>

If the argument is missing, the command will ask for the controller name interactively.
If the argument is missing, the command will ask for the controller name interactively.

To generate a TypeScript file (instead of a JavaScript file) use the <info>--typescript</info>
(or <info>--ts</info>) option:

<info>php %command.full_name% hello --typescript</info>

It will also interactively ask for values, targets, classes to add to the Stimulus
controller (optional).

<info>php %command.full_name%</info>
24 changes: 16 additions & 8 deletions src/Maker/MakeStimulusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\Question;
use Symfony\UX\StimulusBundle\StimulusBundle;
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
Expand All @@ -44,8 +45,11 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
{
$command
->addArgument('name', InputArgument::REQUIRED, 'The name of the Stimulus controller (e.g. <fg=yellow>hello</>)')
->addOption('typescript', 'ts', InputOption::VALUE_NONE, 'Create a TypeScript controller (default is JavaScript)')
->setHelp($this->getHelpFileContents('MakeStimulusController.txt'))
;

$inputConfig->setArgumentAsNonInteractive('typescript');
}

public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
Expand All @@ -54,16 +58,20 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
$command->addArgument('targets', InputArgument::OPTIONAL);
$command->addArgument('values', InputArgument::OPTIONAL);

$chosenExtension = $io->choice(
if ($input->getOption('typescript')) {
$input->setArgument('extension', 'ts');
} else {
$chosenExtension = $io->choice(
'Language (<fg=yellow>JavaScript</> or <fg=yellow>TypeScript</>)',
[
'js' => 'JavaScript',
'ts' => 'TypeScript',
],
'js',
);
[
'js' => 'JavaScript',
'ts' => 'TypeScript',
],
'js',
);

$input->setArgument('extension', $chosenExtension);
$input->setArgument('extension', $chosenExtension);
}

if ($io->confirm('Do you want to include targets?')) {
$targets = [];
Expand Down
35 changes: 33 additions & 2 deletions tests/Maker/MakeStimulusControllerTest.php
E4A5
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ protected function getMakerClass(): string

public function getTestDetails(): \Generator
{
yield 'it_generates_stimulus_controller' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker(
[
'default', // controller name
],
);

$generatedFilePath = $runner->getPath('assets/controllers/default_controller.js');
$this->assertFileExists($generatedFilePath);
}),
];

yield 'it_generates_stimulus_controller_with_targets' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker(
Expand Down Expand Up @@ -74,16 +87,34 @@ public function getTestDetails(): \Generator
}),
];

yield 'it_generates_typescript_stimulus_controller' => [$this->createMakerTest()
yield 'it_generates_typescript_stimulus_controller_interactively' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker(
[
'typescript', // controller name
'ts', // controller language
'no', // do not add targets
]);
],
);

$this->assertFileExists($runner->getPath('assets/controllers/typescript_controller.ts'));
$this->assertFileDoesNotExist($runner->getPath('assets/controllers/typescript_controller.js'));
}),
];

yield 'it_generates_typescript_stimulus_controller_when_option_is_set' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker(
[
'typescript', // controller name
// '', // language is not asked interactively
'no', // do not add targets
],
' --typescript'
);

$this->assertFileExists($runner->getPath('assets/controllers/typescript_controller.ts'));
$this->assertFileDoesNotExist($runner->getPath('assets/controllers/typescript_controller.js'));
}),
];
}
Expand Down
0