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

Storybook generator creates stories for stories #10286

Closed
Brian-McBride opened this issue May 13, 2022 · 5 comments · Fixed by #10570
Closed

Storybook generator creates stories for stories #10286

Brian-McBride opened this issue May 13, 2022 · 5 comments · Fixed by #10570
Assignees
Labels
outdated scope: storybook Issues related to Storybook support in Nx type: bug

Comments

@Brian-McBride
Copy link

Current Behavior

Storybook npx nx generate @nrwl/react:stories is creating stories for files that have .stories in the name.

Expected Behavior

Stories should only be created for actual component files.

Steps to Reproduce

`npx nx generate @nrwl/react:stories --project=

Environment

Node : 16.13.1
   OS   : darwin x64
   npm  : 8.10.0
   
   nx : 14.1.5
   @nrwl/angular : Not Found
   @nrwl/cypress : 14.1.5
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.1.5
   @nrwl/eslint-plugin-nx : 14.1.5
   @nrwl/express : 14.1.5
   @nrwl/jest : 14.1.5
   @nrwl/js : 14.1.5
   @nrwl/linter : 14.1.5
   @nrwl/nest : Not Found
   @nrwl/next : 14.1.5
   @nrwl/node : 14.1.5
   @nrwl/nx-cloud : 14.0.3
   @nrwl/nx-plugin : 14.1.5
   @nrwl/react : 14.1.5
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 14.1.5
   @nrwl/web : 14.1.5
   @nrwl/workspace : 14.1.5
   typescript : 4.6.4
   rxjs : 6.6.7
   ---------------------------------------
   Community plugins:
         @nx-extend/gcp-cloud-run: 3.4.0
         @nx-extend/gcp-functions: 6.2.0
@Brian-McBride
Copy link
Author

Brian-McBride commented May 13, 2022

I don't have time to open a PR, on a tight deadline. But I did trace down the problem.

Affected code: packages/react/src/generators/stories.ts

Existing code link

visitNotIgnoredFiles(tree, projectPath, (path) => {
if (
(path.endsWith('.tsx') && !path.endsWith('.spec.tsx')) ||
(path.endsWith('.js') && !path.endsWith('.spec.js')) ||
(path.endsWith('.jsx') && !path.endsWith('.spec.jsx'))
) {
const ext = path.slice(path.lastIndexOf('.'));
const storyPath = `${path.split(ext)[0]}.stories${ext}`;
// only add component if a stories file doesnt already exist
if (!tree.exists(storyPath)) {
componentPaths.push(path);
}
}
});

Suggestion for updated code

  visitNotIgnoredFiles(tree, projectPath, (path) => {
    if (
      (path.endsWith('.tsx') && !path.endsWith('.spec.tsx')) ||
      (path.endsWith('.js') && !path.endsWith('.spec.js')) ||
      (path.endsWith('.jsx') && !path.endsWith('.spec.jsx'))
    ) {
      const ext = path.slice(path.lastIndexOf('.'));
      const storyPath = `${path.split(ext)[0]}.stories${ext}`;
      // Use the new path using endsWith
      const isStory = storyPath.endsWith(`.stories.stories${ext}`)
      // Optionally use the org path as a comparison
      // const isStory = path.slice(-ext.length - 7, -ext.length) === 'stories'

      // Updating this line with set check
      if (!isStory && !tree.exists(storyPath)) {
        componentPaths.push(path);
      }
    }
  });

Conclusion

Honestly, I think there is a deeper problem with tree.exists() function. Apparently, that should be grabbing the fact that a file exists in that tree. In my case I am using subfolders. libs/client/components/src/lib/ so maybe that is causing issues.

Still, if we know the path of the file we are going to generate, it is easy enough to check if it exists in any number of ways.

The way I see the code currently, it will tag the .stories extension on everything and then check to see if it exists. Which will keep causing recursive creations.

@Brian-McBride
Copy link
Author

Brian-McBride commented May 13, 2022

@Coly010 It looks like you fixed this before. I didn't have this issue an update or two ago (can't remember when).

@AgentEnder AgentEnder changed the title Storybook generator creates stories for stores Storybook generator creates stories for stories May 13, 2022
@AgentEnder AgentEnder added the scope: storybook Issues related to Storybook support in Nx label May 13, 2022
@Balance8
Copy link

Bump, I can confirm this is occurring on my repo as well. It is generating stories for story files as well as double-generating cypress specs.

@mandarini
Copy link
Member

Oops that sounds frustrating. Looking into it now, thanks @Brian-McBride @Balance8 !

@mandarini mandarini self-assigned this Jun 3, 2022
mandarini added a commit to mandarini/nx that referenced this issue Jun 3, 2022
mandarini added a commit to mandarini/nx that referenced this issue Jun 3, 2022
mandarini added a commit to mandarini/nx that referenced this issue Jun 3, 2022
mandarini added a commit that referenced this issue Jun 3, 2022
@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: storybook Issues related to Storybook support in Nx type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants