Introducing a new way to review your PRs.

Read the announcement

solana-foundation / create-solana-dapp

The fastest way to create Solana apps 🚀 Templates 👉 https://github.com/solana-foundation/templates

View on GitHub

18 documents · Updated Nov 6, 2026

docs / initialization-scripts-and-customization

Initialization scripts and customization

Initialization scripts are template-provided rules that adapt a copied starter project to the user's name, tools, and selected features. The generator reads this contract from the create-solana-dapp entry in package.json, applies the requested changes, reports any follow-up work, and then removes the template-only metadata from the generated project.

Customization contract

Initialization script metadata

The initialization script is an optional object in the template's package.json. It describes project changes rather than serving as one shell command. Its fields cover tool checks, package-manager choice, name changes, feature options, user instructions, and optional agent skills.

FieldPurpose
versionsStates the required adb, anchor, or solana versions.
packageManagerRequires one supported package manager for the template.
renameReplaces template names in selected paths and in path names.
optionsDeclares feature flags with defaults, groups, renames, instructions, or a package script to run.
instructionsReturns plain-language steps for the user after customization.
skillsDeclares skills for later installation for AI coding agents.

The contract is read with the generated project's package data. This keeps template customization close to the template while allowing the generator to apply it to any target directory.

References: src/…/init-script-schema.ts• L4-17 src/…/init-script-schema.ts• L49-74 src/…/get-package-json.ts• L6-31

Tool and package-manager requirements

Version requirements give the user a compatibility warning when a required tool is missing or has the wrong version. The check does not stop project creation by itself. A template package-manager requirement is stricter: an explicit conflicting choice fails, and an unavailable required manager also fails.

The effective package manager then controls later package commands. If the generated package.json pins a different manager, the incompatible pin is removed before package work can use the selected manager. This prevents a stale template pin from breaking dependency setup.

References: src/…/init-script-version-check.ts• L8-45 src/…/init-script-package-manager.ts• L8-32 src/…/remove-incompatible-package-manager.ts• L5-35

Project transformation

Name replacement

Rename rules adapt both the text and the names of files and directories that belong to the template. Each rule names a source value, a to value, and one or more paths to search. The target value can use {{name}} to refer to the project name chosen by the user.

The generator also creates a project-wide rename from the template package name to the chosen project name. A rule for that same package name is skipped to avoid applying the same change twice. Other rules cover common name forms such as compact, kebab-case, PascalCase, snake_case, and display names. The canonical path field is in; the older paths field still works but produces a deprecation warning.

Template option renames join the base rename rules before the search begins. This lets a selected option replace its own markers in the same pass and gives the option scope precedence when it targets the same term.

References: src/…/init-script-rename.ts• L20-99 src/…/init-script-rename.ts• L101-149 src/…/init-script-rename.ts• L155-187 test/init-script-rename.test.ts• L57-190

File content replacement

Replacement is part of rename customization. It changes matching text in files and changes matching path names below the selected target. All substitutions for a file are made from its original content in one pass, so a replacement value cannot be processed again by a later rule.

The search applies the most specific path scope when scopes overlap. It processes file content before it renames files and directories. The process skips dependency, build, version-control, and temporary directories, skips symbolic links, and can report planned changes without writing them when dry-run mode is active.

This behavior lets a template use one name rule for both source references and project structure. A missing target path is reported and does not silently create a new target.

References: src/…/search-and-replace.ts• L4-16 src/…/search-and-replace.ts• L17-36 src/…/search-and-replace.ts• L52-83 src/…/search-and-replace.ts• L85-117 src/…/search-and-replace.ts• L152-220 test/search-and-replace.test.ts• L77-187

Template metadata deletion

After the generator applies the initialization rules, it removes the create-solana-dapp entry from the generated package.json. The result is a normal project package file rather than a project that still carries template setup instructions for a later run.

References: src/…/init-script-delete.ts• L7-14 src/…/create-app-task-run-init-script.ts• L34-48

Option-driven customization

Template options

Template options are named Boolean flags that let one template produce several related project variants. An option can be selected directly, enabled by default, or placed in a group where only one option may be active. An unknown flag fails before customization starts and lists the available flags when any exist.

Option metadata can add its own rename rules and instructions. The selected option scopes and messages are collected before the option package scripts run. This preserves the requested transformations even if a script later changes package.json.

References: src/…/init-script-schema.ts• L49-65 src/…/init-script-options.ts• L17-70 src/…/init-script-options.ts• L72-85 test/init-script-options.test.ts• L42-74

Option scripts

