Skip to content

feat(init): support sanity-template.json with postInitMessage#771

Open
hermanwikner wants to merge 1 commit intomainfrom
crx-2061
Open

feat(init): support sanity-template.json with postInitMessage#771
hermanwikner wants to merge 1 commit intomainfrom
crx-2061

Conversation

@hermanwikner
Copy link
Copy Markdown
Member

@hermanwikner hermanwikner commented Mar 24, 2026

Description

This pull request lets template authors include an optional sanity-template.json as part of the template, with a postInitMessage that the CLI shows after the template has been successfully set up. This allows template authors to highlight next steps, links, commands or anything else they want people to see at the end of init – maintained in the template itself, so copy tweaks don't require a new CLI release.

Example flow

  1. Given a template that contains sanity-template.json at the project root, e.g.:

    {
      "postInitMessage": "POST MESSAGE"
    }
  2. When the user bootstraps that template, e.g:

    sanity init --template <your-template-ref> --output-path /path/to/project
  3. Then they see the usual success output from init and after that the postInitMessage appears, e.g:

    Success! Your Studio has been created.
    
    (cd /path/to/project to navigate to your new project directory)
    
    Get started by running pnpm dev to launch your Studio's development server
    
    Other helpful commands:
    npx sanity docs browse     to open the documentation in a browser
    npx sanity manage          to open the project settings in a browser
    npx sanity help            to explore the CLI manual
    
    Message from the template author:
    
    POST MESSAGE
    

What to review

  • From the repo root, after a local build, run sanity init against a template branch that includes sanity-template.json with postInitMessage – e.g. this branch of the Next.js clean starter where that file was added:

    node packages/@sanity/cli/bin/run.js init \
      --template https://github.com/sanity-io/sanity-template-nextjs-clean/tree/herman-test \
      --output-path /tmp/test-template
  • Confirm the postInitMessage appears after the usual success output

@claude
Copy link
Copy Markdown

claude Bot commented Mar 24, 2026

Claude finished @hermanwikner's task in 1m 14s —— View job


PR Review

  • Gather context
  • Read changed files
  • Provide review feedback

Issues

1. sanity-template.json not removed when manifest exists but fails schema validation

removeTemplateManifestFromOutput is only called when templateManifest is truthy (init.ts:737). If the file exists but fails schema validation (e.g. postInitMessage: 42), readTemplateManifest returns null, and the file is left in the user's project directory. The cleanup should be unconditional — removeTemplateManifestFromOutput already swallows errors if the file doesn't exist.

// current
if (templateManifest) {
  await removeTemplateManifestFromOutput(outputPath)
}

// fix: always attempt cleanup, regardless of parse result
await removeTemplateManifestFromOutput(outputPath)

To support this, readTemplateManifest would need to indicate "file existed but was invalid" vs "file didn't exist" — or simply always call removeTemplateManifestFromOutput unconditionally (it's already safe to do so). Fix this →


2. Inconsistent size limits in the schema

In types.ts:23:

z.union([z.string().max(2000), z.array(z.string().max(500)).max(50)])

String form caps at 2,000 chars, but array form allows up to 50 × 500 = 25,000 chars. A template author using the array form can display 12× more text, bypassing the intended limit. Consider either making the total-character limit consistent across both forms, or adding an overall character budget to the array branch. Fix this →

@hermanwikner hermanwikner changed the title feat(init): support sanity-template.json with postInitMessage [WIP] feat(init): support sanity-template.json with postInitMessage [WIP] Mar 24, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 24, 2026

Coverage Delta

File Statements
packages/@sanity/cli/src/actions/init/constants.ts 100.0% (±0%)
packages/@sanity/cli/src/actions/init/getPostInitMessageDisplay.ts 100.0% (new)
packages/@sanity/cli/src/actions/init/readTemplateManifest.ts 100.0% (new)
packages/@sanity/cli/src/actions/init/types.ts 100.0% (new)
packages/@sanity/cli/src/commands/init.ts 91.5% (- 1.1%)

Comparing 5 changed files against main @ 133be82f2a246f5cc1573c8e2733d50a2b2ea746

Overall Coverage

Metric Coverage
Statements 83.0% (±0%)
Branches 72.7% (+ 0.0%)
Functions 83.3% (+ 0.0%)
Lines 83.5% (±0%)

@hermanwikner hermanwikner changed the title feat(init): support sanity-template.json with postInitMessage [WIP] feat(init): support sanity-template.json with postInitMessage Mar 25, 2026
@hermanwikner hermanwikner marked this pull request as ready for review March 27, 2026 09:04
@hermanwikner hermanwikner requested a review from a team as a code owner March 27, 2026 09:04
@hermanwikner hermanwikner requested review from binoy14 and cngonzalez and removed request for a team March 27, 2026 09:04
}

if (templateManifest) {
await removeTemplateManifestFromOutput(outputPath)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Saw claude mentioned that if a bad template manifest or the read fails we do not clean it up? Is it worth cleaning up here regardless or even in readTemplateManifest.ts?

Copy link
Copy Markdown
Contributor

@mttdnt mttdnt left a comment

Choose a reason for hiding this comment

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

Looks good! Just have one question and a change needed due to us changing zod to zod/mini

}

export const templateManifestSchema = z.object({
postInitMessage: z.union([z.string().max(2000), z.array(z.string().max(500)).max(50)]).optional(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Will need an update to zod/mini per #847

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