sap-cap-debugger
Version:
NPX tool for remote debugging SAP CAP applications on Cloud Foundry (CAP v7 and earlier)
402 lines (301 loc) โข 10.2 kB
Markdown
# SAP CAP Remote Debugger
[](https://badge.fury.io/js/sap-cap-debugger)
[](https://www.npmjs.com/package/sap-cap-debugger)
[](https://opensource.org/licenses/MIT)
[](https://nodejs.org/)
> **NPX tool for remote debugging SAP CAP applications on Cloud Foundry (CAP v7 and earlier)**
A comprehensive, production-ready solution for debugging SAP Cloud Application Programming (CAP) applications deployed on SAP Business Technology Platform (BTP) Cloud Foundry. This tool provides an easy-to-use NPX command that handles all the complexity of setting up remote debugging for CAP applications where the built-in `cds debug` command is not available.
## ๐ Quick Start
```bash
# Install and run in one command
npx sap-cap-debugger my-cap-app
# Or install globally
npm install -g sap-cap-debugger
cds-debug my-cap-app
```
## โจ Features
- **๐ฏ One-Command Setup**: Complete remote debugging setup with a single command
- **๐ง Multiple Debuggers**: Support for Chrome DevTools, VS Code, or both
- **๐ฑ Interactive Mode**: Interactive app selection when no app name is provided
- **๐งน Auto Cleanup**: Comprehensive cleanup of debugging sessions
- **โก Professional**: Production-ready with proper error handling and logging
- **๐ Status Monitoring**: Real-time status checking and monitoring
- **๐ ๏ธ Manual Fallback**: Manual step-by-step instructions when automation fails
## ๐ Prerequisites
- **Node.js** 14.0.0 or higher
- **Cloud Foundry CLI** installed and configured
- **SAP CAP application** deployed on Cloud Foundry
- **SSH access** enabled for your Cloud Foundry space
## ๐ ๏ธ Installation
### Option 1: NPX (Recommended)
```bash
# Run directly without installation
npx sap-cap-debugger setup my-app-name
```
### Option 2: Global Installation
```bash
# Install globally
npm install -g sap-cap-debugger
# Use the command
cds-debug my-app-name
# or
npx sap-cap-debugger my-app-name
```
### Option 3: Local Installation
```bash
# Install in your project
npm install --save-dev sap-cap-debugger
# Use with npx
npx sap-cap-debugger my-app-name
# or use the global command
cds-debug my-app-name
```
## ๐ Usage
### Basic Commands
```bash
# Debug an application (interactive app selection if no name provided)
npx sap-cap-debugger
cds-debug
# Debug specific application
npx sap-cap-debugger my-cap-app
cds-debug my-cap-app
# Debug with custom port
npx sap-cap-debugger my-app --port 9230
cds-debug my-app --port 9230
# Debug with VS Code debugger
npx sap-cap-debugger my-app --debugger vscode
cds-debug my-app --debugger vscode
# Debug with both Chrome and VS Code
npx sap-cap-debugger my-app --debugger both
cds-debug my-app --debugger both
# Clean up debugging session
npx sap-cap-debugger cleanup
cds-debug cleanup
# Check debugging status
npx sap-cap-debugger status
cds-debug status
# List available applications
npx sap-cap-debugger apps
cds-debug apps
# Show manual debugging steps
npx sap-cap-debugger manual my-cap-app
cds-debug manual my-cap-app
```
### Command Options
| Option | Description | Default | Values |
|--------|-------------|---------|--------|
| `--port, -p` | Debug port number | `9229` | Any available port |
| `--debugger, -d` | Debugger type | `chrome` | `chrome`, `vscode`, `both` |
| `--verbose, -v` | Enable verbose logging | `false` | `true`, `false` |
### Examples
```bash
# Debug with Chrome DevTools
npx sap-cap-debugger collaboration-srv
# or
cds-debug collaboration-srv
# Debug with VS Code
npx sap-cap-debugger collaboration-srv --debugger vscode
# or
cds-debug collaboration-srv --debugger vscode
# Debug with both debuggers
npx sap-cap-debugger collaboration-srv --debugger both
# or
cds-debug collaboration-srv --debugger both
# Debug with custom port
npx sap-cap-debugger collaboration-srv --port 9230
# or
cds-debug collaboration-srv --port 9230
# Verbose output
npx sap-cap-debugger collaboration-srv --verbose
# or
cds-debug collaboration-srv --verbose
# Interactive app selection
npx sap-cap-debugger
# or
cds-debug
```
## ๐ง VS Code Configuration
For VS Code debugging, ensure your `.vscode/launch.json` contains:
```json
{
"version": "1.0.0",
"configurations": [
{
"name": "Debug CAP App Remote",
"type": "node",
"request": "attach",
"address": "127.0.0.1",
"port": 9229,
"restart": true,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/home/vcap/app",
"skipFiles": [
"<node_internals>/**"
]
}
]
}
```
## ๐ฏ How It Works
The tool uses the **`kill -USR1`** approach from SAP Community blogs, which enables the Node.js inspector on an already running process. This bypasses networking issues that occur with direct SSH tunneling.
### Process Flow
1. **Prerequisites Check**: Verifies CF CLI and required tools
2. **App Verification**: Checks app status and starts if needed
3. **SSH Setup**: Enables SSH access for the application
4. **Process Start**: Starts the CAP application normally
5. **Process Detection**: Finds the Node.js process PID
6. **Tunnel Creation**: Creates SSH tunnel for debugging port
7. **Debug Enablement**: Enables debugging with `kill -USR1`
8. **Debugger Launch**: Opens Chrome DevTools or VS Code
9. **Session Management**: Tracks and manages the debugging session
## ๐จ Troubleshooting
### Common Issues
#### SSH Tunnel Connection Refused
```bash
# Check if the application is running
npx sap-cap-debugger status
# Check application logs
cf logs my-app-name --recent
# Try the setup again
npx sap-cap-debugger setup my-app-name
```
#### Application Not Starting
```bash
# Check application logs
cf logs my-app-name --recent
# Verify Node.js path
cf ssh my-app-name -c "which node"
# Check application structure
cf ssh my-app-name -c "ls -la /home/vcap/app"
```
#### Debugger Not Connecting
```bash
# Verify SSH tunnel
npx sap-cap-debugger status
# Check if process is still running
cf ssh my-app-name -c "ps aux | grep node"
# Clean up and retry
npx sap-cap-debugger cleanup
# or
cds-debug cleanup
npx sap-cap-debugger setup my-app-name
```
### Manual Debugging Steps
If the automated setup fails, use the manual command:
```bash
npx sap-cap-debugger manual my-app-name
```
This will show step-by-step manual instructions.
## ๐ Status Monitoring
```bash
# Check current debugging status
npx sap-cap-debugger status
# List available applications
npx sap-cap-debugger apps
```
## ๐งน Cleanup
```bash
# Clean up debugging session
npx sap-cap-debugger cleanup
# or
cds-debug cleanup
```
This will:
- Kill SSH tunnel processes
- Free up debugging ports
- Clean up any remaining processes
- Reset the debugging environment
## ๐๏ธ Development
### Building from Source
```bash
# Clone the repository
git clone https://github.com/HatriGt/sap-cap-debugger.git
cd sap-cap-debugger
# Install dependencies
bun install
# Build the project
bun run build
# Run tests
bun test
# Run linting
bun run lint
```
### Project Structure
```
src/
โโโ bin/
โ โโโ cap-debug.ts # CLI entry point
โโโ lib/
โ โโโ cap-debugger.ts # Main debugger class
โ โโโ cloudfoundry.ts # CF operations
โ โโโ port-manager.ts # Port management
โ โโโ ssh-tunnel.ts # SSH tunnel management
โ โโโ debugger-launcher.ts # Debugger launching
โโโ utils/
โ โโโ logger.ts # Logging utilities
โ โโโ command.ts # Command execution
โโโ types/
โ โโโ index.ts # TypeScript types
โโโ __tests__/ # Test files
```
### Publishing to NPM
```bash
# Build the project
bun run build
# Run tests
bun test
# Publish to NPM
npm publish
```
## ๐ค Contributing
We welcome contributions! Here's how to get started:
### Development Setup
```bash
# Fork and clone the repository
git clone https://github.com/HatriGt/sap-cap-debugger.git
cd sap-cap-debugger
# Install dependencies
bun install
# Create a feature branch
git checkout -b feature/amazing-feature
# Make your changes and test
bun test
bun run lint
# Commit and push
git commit -m "Add amazing feature"
git push origin feature/amazing-feature
# Create a Pull Request
```
### Commit Convention
We follow [Conventional Commits](https://www.conventionalcommits.org/):
- `feat:` - New features
- `fix:` - Bug fixes
- `docs:` - Documentation changes
- `style:` - Code style changes
- `refactor:` - Code refactoring
- `test:` - Adding or updating tests
- `chore:` - Maintenance tasks
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## ๐ Acknowledgments
- Based on the [SAP Community Blog](https://community.sap.com/t5/technology-blogs-by-sap/set-up-remote-debugging-to-diagnose-cap-applications-node-js-stack-at/ba-p/13515376) approach
- Inspired by the SAP Cloud SDK remote debugging guide
- Built for the SAP CAP community
## ๐ Support
- **Issues**: [GitHub Issues](https://github.com/HatriGt/sap-cap-debugger/issues)
- **Discussions**: [GitHub Discussions](https://github.com/HatriGt/sap-cap-debugger/discussions)
## ๐ Related Projects
- [SAP Cloud Application Programming](https://cap.cloud.sap/)
- [SAP Cloud SDK](https://sap.github.io/cloud-sdk/)
- [SAP Business Technology Platform](https://www.sap.com/products/technology-platform.html)
**Note**: This tool is specifically designed for SAP CAP applications before version 8, where the built-in `cds debug` command is not available. For CAP v8+ applications, consider using the official `cds debug` command.
<div align="center">
<p>Made with โค๏ธ for the SAP CAP Community</p>
<p>
<a href="https://github.com/HatriGt/sap-cap-debugger">GitHub</a> โข
<a href="https://www.npmjs.com/package/sap-cap-debugger">NPM</a> โข
<a href="https://github.com/HatriGt/sap-cap-debugger/issues">Issues</a>
</p>
</div>