An option may name a package script to run after the template is copied. The generator checks that the script exists in package.json, runs it with the effective package manager, and runs it before dependency installation. This order lets an option change the dependency set before the lock file and installed modules are created.

Option definitions and their selected values are captured before option scripts run. A script that edits package.json therefore cannot remove the transformations or instructions already selected for the same generation.

References: src/…/init-script-schema.ts• L53-63 src/…/create-app-task-run-option-script.ts• L8-52 src/…/create-app.ts• L50-69 src/…/create-app-task-run-init-script.ts• L11-17

Generation lifecycle

The initialization flow connects template metadata to the generated project. The following sequence shows where the contract affects the wider project setup.

flowchart TD
    A[Copy template] --> B[Resolve package manager]
    B --> C[Clean incompatible package-manager pin]
    C --> D[Read and snapshot init metadata]
    D --> E[Resolve selected options]
    E --> F[Run option package scripts]
    F --> G[Install dependencies]
    G --> H[Run project setup]
    H --> I[Apply versions and renames]
    I --> J[Collect instructions]
    J --> K[Remove init metadata]
    K --> L[Return generated project]

The main initialization task checks versions, applies option and base renames, combines option and base instructions, substitutes the selected package manager in those instructions, and deletes the metadata. The separate option-script stage runs earlier so its package changes are present before dependency installation. The setup stage remains an optional project-level step.

The initialization task uses the snapshot of the script and selected options that was taken before option scripts run. This keeps the generation contract stable even when an option script rewrites package.json.

References: src/…/create-app.ts• L22-72 src/…/create-app-task-run-init-script.ts• L11-55 src/…/create-app-task-run-option-script.ts• L15-55 src/…/create-app-task-run-setup.ts• L7-31 test/create-app-task-run-init-script.test.ts• L56-72

Instruction output

Instructions are returned to the caller as follow-up messages instead of being executed by the generator. Base instructions and selected-option instructions are combined in option order, empty messages are removed, and {pm} is replaced with the effective package manager.

This output gives templates a safe way to tell the user about commands or manual steps that cannot be completed as part of file customization.

References: src/…/init-script-instructions.ts• L4-16 src/…/init-script-options.ts• L72-85 src/…/create-app-task-run-init-script.ts• L40-48

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the initialization-script schema, option flag resolution, and command-line option input aligned. A schema change can otherwise make a valid template flag fail at generation time.
  • Keep package-manager requirements aligned with package-manager pins in template package.json files. A mismatch can change the effective manager or remove the pin before install.
  • Keep rename target paths aligned with the template layout. A moved path can turn a valid customization into a reported missing target.
  • Keep the project package name and rename values aligned with the forms used in template content and paths. The generator replaces several name forms in one pass.
  • Keep option rename scopes compatible with base rename scopes. Overlapping scopes use the narrower target for the same term, while option scopes are applied after base scopes.
  • Keep option run names aligned with package.json scripts. The option can be selected successfully but fail when its required script is absent.
  • Keep instruction placeholders aligned with the values supplied at output time. {pm} is the supported package-manager substitution.

Important failure modes

TriggerConsequenceGuard
The template requires a package manager that is unavailable.Generation fails before package commands run.Check the required manager in the template contract and install it before using the template.
The user explicitly selects a manager that conflicts with the template requirement.Generation rejects the package-manager choice.Select the manager named by the template or use a template without that requirement.
A selected option is unknown or conflicts with another option in its group.Option resolution fails before project changes run.Use only declared flags and select at most one flag per group.
A rename target path does not exist in the copied template.That rename is skipped and an error is reported.Keep in paths matched to the template layout.
An option names a package script that is not in package.json.The option-script stage fails before dependency installation.Add the named script to the template or remove the run declaration.
A replacement term overlaps another replacement value.Sequential replacement can corrupt names or file paths.Use the shared scoped replacer, which applies matching substitutions to the original value in one pass.

Key files

FileWhy to read it
src/…/init-script-schema.tsDefines the template initialization-script contract.
src/…/create-app.tsShows where initialization metadata enters the project-generation lifecycle.
src/…/create-app-task-run-init-script.tsShows the high-level order for checks, transformations, instructions, and metadata cleanup.
src/…/init-script-options.tsExplains option defaults, groups, selection, and option-owned transformations.
src/…/init-script-rename.tsShows how project names become content and path replacements.
src/…/search-and-replace.tsShows how scoped content and path replacements avoid cascading changes.
src/…/create-app-task-run-option-script.tsShows how selected options invoke package scripts before installation.
src/…/remove-incompatible-package-manager.tsShows how an incompatible package-manager pin is removed.
src/…/init-script-version-check.tsShows how template tool requirements become user warnings.