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(v6): stub @storybook/react types till @redwoodjs/cli-storybook is installed #9027

Merged
merged 8 commits into from
Aug 28, 2023
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'node:fs'
import path from 'node:path'

import execa, { ExecaError } from 'execa'
Expand All @@ -19,6 +20,14 @@ export async function handler({
port,
smokeTest,
}: StorybookYargsOptions) {
// We add a stub file to type generation because users don't have Storybook
// installed when they first start a project. We need to remove the file once
// they install Storybook so that the real types come through.
fs.rmSync(
path.join(getPaths().generated.types.includes, 'web-storybook.d.ts'),
{ force: true }
)

// Check for conflicting options
if (build && smokeTest) {
throw new Error('Can not provide both "--build" and "--smoke-test"')
Expand Down
35 changes: 35 additions & 0 deletions packages/internal/src/generate/typeDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const generateTypeDefs = async () => {
...generateTypeDefGlobalContext(),
...generateTypeDefScenarios(),
...generateTypeDefTestMocks(),
...generateStubStorybookTypes(),
dac09 marked this conversation as resolved.
Show resolved Hide resolved
...gqlApiTypeDefFiles,
...gqlWebTypeDefFiles,
],
Expand Down Expand Up @@ -199,3 +200,37 @@ export const generateTypeDefGlobImports = () => {
export const generateTypeDefGlobalContext = () => {
return writeTypeDefIncludeFile('api-globalContext.d.ts.template')
}

function generateStubStorybookTypes() {
const stubStorybookTypesFileContent = `\
declare module '@storybook/react' {
export type Meta<T = any> = any
export type StoryObj<T = any> = any
}
`

const redwoodProjectPaths = getPaths()

const packageJson = JSON.parse(
fs.readFileSync(
path.join(redwoodProjectPaths.base, 'package.json'),
'utf-8'
)
)

const hasCliStorybook = Object.keys(packageJson['devDependencies']).includes(
'@redwoodjs/cli-storybook'
)

if (hasCliStorybook) {
return []
}

const stubStorybookTypesFilePath = path.join(
redwoodProjectPaths.generated.types.includes,
'web-storybook.d.ts'
)
fs.writeFileSync(stubStorybookTypesFilePath, stubStorybookTypesFileContent)

return [stubStorybookTypesFilePath]
}
Loading