Skip to content
Snippets Groups Projects
Commit cd8aaa62 authored by John Ferris's avatar John Ferris Committed by Mateu Aguiló Bosch
Browse files

Issue #3424336 by pixelwhip, bradwade, e0ipso, bander2, brayn7, markie: Allow...

Issue #3424336 by pixelwhip, bradwade, e0ipso, bander2, brayn7, markie: Allow the server url to be set as a global parameter
parent b04a09fa
No related branches found
No related tags found
1 merge request!6Issue #3424336: Allow the server url to be set as a global parameter
Pipeline #337350 passed with warnings
......@@ -180,6 +180,39 @@ If you want to monitor story changes to compile Twig stories into JSON, execute
watch --color drush storybook:generate-all-stories
```
#### Setting the server url for Stories
In order for Storybook to fetch the rendered story from Drupal, it must know the url for the Storybook route. By default this url is added as a [story parameter](https://storybook.js.org/docs/writing-stories/parameters) during the compilation process and will be set based on the URI configured for drush.
To override the domain, use Drush's `--uri` option.
```bash
drush storybook:generate-all-stories --uri=https://my-site.com
```
If you'd prefer to set the server URL in Storybook configuration, you can omit the server url parameter from the compiled stories.json files with the `--omit-server-url` option. This is useful when deploying a static version of your Storybook application to different environments.
```bash
drush storybook:generate-all-stories --omit-server-url
```
You will then need to set the server url option in Storybook's `.storybook/preview.[ts|js]` file. *NOTE: You must include the full path to Drupal's Storybook route when setting this configuration via Storybook configuration. Setting only the domain will not work*
```js
const preview = {
server: {
url: process.env.STORYBOOK_SERVER_URL || 'http://my-site.com/storybook/stories/render',
},
parameters: {
...
},
};
export default preview;
```
In this example, we are setting the url to the `$STORYBOOK_SERVER_URL` environment variable if it's available, otherwise falling back to `http://my-site.com/storybook/stories/render`.
### Tugboat setup and configuration
[Tugboat](https://www.tugboatqa.com/) is a service that builds a complete, working website, for every pull request. You can also preview your Storybook application within Tugboat with a few additional configurations.
......
......@@ -43,7 +43,8 @@ final class StorybookCommands extends DrushCommands {
*/
#[CLI\Command(name: 'storybook:generate-all-stories', aliases: ['generate-all-stories'])]
#[CLI\Option(name: 'force', description: 'Generate JSON files even for stories that have not changed.')]
public function generateAllStories($options = ['force' => FALSE]): void {
#[CLI\Option(name: 'omit-server-url', description: 'Omits the server url parameter from the generated JSON files.')]
public function generateAllStories($options = ['force' => FALSE, 'omit-server-url' => FALSE]): void {
// Find all templates in the site and call generateStoriesForTemplate.
$scan_dirs = ['modules', 'profiles', 'themes'];
$template_files = array_reduce(
......@@ -109,11 +110,15 @@ final class StorybookCommands extends DrushCommands {
#[CLI\Command(name: 'storybook:generate-stories', aliases: ['generate-stories'])]
#[CLI\Argument(name: 'template_path', description: 'Path to the *.stories.twig template file. This path should be relative to the Drupal root.')]
#[CLI\Option(name: 'force', description: 'Generate JSON files even for stories that have not changed.')]
public function generateStoriesForTemplate(string $template_path, $options = ['force' => FALSE]): void {
#[CLI\Option(name: 'omit-server-url', description: 'Omits the server url parameter from the generated JSON files.')]
public function generateStoriesForTemplate(string $template_path, $options = ['force' => FALSE, 'omit-server-url' => FALSE]): void {
$root = \Drupal::root();
$url = Url::fromUri('internal:/storybook/stories/render', ['absolute' => TRUE])
->toString(TRUE)
->getGeneratedUrl();
$url = '';
if (!$options['omit-server-url']) {
$url = Url::fromUri('internal:/storybook/stories/render', ['absolute' => TRUE])
->toString(TRUE)
->getGeneratedUrl();
}
$template_file = new \SplFileInfo($root . DIRECTORY_SEPARATOR . $template_path);
$destination_path = preg_replace('/\.stories\.twig/', '.stories.json', $template_path);
$should_generate = TRUE;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment