Initialization script execution
Initialization script execution adapts a copied template into the project the user requested. It selects feature options, runs any option and setup scripts at the right points, applies name changes, removes template-only metadata, and returns follow-up instructions to the caller.
Execution lifecycle
The application reads the initialization contract from the copied template after it resolves the effective package manager. It captures the contract and the selected options before any option script can change package.json, so later work uses one stable set of choices.
The main stages are:
- Copy the template and resolve package-manager rules.
- Read the initialization contract and validate the requested option flags.
- Run scripts owned by the selected options.
- Install dependencies.
- Run the optional project
setupscript. - Run the initialization task to check tools, apply changes, collect instructions, and remove the initialization contract.
The following diagram shows how option scripts affect dependency setup before the main initialization task completes the project changes.
flowchart TD
A[Copied template] --> B[Resolve package manager]
B --> C[Snapshot init contract and options]
C --> D[Run selected option scripts]
D --> E[Install dependencies]
E --> F[Run optional setup script]
F --> G[Check required tool versions]
G --> H[Apply selected-option renames]
H --> I[Apply project and base renames]
I --> J[Build user instructions]
J --> K[Delete init metadata]
The skip-init choice disables option scripts and the initialization task. The skip-install choice disables dependency installation and the setup stage, but it does not by itself disable the initialization task.
References: src/…/create-app.ts• L22-72 src/…/get-args-result.ts• L5-19 src/…/create-app-task-run-init-script.ts• L18-55
Selected option execution
Option selection
Selected options are the feature choices that control both project changes and optional commands. The resolver rejects unknown flags, adds default options unless a choice in the same group was requested, and rejects more than one option in a mutually exclusive group.
The resolved list is passed to both option-script execution and option transformation. This keeps a selected feature's command, rename rules, and user guidance tied to the same choice.
References: src/…/init-script-options.ts• L11-63 src/…/init-script-schema.ts• L49-65 src/…/create-app.ts• L50-55
Option script execution
An option script is a package script requested by a selected option. The generator checks that the named script exists, runs it with the effective package manager, and processes selected options in their resolved order.
Option scripts run before dependency installation. This allows a script to adjust the dependency set before the lock file and installed modules reflect the project. They also run before the main initialization task so files created or changed by an option script can receive the later rename rules.
If an option script fails or its package script is missing, the task reports an error and the generation flow does not continue as a successful project creation.
References: src/…/create-app-task-run-option-script.ts• L8-55 src/…/init-script-options.ts• L65-74 src/…/create-app.ts• L57-70
File transformation
Rename application
Rename rules turn template placeholders into project-specific names. A rule applies source values and target values within one or more target paths, and {{name}} resolves to the project name selected by the user.
The generator creates a project-wide rename from the template package name when one is available. It then combines declared rename entries with selected-option scopes. Selected-option scopes are applied last when scopes overlap, so a feature choice can override a base value without applying one replacement to the output of another.
Name changes cover common forms such as compact, kebab, Pascal, snake, and display names. The paths spelling remains supported with a deprecation warning; new contracts use in. A missing target path is reported and does not become an active rename scope.
References: src/…/init-script-rename.ts• L20-99 src/…/init-script-rename.ts• L101-152 src/…/init-script-rename.ts• L155-187 src/…/init-script-options.ts• L65-74
Search-and-replace scope
Search and replace updates matching text in files and matching names in the target tree. It combines all replacements for each file into one pass over the original value, so a new value that contains another search term is not changed again.
Scopes can cover the whole project, a directory, or one file. A narrower scope wins for the same search value. The walk replaces file content first, then renames files and directories using their original paths. This keeps project references and project structure aligned.
The search uses literal matches and skips build output, installed dependencies, version-control data, coverage data, temporary data, and symbolic links. A wider walk does not enter an excluded directory, but an explicit scope for a path inside that directory opts that path in. Dry-run mode reports planned file and path changes without writing them.
References: src/…/search-and-replace.ts• L4-40 src/…/search-and-replace.ts• L42-83 src/…/search-and-replace.ts• L85-117 src/…/search-and-replace.ts• L119-188 src/…/search-and-replace.ts• L191-228
Setup execution
The setup stage runs a template's optional package.json setup script after dependencies are installed. Templates use this stage for project work that needs installed tools, such as a build or client-generation step.
The stage uses the effective package manager and is skipped when installation is skipped. If the template has no setup script, the stage reports that there is no setup work rather than treating the absence as an error.
References: src/…/create-app-task-run-setup.ts• L7-31 src/…/create-app.ts• L59-69
Initialization task
The initialization task applies the contract that remains after option scripts and setup. It checks declared tool versions as warnings, applies selected-option and base rename rules, prepares the instruction list, and removes the template contract from the generated package.json.
The task uses the earlier snapshot of the initialization contract. This protects required renames and instructions when an option script rewrites package.json.
References: src/…/create-app-task-run-init-script.ts• L11-55 src/…/init-script-version.ts• L5-19 src/…/init-script-version-check.ts• L8-45 src/…/create-app.ts• L50-69
Metadata deletion
After the initialization work completes, the generator deletes the create-solana-dapp entry from the generated package data. This prevents template instructions from remaining as active metadata in the finished project.
The deletion is part of the initialization task after renames and instruction collection. A template with no initialization contract reports that no script was found and does not run these transformations.
References: src/…/init-script-delete.ts• L7-14 src/…/create-app-task-run-init-script.ts• L24-48 src/…/get-package-json.ts• L6-31
Instruction output
Instructions are user-facing follow-up messages returned by generation. The generator combines instructions from selected options with the base initialization instructions, removes empty entries, and replaces {pm} with the effective package manager.
These messages are not commands run by the generator. They describe work that remains for the user after automatic setup, such as a command or a manual step. The completed application returns them with the other task results.
References: src/…/init-script-instructions.ts• L4-16 src/…/init-script-options.ts• L65-74 src/…/create-app-task-run-init-script.ts• L36-48 src/…/create-app.ts• L22-73
Related pages
- Initialization scripts and customization gives the broad model for template-provided customization.
- Initialization script configuration explains the contract fields and validation rules.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep option resolution and option-script selection aligned. A flag can resolve correctly but fail later if its
runscript is not present in package.json. - Preserve the snapshot boundary before option scripts. Reading the contract again after a script changes package.json can lose required renames or instructions.
- Keep the order of option scripts, dependency installation, setup, and initialization changes aligned with dependency and file-content needs.
- Keep
{pm}instruction substitution aligned with the package-manager value used by setup and option scripts. - Keep rename target paths aligned with files created or changed by option scripts. A path that moves outside the declared scope is skipped.
- Keep selected-option scopes after base scopes when the selected value must win. The replacement pass must still read each file's original content.
- Treat initialization metadata deletion as part of the finalization contract. Moving it earlier can remove data needed by later stages.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A requested option is unknown or conflicts with another option in its group. | Option resolution stops before option scripts run. | Use only declared flags and select one option per group. |
| A selected option names a missing package script. | Option execution fails before dependency installation. | Keep each option run value matched to a script in package.json. |
| An option script rewrites package.json before the initialization task. | A fresh read could lose the contract data needed for renames or instructions. | Use the contract and option snapshot captured before option scripts. |
| A rename path is absent from the copied template. | That rename is skipped and an error is reported. | Keep rename paths matched to the template and to files created by option scripts. |
| A replacement value contains a search term from the same rename set. | Sequential replacement can change the new value a second time. | Apply all matching substitutions in one pass over the original value. |
| A setup script fails after dependency installation. | Generation stops during project setup. | Keep setup work valid for the selected package manager and installed dependency set. |
skip-init is used when option changes are required. | Option scripts and initialization transformations do not run. | Use skip-init only when the template needs no initialization work. |
Key files
| File | Why to read it |
|---|---|
| src/…/create-app.ts | Shows the full creation order and the snapshot boundary. |
| src/…/create-app-task-run-init-script.ts | Shows the final initialization task and its transformation order. |
| src/…/init-script-options.ts | Shows option selection and option-owned transformations and instructions. |
| src/…/create-app-task-run-option-script.ts | Shows how selected options run package scripts before installation. |
| src/…/create-app-task-run-setup.ts | Shows the optional setup stage after dependency installation. |
| src/…/init-script-rename.ts | Shows how project names and selected options become content and path changes. |
| src/…/search-and-replace.ts | Shows scope precedence, one-pass replacement, exclusions, and path renaming. |
| src/…/init-script-delete.ts | Shows how template-only initialization metadata is removed. |
| src/…/init-script-instructions.ts | Shows how follow-up messages are collected for the caller. |
| src/…/init-script-schema.ts | Defines the initialization and option data used by the execution flow. |