Skip to content

Rebuild chromatic init as a step-by-step setup - #1730

Draft
codykaup wants to merge 2 commits into
codykaup/setup-doctorfrom
codykaup/setup-init
Draft

codykaup wants to merge 2 commits into
codykaup/setup-doctorfrom
codykaup/setup-init

Conversation

@codykaup

@codykaup codykaup commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Rebuilds chromatic init as a step-by-step setup modeled on storybook init: it picks the project, checks the token, writes the config and script, installs once, sets up CI, and ends with doctor's checks and fixes. It sits on the doctor PR below it in the stack.

🎬 Demo

chromatic init in a pnpm monorepo with two Storybooks, where packages/ui has no build-storybook script: picks packages/ui, uses the token from the environment, picks GitHub Actions, then accepts "Set buildScriptName". Recorded on an earlier commit with a fake token and a local stand-in for the token check; the flow is unchanged.

chromatic init demo

📋 What changed

Area Change
bin-src/setup/init.ts Step-by-step init: preflight, detect the project, check the token, write the config, install once, set up CI, check the result and offer fixes, then print next steps
bin-src/setup/token.ts, project.ts The token step (ask, check with the index, save to a gitignored .env) and picking a project and its secret name
bin-src/setup/ciTemplates.ts CI templates: a GitHub workflow, and snippets for GitLab, Bitbucket and others
bin-src/setup/ci.ts, detect.ts, shared.ts Package manager detection, CI provider detection and file writing that only init needs
bin-src/register.js, bin-src/init.ts Routes init to the new code. Removes the old init with its test, and the boxen and prompts dependencies only it used

💡 Why

  • Today's init does very little. On Storybook projects it only adds a script. On Storybook 9+ E2E projects it fails, because it tries to install @storybook/addon-essentials at a version that was never published.
  • Setup is hard to get right, especially TurboSnap. init now writes a working TurboSnap setup and CI job, then checks the result with doctor.

🛠️ How it works

  1. Preflight: git present, package manager, CLI and Node versions.
  2. Detect: Storybooks and Playwright, Cypress or Vitest configs. Asks which one only when there are several.
  3. Token: asks for the project token, or press Enter to use CHROMATIC_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 to package.json or the config file.
  4. Config: writes chromatic.config.json next to the project, with onlyChanged: true except for Playwright, Cypress and a Storybook that CI uploads prebuilt. Reuses an existing .jsonc or .json5, and says what to add instead of rewriting one (that would drop its comments).
  5. Install once: adds chromatic (and @chromatic-com/<framework> for E2E) and the script, then runs one install.
  6. CI: one choice, with providers whose config files were found listed first. Writes .github/workflows/chromatic.yml, or chromatic-<path>.yml named like the secret for a monorepo project (on push, fetch-depth: 0, workingDir, corepack enable for Yarn 2+ and pnpm) 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.
  7. Check and fix: runs the doctor checks and offers each fix. Like doctor, exits 1 when a check still fails.
  8. Next steps: the secret to add and the command to run.

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.

⚖️ Trade-offs & decisions

  • One CI, not several. Two CIs on the same commits make duplicate builds, so init asks for one. init --ci overrides what it detects.
  • Token now, browser sign-in later. OAuth needs work on the backend first.
  • Separate token secret per project in monorepos, named after the project's full path (CHROMATIC_PROJECT_TOKEN_PACKAGES_UI), so packages/ui and apps/ui can't collide. A single-project repo keeps CHROMATIC_PROJECT_TOKEN. Monorepo CI jobs are named the same way (chromatic-packages-ui), so two projects' snippets can share one file.
  • Snippets print raw, outside the box, so they paste as YAML.

🧪 Verification

