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

Docs: Add codemod script for docs snippets #30315

Open
wants to merge 2 commits into
base: kasper/csf-factories
Choose a base branch
from

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Jan 20, 2025

Closes #

What I did

This PR adds a new command to the scripts directory to be able to run codemods in our own documentation file.

Based on a specified codemod and glob, it will essentially go through all of the files, extract all snippets from that given file, go through some logic to define which target snippets should be migrated, then apply the codemod to those snippets.

You can run it like so in the scripts directory:

y docs:codemod <codemod> --glob=<glob> --dry-run

for instance:

y docs:codemod csf-factory-story --glob="loader-story.md" --dry-run

y docs:codemod csf-factory-story --glob="*.md"

available codemods:
csf-factory-story (for stories snippets), csf-factory-config (for preview.js/main.js snippets)

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

Greptile Summary

Adds a new codemod script for transforming documentation code snippets to CSF 4 format, with functionality to extract, transform, and append updated snippets while maintaining original content.

  • Added scripts/snippets/codemod.ts with snippet extraction and transformation logic using concurrent processing
  • Added SNIPPETS_DIRECTORY constant in scripts/utils/constants.ts pointing to '_snippets' directory
  • Added docs:codemod script command and globby dependency in scripts/package.json
  • Implemented CSF factory codemods for both story and config files with proper error handling

@yannbf yannbf added the ci:docs Run the CI jobs for documentation checks only. label Jan 20, 2025
@yannbf yannbf self-assigned this Jan 20, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines 107 to 108
const newSnippet = { ...snippet };
newSnippet.attributes.tabTitle = 'CSF 4 (experimental)';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: redundant assignment of tabTitle - it's set again in the attributes spread below

Comment on lines +176 to +205
export function parseAttributes(attributes: string): Record<string, string> {
const attributeRegex = /([a-zA-Z0-9.-]+)="([^"]+)"/g;
const result: Record<string, string> = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: attributeRegex is too permissive - should escape dots and validate attribute names more strictly

Comment on lines +226 to +255
program
.name('command')
.description('A minimal CLI for demonstration')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: CLI name and description are placeholder values that should be updated with actual command details

Copy link

nx-cloud bot commented Jan 20, 2025

View your CI Pipeline Execution ↗ for commit a7bbc4c.

Command Status Duration Result
nx run-many -t build --parallel=3 ✅ Succeeded 1m 45s View ↗

☁️ Nx Cloud last updated this comment at 2025-01-21 16:59:37 UTC

@yannbf yannbf force-pushed the yann/csf-factories-docs-codemod branch 2 times, most recently from 831bf94 to 7d38d73 Compare January 20, 2025 18:48
@yannbf yannbf force-pushed the yann/csf-factories-base branch from f75b007 to 80c80ac Compare January 20, 2025 20:49
Base automatically changed from yann/csf-factories-base to kasper/csf-factories January 21, 2025 11:30
@yannbf yannbf force-pushed the yann/csf-factories-docs-codemod branch from 7d38d73 to a7bbc4c Compare January 21, 2025 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:docs Run the CI jobs for documentation checks only.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant