@adamhancock/devctl2

中文文档
1.1.4发布于 2026/08/31MITTypeScriptES Module构建来源已验证近 30 天下载 2319

Generic development environment manager for multi-worktree projects with configurable apps, Caddy, database isolation, and port management

原始使用文档
文档目录36 节
  1. Features
  2. Installation
  3. Local Development
  4. From npm (once published)
  5. Quick Start
  6. Configuration
  7. Example Configuration
  8. Configuration Options
  9. Commands
  10. devctl setup [name]
  11. devctl list / devctl ls
  12. devctl remove <subdomain> / devctl rm <subdomain>
  13. devctl ports <subdomain>
  14. devctl init [project-name]
  15. Database Commands
  16. devctl doctor
  17. Workflow Example
  18. Setting up a new feature branch
  19. Working with databases
  20. Cleaning up
  21. Requirements
  22. Caddy Setup
  23. PostgreSQL Setup
  24. How It Works
  25. Port Allocation
  26. Database Isolation
  27. Caddy Routing
  28. Troubleshooting
  29. Caddy is not running
  30. PostgreSQL tools not found
  31. Port conflicts
  32. Database connection errors
  33. Routes not working
  34. Development
  35. License
  36. Contributing

devctl

A powerful development environment manager for multi-worktree projects. Simplifies the management of development environments by automatically handling:

  • Caddy reverse proxy routes for subdomain-based worktrees
  • Database isolation with automatic PostgreSQL database creation per worktree
  • Port allocation using deterministic hashing to avoid conflicts
  • Environment file management with automatic .env updates
  • MCP integration for Claude Code and Spotlight

Features

  • 🌐 Automatic Caddy route setup with subdomain or root domain support
  • 🗄️ PostgreSQL database creation from templates
  • 🔢 Deterministic port allocation based on worktree path
  • 📝 Automatic .env file updates across all packages
  • 🔄 Database backup and restore functionality
  • 🔍 Spotlight integration for debugging
  • 🤖 MCP (Model Context Protocol) configuration
  • 🏥 Built-in health checks and diagnostics

Installation

Local Development

# Install dependencies
pnpm install

# Build the package
pnpm build

# Link globally for local testing
npm link

From npm (once published)

npm install -g devctl
# or
pnpm add -g devctl

Quick Start

  1. Initialize configuration in your project:
cd /path/to/your/project
devctl init my-project
  1. Edit .devctlrc.json to match your project structure

  2. Setup a worktree:

cd /path/to/your/worktree
devctl setup

Configuration

Create a .devctlrc.json file in your project root. You can use devctl init [project-name] to generate a starter config.

Example Configuration

{
  "projectName": "my-project",
  "baseDomain": "dev.local",
  "databasePrefix": "myproject",
  "caddyApi": "http://localhost:2019",
  "portRanges": {
    "api": {
      "start": 3000,
      "count": 1000
    },
    "web": {
      "start": 5000,
      "count": 1000
    },
    "spotlight": {
      "start": 7000,
      "count": 1000
    }
  },
  "envFiles": {
    "api": "packages/api/.env",
    "web": "packages/web/.env",
    "spotlight": "packages/spotlight/.env",
    "e2e": "packages/e2e/.env"
  },
  "database": {
    "host": "localhost",
    "port": 5432,
    "user": "postgres",
    "password": "",
    "templateDb": "myproject_dev"
  },
  "features": {
    "database": true,
    "spotlight": true,
    "queuePrefix": true
  },
  "integrations": {
    "mcp": true,
    "spotlight": true
  }
}

Configuration Options

Core Settings

  • projectName (required): Your project name, used in various identifiers
  • baseDomain (required): Base domain for routing (e.g., dev.local)
  • databasePrefix (required): Prefix for database names
  • caddyApi: URL for Caddy Admin API (default: http://localhost:2019)

Port Ranges

Define port ranges for each service. Ports are deterministically allocated based on worktree path:

"portRanges": {
  "api": { "start": 3000, "count": 1000 },
  "web": { "start": 5000, "count": 1000 },
  "spotlight": { "start": 7000, "count": 1000 }
}

Environment Files

Specify paths to .env files relative to project root:

"envFiles": {
  "api": "packages/api/.env",
  "web": "packages/web/.env",
  "spotlight": "packages/spotlight/.env",
  "e2e": "packages/e2e/.env"
}

Database Configuration

"database": {
  "host": "localhost",
  "port": 5432,
  "user": "postgres",
  "password": "",
  "templateDb": "myproject_dev"
}
  • templateDb: Template database to clone for new worktrees

Features

Enable or disable features:

"features": {
  "database": true,      // PostgreSQL database per worktree
  "spotlight": true,     // Spotlight debugging integration
  "queuePrefix": true    // BullMQ queue prefix per worktree
}

Integrations

"integrations": {
  "mcp": true,          // MCP configuration updates
  "spotlight": true     // Spotlight MCP server
}

Commands

devctl setup [name]

Setup a worktree with ports, database, and Caddy routes.

# Use current branch name
devctl setup

# Use custom subdomain
devctl setup my-feature

# Use root domain instead of subdomain
devctl setup --root-domain

# Use custom config file
devctl setup -c /path/to/.devctlrc.json

What it does:

  1. Generates unique ports based on worktree path
  2. Creates a PostgreSQL database from template (if enabled)
  3. Updates .env files with ports and database URL
  4. Configures Caddy reverse proxy routes
  5. Updates .mcp.json for Claude Code integration
  6. Runs database migrations

devctl list / devctl ls

List all active Caddy routes with their ports and worktree paths.

devctl list

devctl remove <subdomain> / devctl rm <subdomain>

Remove a Caddy route for a specific subdomain.

devctl remove my-feature

devctl ports <subdomain>

Get port information for a specific subdomain.

devctl ports my-feature

devctl init [project-name]

Create a .devctlrc.json configuration file in the current directory.

devctl init my-project

# Overwrite existing config
devctl init my-project --force

Database Commands

devctl dump [database-name]

Dump a database to SQL file.

# Dump current worktree's database
devctl dump

# Dump specific database
devctl dump myproject_feature_branch

# Specify output file
devctl dump -o /path/to/backup.sql

devctl restore <dump-file> [database-name]

Restore a database from SQL dump file.

# Restore to current worktree's database
devctl restore backup.sql

# Restore to specific database
devctl restore backup.sql myproject_feature_branch

devctl list-dumps

List available database dump files.

devctl list-dumps

devctl doctor

Check environment and dependencies.

devctl doctor

Checks:

  • Configuration file
  • Git repository status
  • Caddy availability
  • PostgreSQL tools

Workflow Example

Setting up a new feature branch

# Create a new worktree
git worktree add ../my-feature feature/my-feature
cd ../my-feature

# Setup development environment
devctl setup

# Start your services
pnpm dev

Your feature branch is now accessible at https://my-feature.dev.local with:

  • Isolated PostgreSQL database
  • Unique ports that don't conflict
  • Caddy routing configured
  • All .env files updated

Working with databases

# Dump your main database for use in feature branches
cd /path/to/main-worktree
devctl dump

# In your feature worktree, restore from main
cd /path/to/feature-worktree
devctl restore myproject_main-2024-01-15T10-30-00.sql

Cleaning up

# Remove Caddy route when done
devctl remove my-feature

# Remove worktree
git worktree remove ../my-feature

# Optionally drop the database
dropdb myproject_my_feature

Requirements

  • Node.js: v18 or higher
  • Caddy: v2 with Admin API enabled
  • PostgreSQL: For database features (optional)
  • Git: For worktree detection

Caddy Setup

Your Caddyfile should enable the Admin API:

{
  admin localhost:2019
}

Or run Caddy with API enabled:

caddy run --config Caddyfile

PostgreSQL Setup

For database features, ensure PostgreSQL is installed:

# macOS
brew install postgresql

# Or use Docker
docker run -d \
  --name postgres \
  -e POSTGRES_PASSWORD=postgres \
  -p 5432:5432 \
  pgvector/pgvector:pg16

How It Works

Port Allocation

Ports are deterministically generated using MD5 hashing of the worktree path:

  1. Hash the worktree absolute path
  2. Convert first 4 hex characters to offset
  3. Add offset to configured port range start
  4. Check availability and try next offset if occupied

This ensures:

  • Same worktree always gets same ports (if available)
  • Different worktrees get different ports
  • No manual port management needed

Database Isolation

Each worktree gets its own PostgreSQL database:

  1. Creates database name: {prefix}_{sanitized_branch_name}
  2. Clones from template database using CREATE DATABASE ... TEMPLATE
  3. Runs migrations automatically
  4. Updates all .env files with new DATABASE_URL

Caddy Routing

Automatically configures reverse proxy routes:

https://feature-branch.dev.local/api/*  → localhost:3042 (API)
https://feature-branch.dev.local/*      → localhost:5042 (Web)
https://feature-branch.dev.local/_spotlight → localhost:7042 (Spotlight)

Routes include custom headers with worktree metadata for debugging.

Troubleshooting

Caddy is not running

# Check if Caddy is running
curl http://localhost:2019/config/

# Start Caddy
caddy run --config Caddyfile

PostgreSQL tools not found

# macOS - Install PostgreSQL
brew install postgresql

# Or use the bundled tools from Postgres.app
export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"

Port conflicts

If ports are already in use, devctl will try up to 100 different offsets. If all fail:

  1. Check what's using the ports: lsof -i :PORT
  2. Adjust your port ranges in .devctlrc.json
  3. Kill conflicting processes

Database connection errors

# Check PostgreSQL is running
psql -U postgres -h localhost -d postgres

# Verify template database exists
psql -U postgres -h localhost -d postgres -c "\l"

Routes not working

# Check Caddy configuration
curl http://localhost:2019/config/apps/http/servers/srv0/routes | jq

# List all routes
devctl list

# Run diagnostics
devctl doctor

Development

# Install dependencies
pnpm install

# Build TypeScript
pnpm build

# Watch mode for development
pnpm build --watch

# Link for local testing
npm link

# Test in a project
cd /path/to/test/project
devctl setup

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

版本与发布记录

@adamhancock/devctl2 共发布 9 个版本,首次发布于 2026/01/28;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.4latest2026/08/312 个月前
1.1.32026/08/092 个月前
1.1.22026/08/092 个月前
1.1.12026/04/286 个月前
1.1.02026/04/286 个月前
1.0.72026/03/177 个月前

关于 @adamhancock/devctl2 的常见问题

@adamhancock/devctl2 是什么?

Generic development environment manager for multi-worktree projects with configurable apps, Caddy, database isolation, and port management

如何安装 @adamhancock/devctl2?

在项目目录运行 npm install @adamhancock/devctl2,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 devctl2,可以用 npx devctl2 直接运行。

@adamhancock/devctl2 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adamhancock/devctl2 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@adamhancock/devctl2 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@adamhancock/devctl2 可以商用吗?

@adamhancock/devctl2 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@adamhancock/devctl2 的下载量有多少?

根据 npm 官方下载统计,@adamhancock/devctl2 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,319 次,日均约 77 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adamhancock/devctl2 还在维护吗?

最新版本 1.1.4 发布于 2026/08/31(2 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 9 个;首次发布于 2026/01/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。