Skip to content
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

Fix Storybook build args #4455

127 changes: 95 additions & 32 deletions packages/cli/src/commands/storybook.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import path from 'path'

import execa from 'execa'
import terminalLink from 'terminal-link'

import { getPaths } from '@redwoodjs/internal'
import { errorTelemetry } from '@redwoodjs/telemetry'

import c from '../lib/colors'

export const command = 'storybook'
export const aliases = ['sb']
export const description =
'Launch Storybook: An isolated component development environment'
'Launch Storybook: a tool for building UI components and pages in isolation'

export const builder = (yargs) => {
yargs
Expand Down Expand Up @@ -43,24 +47,30 @@ export const builder = (yargs) => {
type: 'boolean',
default: false,
})
.check((argv) => {
if (argv.build && argv.smokeTest) {
throw new Error('Can not provide both "--build" and "--smoke-test"')
}
if (argv.build && argv.open) {
throw new Error('Can not provide both "--build" or "--open"')
}
return true
})
// .check((argv) => {
// if (argv.build && argv.smokeTest) {
// throw new Error('Can not provide both "--build" and "--smoke-test"')
// }
// if (argv.build && argv.open) {
// throw new Error('Can not provide both "--build" or "--open"')
// }
// return true
// })
thedavidprice marked this conversation as resolved.
Show resolved Hide resolved
.epilogue(
`Also see the ${terminalLink(
'Redwood CLI Reference',
'https://redwoodjs.com/reference/command-line-interface#storybook'
)}`
)
}

export const handler = ({
open,
port,
build,
buildDirectory,
managerCache,
smokeTest,
open = true,
port = 7910,
build = false,
buildDirectory = 'public/storybook',
managerCache = true,
smokeTest = false,
thedavidprice marked this conversation as resolved.
Show resolved Hide resolved
}) => {
const cwd = getPaths().web.base

Expand All @@ -77,21 +87,74 @@ export const handler = ({
require.resolve('@redwoodjs/testing/config/storybook/main.js')
)

execa(
`yarn ${build ? 'build' : 'start'}-storybook`,
[
`--config-dir "${storybookConfig}"`,
!build && `--port ${port}`,
!build && '--no-version-updates',
!managerCache && '--no-manager-cache',
build && `--output-dir "${buildDirectory}"`,
!open && !smokeTest && `--ci`,
smokeTest && `--ci --smoke-test`,
].filter(Boolean),
{
stdio: 'inherit',
shell: true,
cwd,
// Case 1: yarn rw sb
// run start-storybook
// open in browser (default true)
// set port (default 7910)
// directory options are hard-coded
//NOTE: `--no-manager-cache` option is only available for `start-storybook`

if (open) {
execa(
`yarn start-storybook`,
[
`--config-dir "${storybookConfig}"`,
`--port ${port}`,
!managerCache && '--no-manager-cache',
thedavidprice marked this conversation as resolved.
Show resolved Hide resolved
].filter(Boolean),
{
stdio: 'inherit',
shell: true,
cwd,
}
)
}

// Case 2: yarn rw sb --build
// build-directory option (default web/public/storybook)
// directory for config is hard-coded
// manager-cache option (default true)
//NOTE: `--no-manager-cache` option is only available for `start-storybook`

if (build) {
execa(
`yarn build-storybook`,
[
`--config-dir "${storybookConfig}"`,
`--output-dir "${buildDirectory}"`,
thedavidprice marked this conversation as resolved.
Show resolved Hide resolved
].filter(Boolean),
{
stdio: 'inherit',
shell: true,
cwd,
}
)
}

// Case 3: yarn rw sb --smoke-test
// runs the start-storybook command; exits gracefully if started successful, throws otherwise
// passes --smoke-test and --ci options
try {
if (smokeTest) {
execa(
`yarn start-storybook`,
[
`--config-dir "${storybookConfig}"`,
`--port ${port}`,
thedavidprice marked this conversation as resolved.
Show resolved Hide resolved
// !managerCache && '--no-manager-cache',
thedavidprice marked this conversation as resolved.
Show resolved Hide resolved
`--smoke-test`,
`--ci`,
].filter(Boolean),
thedavidprice marked this conversation as resolved.
Show resolved Hide resolved
{
stdio: 'inherit',
shell: true,
cwd,
}
)
}
)
} catch (e) {
console.log(c.error(e.message))
errorTelemetry(process.argv, e.message)
process.exit(1)
}
thedavidprice marked this conversation as resolved.
Show resolved Hide resolved
}