Skip to content

Commit

Permalink
wip upgrade to v7
Browse files Browse the repository at this point in the history
  • Loading branch information
jtoar committed May 19, 2023
1 parent 8c71f3f commit 87f58f4
Show file tree
Hide file tree
Showing 11 changed files with 1,685 additions and 3,633 deletions.
124 changes: 20 additions & 104 deletions packages/cli/src/commands/storybook.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,59 @@
import path from 'path'

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

import { getPaths } from '@redwoodjs/project-config'
import { errorTelemetry } from '@redwoodjs/telemetry'

import c from '../lib/colors'

export const command = 'storybook'
export const aliases = ['sb']

export const description =
'Launch Storybook: a tool for building UI components and pages in isolation'
'Launch Storybook: a tool for building UI components in isolation'

export const builder = (yargs) => {
export function builder(yargs) {
yargs
.option('open', {
describe: 'Open storybooks in your browser on start',
type: 'boolean',
default: true,
})
.option('build', {
describe: 'Build Storybook',
type: 'boolean',
default: false,
})
.option('build-directory', {
describe: 'Directory in web/ to store static files',
type: 'string',
default: 'public/storybook',
})
.option('ci', {
describe: 'Start server in CI mode, with no interactive prompts',
type: 'boolean',
default: false,
})
.option('open', {
describe: 'Open storybook in your browser on start',
type: 'boolean',
default: true,
})
.option('port', {
describe: 'Which port to run storybooks on',
describe: 'Which port to run storybook on',
type: 'integer',
default: 7910,
})
.option('build-directory', {
describe: 'Directory in web/ to store static files',
type: 'string',
default: 'public/storybook',
})
.option('manager-cache', {
describe:
"Cache the manager UI. Disable this when you're making changes to `storybook.manager.js`.",
type: 'boolean',
default: true,
})
.option('smoke-test', {
describe:
"CI mode plus Smoke-test (skip prompts, don't open browser, exit after successful start)",
"CI mode plus smoke-test (skip prompts; don't open browser; exit after successful start)",
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) {
console.warn(
c.warning(
'Warning: --open option has no effect when running Storybook build'
)
)
}

return true
})
.epilogue(
Expand All @@ -73,82 +64,7 @@ export const builder = (yargs) => {
)
}

export const handler = ({
open,
port,
build,
ci,
buildDirectory,
managerCache,
smokeTest,
}) => {
const cwd = getPaths().web.base

const staticAssetsFolder = path.join(getPaths().web.base, 'public')
// Create the `MockServiceWorker.js` file
// https://mswjs.io/docs/cli/init
execa(`yarn msw init "${staticAssetsFolder}" --no-save`, undefined, {
stdio: 'inherit',
shell: true,
cwd,
})

const storybookConfig = path.dirname(
require.resolve('@redwoodjs/testing/config/storybook/main.js')
)

try {
if (build) {
execa(
`yarn build-storybook`,
[
`--config-dir "${storybookConfig}"`,
`--output-dir "${buildDirectory}"`,
!managerCache && `--no-manager-cache`,
].filter(Boolean),
{
stdio: 'inherit',
shell: true,
cwd,
}
)
} else if (smokeTest) {
execa(
`yarn start-storybook`,
[
`--config-dir "${storybookConfig}"`,
`--port ${port}`,
`--smoke-test`,
`--ci`,
`--no-version-updates`,
].filter(Boolean),
{
stdio: 'inherit',
shell: true,
cwd,
}
)
} else {
execa(
`yarn start-storybook`,
[
`--config-dir "${storybookConfig}"`,
`--port ${port}`,
!managerCache && `--no-manager-cache`,
`--no-version-updates`,
ci && '--ci',
!open && `--no-open`,
].filter(Boolean),
{
stdio: 'inherit',
shell: true,
cwd,
}
)
}
} catch (e) {
console.log(c.error(e.message))
errorTelemetry(process.argv, e.message)
process.exit(1)
}
export async function handler(options) {
const { handler } = await import('./storybookHandler')
await handler(options)
}
75 changes: 75 additions & 0 deletions packages/cli/src/commands/storybookHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import path from 'path'

import execa from 'execa'

import { getPaths } from '@redwoodjs/project-config'
import { errorTelemetry } from '@redwoodjs/telemetry'

import c from '../lib/colors'

const redwoodProjectPaths = getPaths()

export const handler = ({
build,
buildDirectory,
ci,
open,
port,
smokeTest,
}) => {
const cwd = redwoodProjectPaths.web.base
const staticAssetsFolder = path.join(cwd, 'public')
const execaOptions = {
stdio: 'inherit',
shell: true,
cwd,
}

// Create the `MockServiceWorker.js` file. See https://mswjs.io/docs/cli/init.
execa.command(`yarn msw init "${staticAssetsFolder}" --no-save`, execaOptions)

const storybookConfigPath = path.dirname(
require.resolve('@redwoodjs/testing/config/storybook/main.js')
)

/** @type {string?} */
let command
const flags = [`--config-dir "${storybookConfigPath}"`]

if (build) {
command = `yarn storybook build ${[
...flags,
`--output-dir "${buildDirectory}"`,
]
.filter(Boolean)
.join(' ')}`
} else if (smokeTest) {
command = `yarn storybook dev ${[
...flags,
`--port ${port}`,
`--smoke-test`,
`--ci`,
`--no-version-updates`,
]
.filter(Boolean)
.join(' ')}`
} else {
command = `yarn storybook dev ${[
...flags,
`--port ${port}`,
`--no-version-updates`,
ci && '--ci',
!open && `--no-open`,
]
.filter(Boolean)
.join(' ')}`
}

try {
execa.command(command, execaOptions)
} catch (e) {
console.log(c.error(e.message))
errorTelemetry(process.argv, e.message)
process.exit(1)
}
}
Loading

0 comments on commit 87f58f4

Please sign in to comment.