-
Notifications
You must be signed in to change notification settings - Fork 10
New maker command #30
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Storybook\Maker; | ||
|
||
use Symfony\Bundle\MakerBundle\ConsoleStyle; | ||
use Symfony\Bundle\MakerBundle\DependencyBuilder; | ||
use Symfony\Bundle\MakerBundle\Generator; | ||
use Symfony\Bundle\MakerBundle\InputConfiguration; | ||
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Question\Question; | ||
|
||
/** | ||
* @method string getCommandDescription() | ||
*/ | ||
class MakeStory extends AbstractMaker | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be final |
||
{ | ||
public function __construct( | ||
private StoryRenderer $storyRenderer, | ||
) { | ||
} | ||
|
||
public static function getCommandName(): string | ||
{ | ||
return 'make:storybook:story'; | ||
} | ||
|
||
public static function getCommandDescription(): string | ||
{ | ||
return 'Creates a new Storybook story'; | ||
} | ||
|
||
public function configureCommand(Command $command, InputConfiguration $inputConfig): void | ||
{ | ||
$command | ||
->addArgument('component', InputArgument::OPTIONAL, 'The name of the component you want a story for') | ||
; | ||
} | ||
|
||
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void | ||
{ | ||
if (null === $input->getArgument('component')) { | ||
$question = new Question('What is the name of the component?'); | ||
$question->setValidator(fn ($value) => null !== $value); | ||
$question->setMaxAttempts(3); | ||
$input->setArgument('component', $io->askQuestion($question)); | ||
} | ||
} | ||
|
||
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) | ||
{ | ||
$componentName = $input->getArgument('component'); | ||
|
||
$this->storyRenderer->render($componentName); | ||
} | ||
|
||
public function configureDependencies(DependencyBuilder $dependencies) | ||
{ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Storybook\Maker; | ||
|
||
use Symfony\Bundle\MakerBundle\Generator; | ||
use Symfony\UX\TwigComponent\ComponentFactory; | ||
|
||
class StoryRenderer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to rename this class to something that doesn't collide with the main Also this class could be final |
||
{ | ||
public function __construct( | ||
private Generator $generator, | ||
private ComponentFactory $componentFactory, | ||
private string $storiesPath, | ||
) { | ||
} | ||
|
||
public function render(string $componentName): void | ||
{ | ||
$componentMetadata = $this->componentFactory->metadataFor($componentName); | ||
|
||
$this->generator->generateFile($this->storiesPath.'/'.$componentName.'.stories.js', __DIR__.'/../Resources/skeleton/Story.tpl.php', [ | ||
'componentName' => $componentName, | ||
'template' => $componentMetadata->getTemplate(), | ||
]); | ||
|
||
$this->generator->writeChanges(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import <?php echo $componentName; ?> from "../templates/<?php echo $template; ?>"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Relates to the previous comment on stories path: I'd prefer the full path to be dynamically generated depending on where the story file is created. |
||
import { twig } from '@sensiolabs/storybook-symfony-webpack5'; | ||
|
||
export default { | ||
component: (args) => ({ | ||
component: {<?php echo $componentName; ?>}, | ||
template: twig` | ||
<twig:<?php echo $componentName; ?>/> | ||
`, | ||
}) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to add a default story here, like export const Default = {}; This is required for Storybook to properly index the story file, otherwise we'll get an indexing error AFAIK. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stories path depends on Storybook configuration, so I think this should be handled in a interactive prompt at maker level, with smart suggestions like
stories/
or the current component template directory?