Ran against 1c936df6 on the original branch (#1728), whose tree is identical to this PR's head, with a fake token and a local stand-in for the token check. Rows marked † ran against earlier commits and weren't affected by later ones.

Check Command Result
✅ init in a pnpm monorepo with two Storybooks (Vite and webpack) CI=1 node dist/bin.cjs init --yes --skip-install --project packages/ui --ci github Wrote the config, script and workflow; applied "Set buildScriptName to build:storybook"; "Checked again: 9 passed"; secret CHROMATIC_PROJECT_TOKEN_PACKAGES_UI
✅ † Existing chromatic.config.jsonc init --yes Leaves it alone and says to add "onlyChanged": true; no .json written next to it. Broken JSON: left alone, exit 0 with a pointer to doctor
✅ † Storybook found through -c init with storybook build -c storybook-config Found; before: not found
✅ † packages/ui and apps/ui init --project … for each CHROMATIC_PROJECT_TOKEN_APPS_UI / _PACKAGES_UI, and chromatic.yml / chromatic-packages-ui.yml named Chromatic (<path>)
✅ † --framework init --framework vitest on a Storybook repo --framework overrides detection
✅ † Token never shown or committed init saving a pasted token with !.env in .gitignore, then without it With the negation, "Didn't save the token"; without it, saved to an ignored .env
✅ † Rerun after a failed install init --yes twice with a dependency that doesn't exist Both runs install and exit 1; before: the second skipped the install and said "Chromatic is set up"
✅ † GitLab snippet for a subdirectory init --project packages/ui --framework playwright --ci gitlab --dry-run, then ran the emitted lines in one shell with pwd for the commands Each line runs in packages/ui (before: the second cd packages/ui failed)
✅ † Snippets paste as YAML CI=1 init --project packages/ui --ci gitlab --dry-run --yes in a pnpm monorepo; --ci bitbucket in a Playwright app Printed without the box; the 100-character script line stays on one line and the copied text parses with yaml (before: wrapped at 80 columns with │ borders)
✅ † Failed checks fail init init --yes --skip-ci with storybookConfigDir: "nowhere" Exits 1 (before: 0)
✅ Prebuilt CI uploads and monorepo snippets, 2140141e vs. 1c936df6 init --yes with a workflow whose chromaui/action step sets storybookBuildDir; init --ci gitlab for two monorepo projects Before: "onlyChanged": true written, two chromatic: jobs. After: no onlyChanged, chromatic-packages-ui: and chromatic-packages-web:
✅ Yarn 2+ workflow init --ci github --yes in a project with packageManager: yarn@4.18.0 - run: corepack enable after setup-node, before yarn install --immutable
✅ † Round-1 fixes still hold CI=1 init on an open pipe; a pnpm install that fails Doesn't wait; install failure exits 1
✅ New tests catch the old bugs In the snippets, put back a bare cd, the shared chromatic: job name, and no corepack enable A test fails for each
⚠️ Token check init with CHROMATIC_INDEX_URL pointed at a local stand-in Valid and invalid tokens handled; not run against the real index
⏭️ Publish to Chromatic — Not run: setup only, no build

👀 Reviewer notes

  • Start here: bin-src/setup/init.ts; main at the bottom reads as the step list.
  • Untested in places. The token step and project picker have no tests yet; the snippet has one.
  • Aikido's readFile warnings are about reading files in the user's own project, which the user can already read.
  • Split out of Prototype step-by-step init and doctor subcommands #1728, where rounds 1–5 of review happened.
📦 Published PR as canary version: 18.15.2--canary.1730.45043693619.0

✨ Test out this PR locally via:

npm install chromatic@18.15.2--canary.1730.45043693619.0
# or 
yarn add chromatic@18.15.2--canary.1730.45043693619.0

@codykaup codykaup added minor Auto: Increment the minor version when merged skip-release Auto: Preserve the current version when merged labels Nov 21, 2026
@coderabbitai

coderabbitai Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment thread bin-src/setup/shared.ts
) {}

write(file: string, content: string) {
if (existsSync(file) && readFileSync(file, 'utf8') === content) return false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread bin-src/setup/shared.ts
if (existsSync(file) && readFileSync(file, 'utf8') === content) return false;
if (!this.dryRun) {
mkdirSync(path.dirname(file), { recursive: true });
writeFileSync(file, content);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

📦 Package Size: 8048 KB

Bundled dependencies compared with PR base 154df91.

Bundled dependency changes

Versions from emitted source maps; external dependencies are not included.

CLI / Node API

Package Base PR
boxen 9.0.0 —
chalk 1.1.3, 2.4.2, 4.1.2, 6.0.1 1.1.3, 2.4.2, 4.1.2
cli-boxes 4.0.1 —
get-east-asian-width 1.6.0, 1.7.0 1.6.0
is-fullwidth-code-point 1.0.0, 2.0.0, 5.1.0 1.0.0, 2.0.0
kleur 3.0.3 —
prompts 2.4.2 —
slice-ansi 0.0.4, 9.0.1 0.0.4
storybook 10.6.6 10.6.7
string-width 1.0.2, 2.1.1, 8.2.1, 8.3.0 1.0.2, 2.1.1, 8.2.1
widest-line 6.0.0 —
yaml 2.9.1 —
zen-observable 0.8.15 0.10.0

GitHub Action

Package Base PR
storybook 10.6.6 10.6.7
zen-observable 0.8.15 0.10.0

…init

init now picks the Storybook project, asks for and checks the project
token, keeps it in a gitignored .env, writes chromatic.config.json and
the chromatic script, installs the package, and writes a GitHub workflow
or prints a snippet for other CI providers. It ends by running doctor's
checks, so the user sees what's left. It replaces the old init and drops
its boxen and prompts dependencies.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
init can't tell from CI files whether a monorepo already runs Chromatic
for a project, so its CI step wrote duplicate workflows. It now ends
with a link to the CI setup steps instead, and drops --ci, --skip-ci
and the per-project secret names. TurboSnap comes from doctor's fix in
the closing check, which asks how CI runs Chromatic first.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
@codykaup
codykaup force-pushed the codykaup/setup-init branch from ec08199 to ae4f6f7 Compare November 22, 2026 16:24

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Auto: Increment the minor version when merged skip-release Auto: Preserve the current version when merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant