Commit ff59c19
fix(create-app): Fix critical CLI validation bugs and improve error handling (#34472)
## Summary
Fixes critical bugs and adds robust validation to the
`@dotcms/create-app` CLI tool. This PR addresses multiple issues
including nested directory creation, path display problems, exit code
inconsistencies, authentication failures, and improves overall error
handling with comprehensive pre-flight checks.
---
## Key Improvements & Bug Fixes
### 🐛 Critical Bug Fixes
#### 1. **Nested Directory Structure** (NEW)
**Issue:** Running CLI with directory path that includes project name
created nested structure
```bash
# Before
dotcms-create-app my-app -d /tmp/my-app
# Created: /tmp/my-app/my-app ❌
# After
# Creates: /tmp/my-app ✅
```
**Fix:** Detect when directory path already ends with project name and
avoid double-nesting
#### 2. **Directory Path Display** (NEW)
**Issue:** Confusing output with excessive parent directory traversals
```bash
# Before
cd ../../../../tmp/test-dir/my-project
# After
cd /tmp/test-dir/my-project # Cleaner absolute path
```
**Fix:** Smart path display logic - uses absolute paths when relative
would have 3+ parent traversals
#### 3. **Exit Code Consistency** (NEW)
**Issue:** CLI exited with code 0 on failures, breaking CI/CD detection
```bash
# 14+ failure scenarios were exiting with code 0
```
**Fix:** All error paths now use `process.exit(1)` for proper failure
signaling
#### 4. **Authentication Error Handling** (NEW)
**Issue:** Cryptic JSON dumps on wrong credentials, no retry mechanism
```bash
# Before: JSON error dump
# After: Clear message with 3-attempt retry
```
**Fix:**
- User-friendly error messages distinguishing 401 vs connection vs
server errors
- 3-attempt retry loop for authentication failures
- Users can correct credentials without restarting CLI
#### 5. **URL Trailing Slash Issues** (NEW)
**Issue:** URLs with trailing slashes caused double-slash API endpoints
```bash
https://demo.dotcms.com/ + /api/v1/... → https://demo.dotcms.com//api/v1/...
```
**Fix:** Normalize URLs by stripping trailing slashes before API calls
### 🐳 Docker & Port Pre-Flight Checks
**Before:** Generic error
```
✖ Failed to start dotCMS ensure docker is running and ports 8082, 8443, 9200, and 9600 are free.
```
**After:** Specific errors with solutions
- ✅ Check Docker availability → Installation instructions + download
link
- ✅ Check port conflicts → Lists busy ports with platform-specific
diagnostic commands (`lsof` / `netstat`)
- ✅ Container diagnostics → Shows status and logs when startup fails
### 🛡️ Input Validation
- **Project names:** Stricter validation - only alphanumeric, hyphens,
underscores, dots (no path traversal, reserved names)
- **Frameworks:** Supports aliases (`next` → `nextjs`) and
case-insensitive matching
- **URLs:** Protocol requirement (`https://` or `http://`), trailing
slash normalization
- **Usernames/Passwords:** Non-empty field validation
- **Parameters:** Warns when mixing `--local` with cloud flags (`--url`,
`-u`, `-p`)
### 🔧 Error Handling & Code Quality
- Added health check retry constants (Cloud: 5 attempts, Local: 60
attempts)
- Better variable naming (`healthCheckResult` vs
`checkIfDotcmsIsRunning`)
- Changed `console.log` → `console.error` for error outputs (proper
stream)
- Fixed typos throughout codebase
- Added comprehensive JSDoc comments
- Modular utility organization (`getDockerDiagnostics()` moved to utils)
### 🔒 Security
- Path traversal prevention in project names
- Shell injection protection with `escapeShellPath()` utility
- URL protocol validation (prevents file:// or javascript: schemes)
- Cross-platform command escaping (Windows/macOS/Linux)
---
## Detailed Changes by Category
### Authentication & Network (4 commits)
- ✅ Enhanced `getAuthToken()` API error handling
- ✅ 3-attempt authentication retry with user prompts
- ✅ URL trailing slash normalization
- ✅ Distinguish 401 vs connection vs 500 errors
### Validation (2 commits)
- ✅ Stricter project name validation (alphanumeric + `-_.` only)
- ✅ URL validation with protocol requirement
- ✅ Empty field validation for credentials
- ✅ Conflicting parameter detection
### Directory Handling (2 commits)
- ✅ Prevent nested directory structures
- ✅ Smart relative/absolute path display
### Infrastructure (3 commits)
- ✅ Docker availability checks with actionable errors
- ✅ Port conflict detection (8082, 8443, 9200, 9600)
- ✅ Container diagnostic utilities
- ✅ Exit code consistency (`process.exit(1)` on all failures)
---
## Files Changed
| File | Changes | Description |
|------|---------|-------------|
| `src/index.ts` | +60/-30 | Main CLI flow, authentication retry, path
display |
| `src/utils/index.ts` | +140/-10 | Docker checks, port validation, path
helpers |
| `src/utils/validation.ts` | +318 (new) | Comprehensive input
validation |
| `src/asks.ts` | +30/-10 | Directory preparation, nested structure fix
|
| `src/api/index.ts` | +20/-5 | Enhanced error handling |
| `src/constants/index.ts` | +5 | Health check retry constants |
**Total:** ~500 lines added/modified across 7 commits
---
## Testing
✅ Build passing (`nx run sdk-create-app:build`)
✅ Lint passing (`nx run sdk-create-app:lint`)
✅ All pre-commit hooks passing
✅ Manually tested: Docker checks, port conflicts, auth retries, path
display, nested directories
### Test Scenarios Validated
- ✅ Wrong credentials → retry 3 times with friendly messages
- ✅ Docker not running → clear installation instructions
- ✅ Ports busy → lists conflicts with diagnostic commands
- ✅ Directory path includes project name → no nesting
- ✅ Deep relative paths → shows absolute when cleaner
- ✅ All failures → exit with code 1 (not 0)
- ✅ URL trailing slashes → normalized correctly
- ✅ Invalid project names → clear validation errors
---
## Breaking Changes
None - all changes are additive and backward compatible.
---
## Related Issues
Fixes multiple critical bugs discovered during QA testing of
`@dotcms/create-app` CLI tool.
---------
Co-authored-by: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>1 parent 4b78768 commit ff59c19
7 files changed
Lines changed: 1204 additions & 192 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | | - | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
25 | | - | |
| 26 | + | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
| |||
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
68 | | - | |
69 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
70 | 76 | | |
71 | 77 | | |
72 | 78 | | |
| |||
88 | 94 | | |
89 | 95 | | |
90 | 96 | | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
| 6 | + | |
10 | 7 | | |
11 | 8 | | |
12 | 9 | | |
| |||
27 | 24 | | |
28 | 25 | | |
29 | 26 | | |
30 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
31 | 30 | | |
32 | | - | |
33 | 31 | | |
34 | 32 | | |
35 | 33 | | |
36 | | - | |
37 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
38 | 66 | | |
39 | 67 | | |
40 | 68 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
36 | 45 | | |
37 | 46 | | |
38 | 47 | | |
| |||
62 | 71 | | |
63 | 72 | | |
64 | 73 | | |
65 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
66 | 83 | | |
67 | 84 | | |
68 | 85 | | |
| |||
77 | 94 | | |
78 | 95 | | |
79 | 96 | | |
80 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
81 | 104 | | |
82 | 105 | | |
83 | 106 | | |
84 | 107 | | |
85 | 108 | | |
86 | 109 | | |
87 | | - | |
| 110 | + | |
88 | 111 | | |
89 | 112 | | |
90 | 113 | | |
| |||
93 | 116 | | |
94 | 117 | | |
95 | 118 | | |
96 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
97 | 126 | | |
98 | 127 | | |
99 | 128 | | |
| |||
124 | 153 | | |
125 | 154 | | |
126 | 155 | | |
127 | | - | |
| 156 | + | |
128 | 157 | | |
129 | 158 | | |
130 | 159 | | |
| |||
142 | 171 | | |
143 | 172 | | |
144 | 173 | | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
145 | 179 | | |
146 | 180 | | |
147 | | - | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
148 | 195 | | |
149 | 196 | | |
150 | 197 | | |
| |||
0 commit comments