Skip to content

Commit a61b18e

Browse files
committed
Add --typescript / --ts (non interactive) option (default false)
1 parent ac7e35a commit a61b18e

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
The <info>%command.name%</info> command generates new Stimulus Controller.
1+
The <info>%command.name%</info> command generates a new Stimulus controller.
22

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

5-
If the argument is missing, the command will ask for the controller name interactively.
5+
If the argument is missing, the command will ask for the controller name interactively.
6+
7+
To generate a TypeScript file (instead of a JavaScript file) use the <info>--typescript</info>
8+
(or <info>--ts</info>) option:
9+
10+
<info>php %command.full_name% hello --typescript</info>
11+
12+
It will also interactively ask for values, targets, classes to add to the Stimulus
13+
controller (optional).
14+
15+
<info>php %command.full_name%</info>

src/Maker/MakeStimulusController.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Command\Command;
2020
use Symfony\Component\Console\Input\InputArgument;
2121
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\Console\Input\InputOption;
2223
use Symfony\Component\Console\Question\Question;
2324
use Symfony\UX\StimulusBundle\StimulusBundle;
2425
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
@@ -44,8 +45,11 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
4445
{
4546
$command
4647
->addArgument('name', InputArgument::REQUIRED, 'The name of the Stimulus controller (e.g. <fg=yellow>hello</>)')
48+
->addOption('typescript', 'ts', InputOption::VALUE_NONE, 'Create a TypeScript controller (default is JavaScript)')
4749
->setHelp($this->getHelpFileContents('MakeStimulusController.txt'))
4850
;
51+
52+
$inputConfig->setArgumentAsNonInteractive('typescript');
4953
}
5054

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

57-
$chosenExtension = $io->choice(
61+
if ($input->getOption('typescript')) {
62+
$input->setArgument('extension', 'ts');
63+
} else {
64+
$chosenExtension = $io->choice(
5865
'Language (<fg=yellow>JavaScript</> or <fg=yellow>TypeScript</>)',
59-
[
60-
'js' => 'JavaScript',
61-
'ts' => 'TypeScript',
62-
],
63-
'js',
64-
);
66+
[
67+
'js' => 'JavaScript',
68+
'ts' => 'TypeScript',
69+
],
70+
'js',
71+
);
6572

66-
$input->setArgument('extension', $chosenExtension);
73+
$input->setArgument('extension', $chosenExtension);
74+
}
6775

6876
if ($io->confirm('Do you want to include targets?')) {
6977
$targets = [];

tests/Maker/MakeStimulusControllerTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ protected function getMakerClass(): string
2424

2525
public function getTestDetails(): \Generator
2626
{
27+
yield 'it_generates_stimulus_controller' => [$this->createMakerTest()
28+
->run(function (MakerTestRunner $runner) {
29+
$runner->runMaker(
30+
[
31+
'default', // controller name
32+
],
33+
);
34+
35+
$generatedFilePath = $runner->getPath('assets/controllers/default_controller.js');
36+
$this->assertFileExists($generatedFilePath);
37+
}),
38+
];
39+
2740
yield 'it_generates_stimulus_controller_with_targets' => [$this->createMakerTest()
2841
->run(function (MakerTestRunner $runner) {
2942
$runner->runMaker(
@@ -74,16 +87,34 @@ public function getTestDetails(): \Generator
7487
}),
7588
];
7689

77-
yield 'it_generates_typescript_stimulus_controller' => [$this->createMakerTest()
90+
yield 'it_generates_typescript_stimulus_controller_interactively' => [$this->createMakerTest()
7891
->run(function (MakerTestRunner $runner) {
7992
$runner->runMaker(
8093
[
8194
'typescript', // controller name
8295
'ts', // controller language
8396
'no', // do not add targets
84-
]);
97+
],
98+
);
99+
100+
$this->assertFileExists($runner->getPath('assets/controllers/typescript_controller.ts'));
101+
$this->assertFileDoesNotExist($runner->getPath('assets/controllers/typescript_controller.js'));
102+
}),
103+
];
104+
105+
yield 'it_generates_typescript_stimulus_controller_when_option_is_set' => [$this->createMakerTest()
106+
->run(function (MakerTestRunner $runner) {
107+
$runner->runMaker(
108+
[
109+
'typescript', // controller name
110+
// '', // language is not asked interactively
111+
'no', // do not add targets
112+
],
113+
' --typescript'
114+
);
85115

86116
$this->assertFileExists($runner->getPath('assets/controllers/typescript_controller.ts'));
117+
$this->assertFileDoesNotExist($runner->getPath('assets/controllers/typescript_controller.js'));
87118
}),
88119
];
89120
}

0 commit comments

Comments
 (0)