by guo-yu
运行多个开发项目会导致端口冲突和混乱。端口分配器自动为每个项目分配唯一的端口范围。
1. 打开 Claude 聊天界面
2. 点击下方 "📋 复制" 按钮
3. 粘贴到 Claude 聊天框中并发送
4. 输入 "使用 port-allocator 技能" 开始使用
=== port-allocator 技能 === 作者: guo-yu 描述: 运行多个开发项目会导致端口冲突和混乱。端口分配器自动为每个项目分配唯一的端口范围。 使用方法: 1. 调用技能: "使用 port-allocator 技能" 2. 提供相关信息: 根据技能要求提供必要参数 3. 查看结果: 技能会返回处理结果 示例: "使用 port-allocator 技能,帮我分析一下这段代码"
这种方法适用于所有 Claude 用户,不需要安装额外工具。
devops
low
Smart port allocator that only assigns ports to real projects containing package.json.
| Command | Description |
|---|---|
/port-allocator | Allocate/query port for current project |
/port-allocator list | List all allocated ports |
/port-allocator scan | Scan code directory, discover and allocate ports for new projects |
/port-allocator config <path> | Set the main code directory path |
/port-allocator add <dir-path> | Manually add port allocation for a project |
/port-allocator allow | Configure Claude Code permissions for this skill's commands |
When restarting the development server, only kill processes within the current project's port range, never affect other ports:
# Correct: Only kill current project ports (e.g., 3000-3009)
lsof -ti:3000 | xargs kill -9 2>/dev/null
lsof -ti:3001 | xargs kill -9 2>/dev/null
# Wrong: Kill all node processes or other ports
pkill -f node # Will affect other projects!
lsof -ti:3010 | xargs kill # This is another project's port!
When updating ~/.claude/CLAUDE.md, must preserve the user's existing content:
# Correct: Check and append or update specific sections
# Wrong: Directly overwrite the entire file
/port-allocator allowConfigure Claude Code to allow commands used by this skill, avoiding manual confirmation each time:
~/.claude/settings.json (if exists)permissions.allow array (preserve existing config):{
"permissions": {
"allow": [
"Bash(ls -d *)",
"Bash(find * -maxdepth * -name package.json *)",
"Bash(cat ~/.claude/*)",
"Bash(dirname *)",
"Bash(lsof -i:3*)",
"Bash(lsof -ti:3*)"
]
}
}
Output Format:
Configured Claude Code permissions
Added allowed command patterns:
- Bash(ls -d *)
- Bash(find * -maxdepth * -name package.json *)
- Bash(cat ~/.claude/*)
- Bash(lsof -i:3*)
- Bash(lsof -ti:3*)
Config file: ~/.claude/settings.json
/port-allocator config <path>Set the user's main code directory:
code_root field in ~/.claude/port-registry.jsonOn first run (when ~/.claude/port-registry.json doesn't exist or has no code_root), automatically detect the code directory:
# Check common code directories
for dir in ~/Codes ~/Code ~/Projects ~/Dev ~/Development ~/repos; do
if [ -d "$dir" ]; then
CODE_ROOT="$dir"
break
fi
done
# If none exist, default to ~/Codes
CODE_ROOT="${CODE_ROOT:-~/Codes}"
Auto-detection output:
First run, detecting code directory...
Code directory detected: ~/Codes
Port registry initialized: ~/.claude/port-registry.json
To change, use:
/port-allocator config ~/your/code/path
If no directory found:
Could not auto-detect code directory.
Please configure manually:
/port-allocator config ~/your/code/path
Common locations:
~/Codes, ~/Code, ~/Projects, ~/Dev
/port-allocator scanScan code directory, automatically discover and register projects:
~/.claude/port-registry.json to get code_root
code_root directory doesn't exist, prompt user to configurepackage.json (exact to package.json location):# Find all package.json, exclude build artifact directories
find <code_root> -maxdepth 3 -name "package.json" -type f \
-not -path "*/.next/*" \
-not -path "*/node_modules/*" \
-not -path "*/dist/*" \
-not -path "*/build/*" | while read pkg; do
dirname "$pkg"
done
Important: Path must be exact to the directory containing package.json
~/Codes/chekusu/landing~/Codes/chekusu (if package.json is in subdirectory)For each discovered project directory:
Update config file (append mode, don't overwrite user content)
Output scan result summary
/port-allocator (default)Allocate/query port for current project:
code_root and allocated portspackage.json, indicate this is not a project needing ports/port-allocator listList all allocated ports (read-only operation).
Project directory: ~/Codes/chekusu/landing
package.json: Detected
Port range: 3000-3009
- Main app: 3000
- API: 3001
- Other services: 3002-3009
Warning: Only operate on ports 3000-3009 when restarting services!
Scan complete: ~/Codes
Registered projects (N):
- chekusu/landing: 3000-3009
- saifuri: 3010-3019
Newly discovered projects (M):
- new-project: 3090-3099 (newly allocated)
Skipped (K):
- .next, node_modules (build artifacts)
- research-folder (no package.json)
x0: Main application (e.g., 3000, 3010, 3020)x1: API service (e.g., 3001, 3011, 3021)x2-x9: Other services (database, cache, etc.)~/.claude/port-registry.json~/.claude/CLAUDE.md (append mode updates)~/.claude/settings.json (stores allowedCommands).next, node_modules, dist, build/port-allocator allow to configure permissions firstView Count
0
Download Count
0
Favorite Count
0
Quality Score
68