Repository navigation
Conversation
init detects the Storybook or E2E project, checks the project token, writes chromatic.config.json, adds the package and script, sets up one CI provider, then checks the result and offers fixes. doctor checks a project's config, git history, token and CI setup, and --fix applies fixes one at a time after showing a diff. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
|
| * @returns The edit for a fix. | ||
| */ | ||
| export function packageJsonEdit(file: string, change: (json: Record<string, any>) => void) { | ||
| const previous = readFileSync(file, 'utf8'); |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - high severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
|
|
||
| export function readJson(filePath: string): Record<string, any> | undefined { | ||
| try { | ||
| return JSON.parse(readFileSync(filePath, 'utf8')); |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - high severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| ); | ||
| change(json); | ||
| const file = input.configFile ?? path.join(input.cwd, 'chromatic.config.json'); | ||
| const previous = existsSync(file) ? readFileSync(file, 'utf8') : undefined; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| return detected.flatMap(({ provider, chromaticFiles }) => | ||
| chromaticFiles | ||
| .filter((file) => { | ||
| const text = readFileSync(path.join(gitRoot, file), 'utf8').replaceAll( |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| const { fix } = next; | ||
| const preview = fix.edits | ||
| .map(({ file, content }) => { | ||
| const before = existsSync(file) ? readFileSync(file, 'utf8') : ''; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
|
📦 Package Size: 8180 KB Bundled dependencies compared with PR base 82312b5. Bundled dependency changesVersions from emitted source maps; external dependencies are not included. CLI / Node API
GitHub ActionNo bundled dependency changes. |
|
|
||
| write(file: string, content: string) { | ||
| const existed = existsSync(file); | ||
| if (existed && readFileSync(file, 'utf8') === content) return false; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| this.changes.push({ file, kind: existed ? 'updated' : 'created', content }); | ||
| if (!this.dryRun) { | ||
| mkdirSync(path.dirname(file), { recursive: true }); | ||
| writeFileSync(file, content); |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| */ | ||
| export function gitignoreAdditions(gitRoot: string, wanted: string[]) { | ||
| const file = path.join(gitRoot, '.gitignore'); | ||
| const text = existsSync(file) ? readFileSync(file, 'utf8') : ''; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
Remove the old init flow, now unrouted, with its test and the dependencies only it used. Rename abbreviations, add JSDoc, move the token step into its own file, and mark the long step functions as prototype code for the complexity rules. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
| { | ||
| for (const { provider, file } of runs) { | ||
| // Drop comments, so a comment that mentions pull_request doesn't count as a trigger. | ||
| const raw = readFileSync(path.join(gitRoot, file), 'utf8'); |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| */ | ||
| export function environmentTokenEdits(input: DoctorInput, token: string) { | ||
| const environmentFile = path.join(input.cwd, '.env'); | ||
| const text = existsSync(environmentFile) ? readFileSync(environmentFile, 'utf8') : ''; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
|
|
||
| for (const file of files.filter((f) => STORYBOOK_MAIN.test(f))) { | ||
| const directory = path.join(gitRoot, path.dirname(path.dirname(file))); | ||
| const main = readFileSync(path.join(gitRoot, file), 'utf8'); |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| return CI_PROVIDERS.map((provider) => { | ||
| const found = files.filter((file) => provider.patterns.some((p) => p.test(file))); | ||
| const chromaticFiles = found.filter((file) => | ||
| RUNS_CHROMATIC.test(readFileSync(path.join(gitRoot, file), 'utf8')) |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| function configStep(session: Session, project: Project) { | ||
| const { writer } = session; | ||
| const file = path.join(project.directory, 'chromatic.config.json'); | ||
| const previous = existsSync(file) ? readFileSync(file, 'utf8') : undefined; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| async function packageStep(session: Session, project: Project, framework: Framework) { | ||
| const { writer } = session; | ||
| const file = path.join(project.directory, 'package.json'); | ||
| const previous = readFileSync(file, 'utf8'); |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| ); | ||
| if (!save) return; | ||
| const environmentFile = path.join(project.directory, '.env'); | ||
| const text = existsSync(environmentFile) ? readFileSync(environmentFile, 'utf8') : ''; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
…prototype Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef # Conflicts: # package.json # yarn.lock
Section titles are now nouns: Project, Configuration, and Git, token and CI. The TurboSnap check also looks for --only-changed in the package's scripts and onlyChanged in the CI step that runs Chromatic, so it no longer reports TurboSnap off when there's no config file. Long details wrap at the width Clack uses without a terminal. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
In a terminal, init shows the error and asks for the token again instead of exiting. A rejected CHROMATIC_PROJECT_TOKEN is no longer offered. With --project-token or without a terminal, a rejected token still stops the run. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
init no longer waits on a fix prompt in CI, never overwrites an existing workflow file, lets pnpm and Yarn Berry update the lockfile, and exits 1 when the install fails. Yarn is detected from the packageManager version. Fix previews mask project tokens, a token is never moved into a .env that git tracks, and no fixes are offered when the config file fails to load. AGENTS.md and the verify skill now describe the new init and doctor. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
The CLI loads .env before init writes it, so set the variable the way dotenv would after saving, and the check no longer reports no token. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
doctor and init guessed options, targets and CI facts with their own text heuristics, then edited files on those guesses. They now share the main command's sources of truth: - CI files are parsed as YAML, one run per job that runs Chromatic. Clone depth, working directory and --only-changed are judged per job, and fixes edit only that job with comments and layout kept. Comments that mention chromatic no longer count as runs. - Options come from getPartialOptions with the cwd's .env, so --build-command, --vitest, --project-token and conflicting options are honored. Config fixes are offered only for plain JSON the option comes from, never over a flag. - Projects include Storybooks found through a build script's -c, skip tracked files deleted from disk, and --framework overrides detection. - init reuses an existing chromatic.config.jsonc or .json5 instead of shadowing it, and leaves files it can't safely rewrite alone. - In a monorepo, secrets and workflow files are named after the project's full path (CHROMATIC_PROJECT_TOKEN_PACKAGES_UI, chromatic-packages-ui.yml). Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
| return detectCi(files).flatMap(({ provider, files: found }) => | ||
| found.flatMap((file) => { | ||
| try { | ||
| const raw = readFileSync(path.join(gitRoot, file), 'utf8'); |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - high severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| ); | ||
| change(json); | ||
| const file = input.configFile ?? path.join(input.cwd, 'chromatic.config.json'); | ||
| const previous = existsSync(file) ? readFileSync(file, 'utf8') : undefined; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - high severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
|
|
||
| function storybookProject(gitRoot: string, relative: string, file: string): Project { | ||
| const directory = path.join(gitRoot, relative); | ||
| const main = readFileSync(path.join(gitRoot, file), 'utf8'); |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| */ | ||
| function buildEnvironment(cwd: string) { | ||
| const environmentFile = path.join(cwd, '.env'); | ||
| const fromFile = existsSync(environmentFile) ? dotenv.parse(readFileSync(environmentFile)) : {}; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
- Fix previews mask the token a fix moves and the environment's token wherever they appear, not only in one JSON spelling. - init and doctor save a token to .env through one helper that asks git whether the file is ignored, so a .gitignore that un-ignores .env (or a tracked .env) means the token isn't written there. - init runs its install whenever install isn't skipped. A failed install had already edited package.json, so a rerun skipped it and said setup was done. - GitHub workflow files are named like the secret: chromatic.yml, or chromatic-packages-ui.yml for a monorepo project. - Drop the unused addDev commands and FileWriter.changes. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
| ) {} | ||
|
|
||
| write(file: string, content: string) { | ||
| if (existsSync(file) && readFileSync(file, 'utf8') === content) return false; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - high severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| } | ||
| if (!configuration.configFile) return { configuration, configEditable: true }; | ||
| const configFile = path.resolve(cwd, configuration.configFile); | ||
| const configText = readFileSync(configFile, 'utf8'); |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
| reason: `git tracks ${relativeOrDot(gitRoot, file)}, so the token would be committed`, | ||
| }; | ||
| } | ||
| const text = existsSync(file) ? readFileSync(file, 'utf8') : ''; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
…s advisory doctor worked out on its own what a build and a CI runner would do, and each review found another case where its version differed. Now: - The Storybook config directory comes from findConfigDirectory with the build script's -c, and storybookBuildDir goes through validateFiles, so a missing or invalid prebuilt directory fails and a valid one needs no Storybook source. Fixes that dropped storybookConfigDir as "the default" are gone, since -c would then decide instead. - No framework is injected from detected config files: an E2E project that doesn't say so fails like a build would, with a fix that sets it. --config-file resolves from where doctor runs, for every project. - CI commands may set variables first (as init's own snippets do), YAML that doesn't parse is reported instead of half-read, GitLab scripts and GIT_DEPTH follow extends order and the pipeline defaults, and steps in one job count as one job. GitLab and scanned providers get instructions instead of automatic edits, and init's --ci overrides an existing run. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
- Before writing a token to .env, git checks the ignore rule that would be added, so a !.env in a nested .gitignore means no write, like one in the root. - storybookBuildDir is only removed when the folder isn't a valid build, as the build fails with it anyway. A valid prebuilt folder keeps its TurboSnap warning but gets no fix. - No E2E fix when the folder has two test configs; which one runs is the user's call. - A shallow checkout reached through a YAML alias is still reported, just without a fix, instead of the file being reported as unreadable. - GitLab and Bitbucket snippets run each line in a subshell, so a cd no longer carries into the next line. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
- Always add the .env ignore rule once git says no existing line ignores it, so a rule git reads differently, like ./.env, can't leak the token. - Never remove storybookBuildDir: CI may build the folder first. A missing folder is now a warning; an existing invalid one still fails. - Don't turn on TurboSnap for a prebuilt Storybook, which may have no stats file. - Rewrite storybookConfigDir or remove storybookBaseDir only when CI runs Chromatic from the project, not from the root with --config-file. - Move projectToken out of the config file only when the project's CI job passes a token itself, and say why when there's no fix. - Print CI snippets outside the note box so they paste as valid YAML. - init exits 1 when a check still fails, like doctor. - Read CI once per doctor run, recognise (npx chromatic --only-changed), skip checkouts of other repositories, and don't report missing CI when a CI file couldn't be read. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
| // Always add the pattern: git just said no existing line ignores the file, even one that looks | ||
| // like it should, such as ./.env. | ||
| const gitignore = path.join(gitRoot, '.gitignore'); | ||
| const ignoreText = existsSync(gitignore) ? readFileSync(gitignore, 'utf8') : ''; |
There was a problem hiding this comment.
Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.
Show fix
Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info
There was a problem hiding this comment.
These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.
…n init and doctor - Lift git's 1 MiB output limit, so ls-files in a large repository isn't cut off and every file made to look absent. - Don't turn on TurboSnap when a CI job or the chromatic script uploads a prebuilt Storybook with storybookBuildDir. - Enable Corepack before installing with Yarn 2+ in the GitHub workflow, and with Yarn 2+ or pnpm in the other snippets. - Count a scanned CI command with no cd for every project, since its provider may set the directory itself, but rely only on parsed jobs for fixes that change what CI reads and for duplicate warnings. - Name each monorepo project's GitLab and CircleCI job like its workflow. - Add to init's own .gitignore section instead of starting another. - Fix doctor's project count wording and the empty parentheses after an unchanged config, hide init's 'No CI job runs Chromatic' after printing a snippet, drop findChromaticRuns and an unreachable branch, and fill in the JSDoc stubs lint warned about. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
|
This PR is now split into a stack of three draft PRs (stack #1732):
The top of #1730 has the same tree as this PR's head ( |
Prototype of a rebuilt
chromatic initand a newchromatic doctor, modeled onstorybook init,storybook doctorandstorybook automigrate. It's a draft while it goes through review.🎬 Demo
Recorded from
7ce1cc08with a fake token and a local stand-in for the token check.chromatic initin a pnpm monorepo with two Storybooks, wherepackages/uihas nobuild-storybookscript: pickspackages/ui, uses the token from the environment, picks GitHub Actions, then accepts "Set buildScriptName".chromatic doctor, thenchromatic doctor --fixinpackages/uiof an npm monorepo with a misconfiguredchromatic.config.json(valid JSON, wrong values), a GitHub workflow withoutfetch-depth, and a CircleCI job that also runs Chromatic: reports 7 warnings and 3 failures, then accepts all 9 fixes. The duplicate CI andpull_requestwarnings stay, since they have no automatic fix.📋 What changed
bin-src/setup/init.tsinit: preflight, detect the project, check the token, write the config, install once, set up CI, check the result and offer fixes, then print next stepsbin-src/doctor.tsdoctorsubcommand: checks the setup without starting a build;--fixapplies fixes one at a time after showing a diffbin-src/setup/checks.ts,valueChecks.tsbin-src/setup/doctorInput.tsgetPartialOptions, config file and the project's.env, and which file owns each optionbin-src/setup/ci.ts--only-changed. GitHub and GitLab are read job by job; other providers are scanned for commands and treated as advisorybin-src/setup/detect.ts,project.ts,ciTemplates.ts,report.ts,shared.tsnode-src/lib/getOptions.ts,getStorybookMetadata.tsfindBuildScriptNameandfindConfigFlagssodoctorfinds the build script and-cfolder the same way a build doesbin-src/register.js,bin-src/init.tsdoctor, and routesinitto the new code. Removes the oldinitwith its test, and theboxenandpromptsdependencies only it used💡 Why
initdoes very little. On Storybook projects it only adds a script. On Storybook 9+ E2E projects it fails, because it tries to install@storybook/addon-essentialsat a version that was never published.🛠️ How it works
chromatic initCHROMATIC_PROJECT_TOKEN(shows its last four characters). Checks it with the same exchange every build does, and offers to save it to a gitignored.env. Never writes it topackage.jsonor the config file.chromatic.config.jsonnext to the project, withonlyChanged: trueexcept for Playwright and Cypress. Reuses an existing.jsoncor.json5, and says what to add instead of rewriting one (that would drop its comments).chromatic(and@chromatic-com/<framework>for E2E) and the script, then runs one install..github/workflows/chromatic.yml, orchromatic-<path>.ymlnamed like the secret for a monorepo project (on push,fetch-depth: 0,workingDir) and prints a snippet for other providers, outside the box so it pastes as valid YAML. Never overwrites a workflow. Skips when a CI job already runs Chromatic for the project.doctor, exits 1 when a check still fails.Flags:
--project,--framework,--project-token,--ci <provider>,--skip-ci,--skip-install,--dry-run,--yes. Without a terminal or in CI it never waits for input.chromatic doctor--build-command,--vitest,--project-token,--config-file, …), config file, environment and the project's.envthrough the main command's own option handling. The Storybook config directory comes from the build'sfindConfigDirectory, and--storybook-build-dirgoes through the samevalidateFilesas an upload."onlyChanged": "true"(a string, read as a branch name)true"onlyChanged": true, creating the config file if needed. Not when the config, a CI job or thechromaticscript uploads a prebuiltstorybookBuildDir, which may have no stats fileonlyChangedon Playwright or CypressstorybookConfigDirwritten from the repo root,storybookBaseDirthat repeats the directory Chromatic runs fromstorybookBaseDir, only when CI runs Chromatic from the project too. A CI that runs from the root with--config-filereads the paths as writtenuntraced/externalsglobs that match nothingprojectTokenin the config file.envthat git confirms is ignored, including any nested.gitignore. Only when the project's CI job passes its own token (CHROMATIC_PROJECT_TOKENor the Action'sprojectToken); otherwise it says to give CI the token firstbuild-storybookscriptbuildScriptNameat a script that runsstorybook build, or add"build-storybook": "storybook build"chromaticnot a dependencydevDependenciesfetch-depth: 0on the checkout steps of the job that runs Chromatic. Judged per job, so another job's full clone doesn't hide a shallow one. GitLab gets instructions instead, since a job-levelvariablesblock can replace one from an anchor or template"playwright": true(or Cypress, Vitest) when the folder has exactly one of those config files; with two, a failure that names both and no fixpull_requesttrigger and more than one CI running Chromatic for a project (choices, not mistakes), a missing token, a shallow local clone, and astorybookBuildDirfolder that hasn't been built yet (CI may build it first). A folder that exists but isn't a Storybook build fails, still with no fix.--fixapplies nothing and says to rerun with--fix --yes.⚖️ Trade-offs & decisions
initasks for one anddoctorwarns when there are more.storybookBaseDirorstorybookConfigDir. Making a config file in another directory act as the project root is deferred, because it would break setups that already compensate with root-relative paths.CHROMATIC_PROJECT_TOKEN_PACKAGES_UI), sopackages/uiandapps/uican't collide. A single-project repo keepsCHROMATIC_PROJECT_TOKEN.chromaticused to count as a run, and one job'sfetch-depth: 0passed every job. GitHub and GitLab jobs are read with their working directory,extendsorder and pipeline defaults; a file that doesn't parse is reported rather than half-read. Other providers (CircleCI, Jenkins, …) get a command scan: "looks like it runs Chromatic", each file counted once toward duplicates, no clone-depth check, andinit --cioverrides it. A scanned command with nocdcounts for every project, since the provider may set the directory itself (CircleCI'sworking_directory, Jenkins'dir()), but fixes that change what CI reads rely only on jobs read from GitHub or GitLab.🧪 Verification
initin a pnpm monorepo with two Storybooks (Vite and webpack)CI=1 node dist/bin.cjs init --yes --skip-install --project packages/ui --ci githubCHROMATIC_PROJECT_TOKEN_PACKAGES_UIdoctor --fix --yeson a monorepo with a misconfigured configCI=1 node dist/bin.cjs doctor --fix --yesinpackages/uidoctorreports 4 passed, 8 warnings, 2 failed and exits 1;--fix --yesapplies 8 fixes and ends at 9 passed, 3 warnings (duplicate CI with CircleCI,pull_request, unbuiltstorybookBuildDir)doctorwith# TODO: run npx chromaticin a workflowdoctor --fix --yeswithfetch-depth: 0only in another jobfetch-depth: 0to that job's checkout only and leaves comments,[push, pull_request]and long lines as writtenchromatic.config.jsoncinit --yes"onlyChanged": true; no.jsonwritten next to it. Broken JSON: left alone, exit 0 with a pointer todoctordoctorresolves paths like a build--build-commandwith no build script;--build-commandwith-c missingin the build script;--storybook-build-dirmissing, then a prebuilt folder with no.storybook; an emptystorybookConfigDir;--project packages/ui --config-file configs/selected.json.storybookas the build does; fails, then passes; fails; readsselected.json-cdoctorandinitwithstorybook build -c storybook-configpackages/uiandapps/uiinit --project …for eachCHROMATIC_PROJECT_TOKEN_APPS_UI/_PACKAGES_UI, andchromatic.yml/chromatic-packages-ui.ymlnamedChromatic (<path>)main.tsdeleted from diskdoctorandinitfrom the rootdoctor --vitestnext toplaywright.config.ts;doctor --fix --yesin a Playwright app with no config;init --framework viteston a Storybook repoonlyChangedkept; fails like a build, then sets"playwright": true;--frameworkoverrides detectiondoctorwithCHROMATIC_PROJECT_TOKEN=$X npx chromaticin CircleCI; twoif:steps in one GitHub job; unclosed[in a workflow; GitLabextends: [.full, .shallow]doctor --fix --yeswith{ "projectToken" : "…" }; with.envthen!.envin.gitignore; with!.envinpackages/ui/.gitignore;initsaving a pasted token with the same.gitignoreinitsays "Didn't save the token"; without it, saved to an ignored.envinit --yestwice with a dependency that doesn't existdoctor --fixleaves what it can't be sure ofout/withonlyChanged; Playwright and Vitest configs with no flag; a shallow checkout behind a YAML alias (with: *clone)init --project packages/ui --framework playwright --ci gitlab --dry-run, then ran the emitted lines in one shell withpwdfor the commandspackages/ui(before: the secondcd packages/uifailed)CI=1 init --project packages/ui --ci gitlab --dry-run --yesin a pnpm monorepo;--ci bitbucketin a Playwright appyaml(before: wrapped at 80 columns with│borders)270938b8vs.2140141edoctor --fix --yeswith:./.envin.gitignore;storybookBuildDirbuilt by an earlier CI step; a prebuiltout/with no stats file; CI running from the root with--config-file packages/ui/chromatic.config.json; a token in the config and a workflow without a secret.init --yes --skip-ciwithstorybookConfigDir: "nowhere"270938b8):?? .env, removedstorybookBuildDir,onlyChangedon a stats-less build, rewrote both paths, moved the token, init exit 0. After:.envignored, folder kept with a warning, no TurboSnap fix, paths kept, token kept with "Give CI the token … first", init exits 12140141evs. this commitdoctorin a repo with 40,000 files whose workflow runsnpx chromatic --only-changed;init --yeswith a workflow whosechromaui/actionstep setsstorybookBuildDir;doctorinpackages/uiwith CircleCIworking_directory: ~/project/packages/ui;init --ci gitlabfor two monorepo projects;doctorfrom a root with one project not set up"onlyChanged": truewritten, "No CI job runs Chromatic for this project", twochromatic:jobs, "Found 1 projects". After: "TurboSnap is set in .github/workflows/ci.yml", noonlyChangedand no TurboSnap fix, "CircleCI looks like it runs Chromatic",chromatic-packages-ui:andchromatic-packages-web:, "Found 1 project"init --ci github --yesin a project withpackageManager: yarn@4.18.0- run: corepack enableaftersetup-node, beforeyarn install --immutableCI=1 initon an open pipe;doctor --fix --yeswith a token in the config; a pnpm install that fails.env; install failure exits 1ci.ts: file-wide clone depth, default YAML printing, noVAR=prefix, ignored parse errors, firstextendsparent wins, job's own depth ignored, globalbefore_scriptignored, no per-job merging; the)afterchromatic, the alias guard; in the snippet, a barecd; intokenEnvironmentEdits: the tracked, root and nested negation checks, dropping the first ignore check, a duplicated token line, no ignore rule after./.env; inci.ts: the job's env, the Action'sprojectToken, the command'sCHROMATIC_PROJECT_TOKEN=, a(beforechromatic, a)after--only-changed, a checkout of another repository, the Action'sstorybookBuildDir, a command's-d, merging-dacross steps, a quote beforenpx chromatic, scanned runs matching every project (and parsed runs not); in the snippets, the job name andcorepack enable; a second# Chromaticsection in.gitignoreinitwithCHROMATIC_INDEX_URLpointed at a local stand-in👀 Reviewer notes
bin-src/setup/init.ts(mainat the bottom reads as the step list), thenbin-src/setup/doctorInput.tsandci.ts, which decide whatdoctorbelieves.ci.tsand the.envtoken helper have tests; the doctor checks anddoctorInputdon't yet. 10 functions still turn off thecomplexitylint rule (down from 14), 4 of them alsomax-statements: mostly the check lists, the token step and the twomains.doctorGIF is out of date: it still shows "Remove storybookBuildDir so Chromatic builds Storybook", which no longer exists. Today the unbuilt folder is a warning, so the run applies 8 fixes and ends at 9 passed, 3 warnings. TheinitGIF's flow is unchanged.yaml, bundled like the others.📦 Published PR as canary version:
18.15.2--canary.1728.44922920874.0✨ Test out this PR locally via:
npm install chromatic@18.15.2--canary.1728.44922920874.0 # or yarn add chromatic@18.15.2--canary.1728.44922920874.0