Skip to content

refactor(init): split init flows into separate functions#753

Open
rexxars wants to merge 5 commits intorefactor/extract-init-cmd-to-actionfrom
refactor/split-init-flows
Open

refactor(init): split init flows into separate functions#753
rexxars wants to merge 5 commits intorefactor/extract-init-cmd-to-actionfrom
refactor/split-init-flows

Conversation

@rexxars
Copy link
Member

@rexxars rexxars commented Mar 20, 2026

Note

This is part 2 of a larger init refactor. See #748 for part 1.

Makes it easier to reason about and make changes to each individual init flow.

rexxars and others added 4 commits March 19, 2026 23:06
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move doInitNextJs (renamed to initNextJs), writeOrOverwrite, and
writeSourceFiles into a dedicated initNextJs.ts file. Shared helpers
(shouldPrompt, flagOrDefault, getPostInitMCPPrompt, writeStagingEnvIfNeeded)
are moved to initHelpers.ts to avoid circular dependencies between the
orchestrator and the Next.js flow module.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@claude
Copy link

claude bot commented Mar 20, 2026

Claude finished @rexxars's task in 3m 11s —— View job


Review

  • Read changed files
  • Review diff
  • Post findings

scaffoldTemplate.ts:61-70 — Studio-specific fields required in shared interface

ScaffoldOptions declares projectId: string, datasetName: string, and displayName: string as required, even though they're meaningless for the app flow. This forces initApp to pass empty strings with a comment to explain why. Consider making them optional (projectId?: string, etc.) and having bootstrapTemplate handle undefined (it already falls back via displayName || defaults.projectName).

Fix this →


initNextJs.ts:248-350writeOrOverwrite / writeSourceFiles are private to this file but duplicate the pattern from initAction.ts

These helpers were moved verbatim from the old monolithic file — no issue there — but writeOrOverwrite silently swallows mkdir errors (catch { debug(...) }) without any fallback. If directory creation fails and the file write then fails, the error from writeFile will be thrown with no context about the directory creation failure. Same behavior as before, but worth noting since it's now more visible in isolation.


No other behavioral differences found. The refactoring is functionally equivalent to the original code.

…d scaffolding

Extract the shared template selection and scaffolding pipeline into
scaffoldTemplate.ts (selectTemplate + scaffoldAndInstall), create a
dedicated initApp.ts for the app template flow, and slim initStudio.ts
down to studio-only logic. The orchestrator in initAction.ts now
branches on isAppTemplate before calling the appropriate flow function.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@rexxars rexxars force-pushed the refactor/split-init-flows branch from d819061 to e738e9f Compare March 20, 2026 20:19
@github-actions
Copy link

Coverage Delta

File Statements
packages/@sanity/cli/src/actions/init/bootstrapTemplate.ts 0.0% (±0%)
packages/@sanity/cli/src/actions/init/initAction.ts 94.8% (new)
packages/@sanity/cli/src/actions/init/initApp.ts 96.0% (new)
packages/@sanity/cli/src/actions/init/initError.ts 100.0% (new)
packages/@sanity/cli/src/actions/init/initHelpers.ts 100.0% (new)
packages/@sanity/cli/src/actions/init/initNextJs.ts 86.7% (new)
packages/@sanity/cli/src/actions/init/initStudio.ts 87.0% (new)
packages/@sanity/cli/src/actions/init/scaffoldTemplate.ts 88.0% (new)
packages/@sanity/cli/src/actions/init/types.ts 100.0% (new)
packages/@sanity/cli/src/commands/init.ts 100.0% (+ 7.4%)
packages/@sanity/cli/src/telemetry/init.telemetry.ts 100.0% (±0%)

Comparing 11 changed files against main @ 7af617a3f19983756f93edca8f939e19bdbb4d2b

Overall Coverage

Metric Coverage
Statements 81.7% (- 1.3%)
Branches 70.7% (- 1.9%)
Functions 80.1% (- 3.1%)
Lines 82.1% (- 1.3%)

@rexxars rexxars marked this pull request as ready for review March 21, 2026 17:47
@rexxars rexxars requested a review from a team as a code owner March 21, 2026 17:47
@rexxars rexxars requested review from binoy14, joshuaellis and mttdnt and removed request for a team and joshuaellis March 21, 2026 17:47
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.

1 participant