anchor-init
v1.1.1
Published
Scaffold a custom Solana Anchor program in seconds
Maintainers
Readme
About
anchor-init is a command-line tool designed to quickly scaffold Solana Anchor programs with best practices. It generates a new project with a custom programId, sets up the proper directory structure for Anchor development, and provides an interactive setup experience to get you coding faster.
Features
- Fast project scaffolding with custom programId generation
- Handlebars template system for easy customization
- Automatic initial build process
- Interactive setup with customizable steps
- Optional Git repository initialization
- Smart project name validation and conversion
- One-c0mmand full setup with
--allflag - Enhanced error detection and helpful suggestions
Installation
From npm
npm install -g anchor-initFrom Source
- Clone the repository
git clone https://github.com/priyanshpatel18/anchor-init.git cd anchor-init - Install dependencies
npm install - Link globally
npm link
Usage
Basic Usage
anchor-init my_projectAdvanced Usage
Full automated setup
anchor-init my_project --all --gitAvailable Options
--all: Run all setup steps automatically (keys sync, build, install, deploy, test)--git: Initialize a Git repository automatically-h, --help: Display help information
What happens when you run anchor-init?
- Project Creation: Creates a new directory with your project name (converted to snake_case if needed)
- Program ID Generation: Generates a unique Solana program ID using a new keypair
- Template Processing: Copies and processes Handlebars templates with your project context
- Initial Build: Runs
anchor buildto ensure everything compiles - Interactive Setup: Prompts you to select additional steps to run:
anchor keys sync- Sync program keysanchor build- Build the programyarn install- Install dependenciesyarn deploy:local- Deploy to local validatoryarn test:local- Run tests
- Git Initialization: Optionally initialize a Git repository
Examples
Basic project creation
anchor-init cool_projectFull setup with Git
anchor-init my-awesome-program --all --gitManual step selection
anchor-init my_project
# Then select which steps to run from the interactive menuProject Structure
After running anchor-init, you'll get a complete Anchor project structure:
my_project/
├── programs/
│ └── my_project/
│ ├── src/
│ │ └── lib.rs
│ └── Cargo.toml
├── tests/
│ └── my_project.ts
├── migrations/
├── app/
├── Anchor.toml
├── .gitignore
├── Cargo.toml
├── package.json
└── tsconfig.jsonError Handling
anchor-init includes intelligent error detection:
- Connection Issues: Detects when Solana test validator isn't running
- WebSocket Errors: Identifies validator connectivity problems
- Test Issues: Helps troubleshoot missing test files
- Build Failures: Provides clear error messages and suggestions
Development
Local Testing
git clone https://github.com/priyanshpatel18/anchor-init.git
cd anchor-init
npm install
npm link
anchor-init test_projectTemplate Development
The tool uses Handlebars templates located in the templates/ directory. Available template variables:
{{projectName}}- Snake case project name{{programId}}- Generated program ID{{PascalCase projectName}}- Pascal case helper{{camelCase projectName}}- Camel case helper
Prerequisites
- Node.js (v14 or higher)
- Anchor CLI installed and configured
- Solana CLI tools
- Yarn package manager
Troubleshooting
Common Issues
"anchor build failed"
- Ensure Anchor CLI is properly installed
- Check that Rust toolchain is up to date
"Connection refused" during deploy
- Start the Solana test validator:
solana-test-validator - Or use:
anchor test-validator
"No test files found"
- Ensure test files exist in
tests/**/*.ts - Check that test files follow proper naming convention
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
MIT - see the LICENSE file for details.
