Skip to content

refactor(init): split init flows into separate files#929

Merged
mttdnt merged 1 commit intomainfrom
refactor/split-init-flows
Apr 16, 2026
Merged

refactor(init): split init flows into separate files#929
mttdnt merged 1 commit intomainfrom
refactor/split-init-flows

Conversation

@mttdnt
Copy link
Copy Markdown
Contributor

@mttdnt mttdnt commented Apr 13, 2026

Description

Splits packages/@sanity/cli/src/commands/init.ts (~1755 lines) into 5 separate flow modules under actions/init/. This is prep work so the bundled-create branch (PR #845) can cleanly rebase on top — the file structure now matches commit 2 of that branch (d38cfc05).

New files:

  • initHelpers.ts — shared utilities (shouldPrompt, flagOrDefault, getPostInitMCPPrompt, writeStagingEnvIfNeeded)
  • scaffoldTemplate.ts — template selection + scaffolding pipeline (selectTemplate, scaffoldAndInstall)
  • initNextJs.ts — Next.js integration flow
  • initApp.ts — app template flow
  • initStudio.ts — studio template flow (includes dataset import)

9 private methods removed from InitCommand, 13 remain (auth, project/dataset/org management — those move in the future oclif-removal PR).

No new types (InitOptions, InitContext, InitError) are introduced — extracted functions take explicit parameters and use the existing Output type. The future rebase PR will consolidate these into the context/options pattern.

What to review

  • The 5 new files under actions/init/ — each is a straightforward extraction from init.ts private methods
  • init.ts changes — run() now delegates to initApp/initStudio/initNextJs instead of inline logic
  • Test changes: 3 tests updated (init.nextjs.test.ts — removed this.exit(0) expectation, init.get-project-details.test.ts — removed stale mock for app template path)
  • Verify the split doesn't change any user-facing behavior

Testing

All 78 existing init tests pass. No new tests added — this is a pure refactor with no behavioral changes (except removing the unnecessary this.exit(0) from the Next.js flow, matching the bundled-create target).

  • pnpm check:types
  • pnpm check:lint
  • pnpm check:deps ✅ (only pre-existing fixture issues)
  • pnpm test (init suite) ✅ 78/78
  • pnpm build:cli

🤖 Generated with Claude Code

@claude
Copy link
Copy Markdown

claude Bot commented Apr 13, 2026

Claude finished @mttdnt's task in 3m 59s —— View job


Review

  • Gather context
  • Review new files under actions/init/
  • Review init.ts changes
  • Check test changes
  • Check changeset requirement

Changeset: Not needed — pure refactor with no public API or behavioral change (other than the intentional this.exit(0) removal). ✓


scaffoldTemplate.ts:79shouldPrompt not used

} else if (!unattended && typescript === undefined) {

This is the same logic as shouldPrompt(unattended, typescript) (from initHelpers.ts), which is used consistently everywhere else this PR introduces. The helper is already imported here (writeStagingEnvIfNeeded is from the same module). Should be:

} else if (!remoteTemplateInfo && resolvedTemplate && resolvedTemplate.typescriptOnly === true) {
  useTypeScript = true
} else if (shouldPrompt(unattended, typescript)) {

Fix this →


init.get-project-details.test.ts — removed setupInitSuccessMocks('')

The removal is correct — the app-template test path doesn't call updateProjectInitializedAt (now only in initStudio.ts), so the GET /projects/ mock was stale. Worth noting that passing '' as projectId would have matched a path of /projects/ (the list endpoint), which is why it was being consumed rather than left pending. ✓


init.nextjs.test.tsthis.exit(0) assertions removed

Replacing expect(error?.oclif?.exit).toBe(0) with if (error) throw error is correct since this.exit(0) was removed from the Next.js flow. ✓

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 13, 2026

📦 Bundle Stats — @sanity/cli

Compared against main (c5554452)

@sanity/cli

Metric Value vs main (c555445)
Internal (raw) 2.1 KB -
Internal (gzip) 799 B -
Bundled (raw) 10.95 MB -
Bundled (gzip) 2.06 MB -
Import time 867ms +28ms, +3.3%

bin:sanity

Metric Value vs main (c555445)
Internal (raw) 975 B -
Internal (gzip) 460 B -
Bundled (raw) 9.84 MB -
Bundled (gzip) 1.77 MB -
Import time 2.06s +72ms, +3.6%

🗺️ View treemap · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

📦 Bundle Stats — @sanity/cli-core

Compared against main (c5554452)

Metric Value vs main (c555445)
Internal (raw) 93.1 KB -
Internal (gzip) 21.9 KB -
Bundled (raw) 21.63 MB -
Bundled (gzip) 3.42 MB -
Import time 819ms +20ms, +2.5%

🗺️ View treemap · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

📦 Bundle Stats — create-sanity

Compared against main (c5554452)

Metric Value vs main (c555445)
Internal (raw) 976 B -
Internal (gzip) 507 B -
Bundled (raw) 50.7 KB -
Bundled (gzip) 12.6 KB -
Import time ❌ ChildProcess denied: node -
Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 13, 2026

Coverage Delta

File Statements
packages/@sanity/cli/src/actions/init/initApp.ts 95.5% (new)
packages/@sanity/cli/src/actions/init/initHelpers.ts 100.0% (new)
packages/@sanity/cli/src/actions/init/initNextJs.ts 89.8% (new)
packages/@sanity/cli/src/actions/init/initStudio.ts 86.4% (new)
packages/@sanity/cli/src/actions/init/scaffoldTemplate.ts 78.6% (new)
packages/@sanity/cli/src/commands/init.ts 95.3% (+ 2.7%)

Comparing 6 changed files against main @ c5554452396209ab6fb3ade5445fbf7c8a35b876

Overall Coverage

Metric Coverage
Statements 83.0% (+ 0.0%)
Branches 73.0% (+ 0.0%)
Functions 82.9% (+ 0.0%)
Lines 83.5% (+ 0.0%)

Extract 9 private methods from the monolithic InitCommand class into
5 standalone modules under actions/init/, preparing the file structure
for the bundled-create branch to cleanly rebase on top.

New files:
- initHelpers.ts (shouldPrompt, flagOrDefault, getPostInitMCPPrompt, writeStagingEnvIfNeeded)
- scaffoldTemplate.ts (selectTemplate, scaffoldAndInstall)
- initNextJs.ts (initNextJs, writeOrOverwrite, writeSourceFiles)
- initApp.ts (initApp — app template flow)
- initStudio.ts (initStudio — studio template flow)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mttdnt mttdnt force-pushed the refactor/split-init-flows branch from 1e38824 to 27ef18e Compare April 13, 2026 17:16
@mttdnt mttdnt marked this pull request as ready for review April 15, 2026 17:25
@mttdnt mttdnt requested a review from a team as a code owner April 15, 2026 17:25
@mttdnt mttdnt requested review from binoy14 and gu-stav and removed request for a team April 15, 2026 17:25
@mttdnt mttdnt merged commit ca9d9d5 into main Apr 16, 2026
45 checks passed
@mttdnt mttdnt deleted the refactor/split-init-flows branch April 16, 2026 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants