create-bunspace 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +181 -0
- package/dist/bin.js +5755 -0
- package/dist/templates/monorepo/CLAUDE.md +164 -0
- package/dist/templates/monorepo/LICENSE +21 -0
- package/dist/templates/monorepo/MUST-FOLLOW-GUIDELINES.md +269 -0
- package/dist/templates/monorepo/README.md +74 -0
- package/dist/templates/monorepo/SYNC_VERIFICATION.md +1 -0
- package/dist/templates/monorepo/apps/example/package.json +19 -0
- package/dist/templates/monorepo/apps/example/src/index.ts +23 -0
- package/dist/templates/monorepo/apps/example/src/types/index.ts +7 -0
- package/dist/templates/monorepo/apps/example/src/utils/index.ts +7 -0
- package/dist/templates/monorepo/core/packages/main/package.json +41 -0
- package/dist/templates/monorepo/core/packages/main/rolldown.config.ts +24 -0
- package/dist/templates/monorepo/core/packages/main/src/index.ts +80 -0
- package/dist/templates/monorepo/core/packages/main/src/types/constants.ts +15 -0
- package/dist/templates/monorepo/core/packages/main/src/types/index.ts +8 -0
- package/dist/templates/monorepo/core/packages/main/src/types/main.types.ts +25 -0
- package/dist/templates/monorepo/core/packages/main/src/utils/index.ts +5 -0
- package/dist/templates/monorepo/core/packages/utils/package.json +43 -0
- package/dist/templates/monorepo/core/packages/utils/rolldown.config.ts +34 -0
- package/dist/templates/monorepo/core/packages/utils/src/index.ts +2 -0
- package/dist/templates/monorepo/core/packages/utils/src/logger.ts +68 -0
- package/dist/templates/monorepo/core/packages/utils/src/result.ts +146 -0
- package/dist/templates/monorepo/core/packages/utils/src/types/constants.ts +15 -0
- package/dist/templates/monorepo/core/packages/utils/src/types/index.ts +8 -0
- package/dist/templates/monorepo/core/packages/utils/src/types/utils.types.ts +32 -0
- package/dist/templates/monorepo/core/packages/utils/src/utils/index.ts +5 -0
- package/dist/templates/monorepo/oxlint.json +14 -0
- package/dist/templates/monorepo/package.json +39 -0
- package/dist/templates/monorepo/tsconfig.json +35 -0
- package/dist/templates/telegram-bot/.oxlintrc.json +33 -0
- package/dist/templates/telegram-bot/.prettierignore +5 -0
- package/dist/templates/telegram-bot/.prettierrc +26 -0
- package/dist/templates/telegram-bot/CLAUDE.deploy.md +356 -0
- package/dist/templates/telegram-bot/CLAUDE.dev.md +266 -0
- package/dist/templates/telegram-bot/CLAUDE.md +280 -0
- package/dist/templates/telegram-bot/Dockerfile +46 -0
- package/dist/templates/telegram-bot/README.md +245 -0
- package/dist/templates/telegram-bot/apps/.gitkeep +0 -0
- package/dist/templates/telegram-bot/bun.lock +208 -0
- package/dist/templates/telegram-bot/core/.env.example +71 -0
- package/dist/templates/telegram-bot/core/README.md +1067 -0
- package/dist/templates/telegram-bot/core/package.json +15 -0
- package/dist/templates/telegram-bot/core/src/config/env.ts +131 -0
- package/dist/templates/telegram-bot/core/src/config/index.ts +97 -0
- package/dist/templates/telegram-bot/core/src/config/logging.ts +110 -0
- package/dist/templates/telegram-bot/core/src/handlers/control.ts +85 -0
- package/dist/templates/telegram-bot/core/src/handlers/health.ts +83 -0
- package/dist/templates/telegram-bot/core/src/handlers/logs.ts +126 -0
- package/dist/templates/telegram-bot/core/src/index.ts +161 -0
- package/dist/templates/telegram-bot/core/src/middleware/auth.ts +41 -0
- package/dist/templates/telegram-bot/core/src/middleware/error-handler.ts +41 -0
- package/dist/templates/telegram-bot/core/src/middleware/logging.ts +1 -0
- package/dist/templates/telegram-bot/core/src/middleware/topics.ts +55 -0
- package/dist/templates/telegram-bot/core/src/types/bot.ts +92 -0
- package/dist/templates/telegram-bot/core/src/types/constants.ts +50 -0
- package/dist/templates/telegram-bot/core/src/types/result.ts +1 -0
- package/dist/templates/telegram-bot/core/src/utils/bot-manager.test.ts +111 -0
- package/dist/templates/telegram-bot/core/src/utils/bot-manager.ts +201 -0
- package/dist/templates/telegram-bot/core/src/utils/commands.ts +63 -0
- package/dist/templates/telegram-bot/core/src/utils/formatters.ts +82 -0
- package/dist/templates/telegram-bot/core/src/utils/instance-manager.ts +189 -0
- package/dist/templates/telegram-bot/core/src/utils/memory.ts +33 -0
- package/dist/templates/telegram-bot/core/src/utils/result.ts +26 -0
- package/dist/templates/telegram-bot/core/src/utils/telegram.ts +31 -0
- package/dist/templates/telegram-bot/core/src/utils/type-guards.ts +71 -0
- package/dist/templates/telegram-bot/core/tsconfig.json +9 -0
- package/dist/templates/telegram-bot/docker-compose.yml +37 -0
- package/dist/templates/telegram-bot/docs/cli-commands.md +377 -0
- package/dist/templates/telegram-bot/docs/development.md +363 -0
- package/dist/templates/telegram-bot/docs/environment.md +460 -0
- package/dist/templates/telegram-bot/docs/examples/middleware-auth.md +335 -0
- package/dist/templates/telegram-bot/docs/examples/simple-command.md +207 -0
- package/dist/templates/telegram-bot/docs/examples/webhook-setup.md +362 -0
- package/dist/templates/telegram-bot/docs/getting-started.md +223 -0
- package/dist/templates/telegram-bot/docs/troubleshooting.md +489 -0
- package/dist/templates/telegram-bot/package.json +49 -0
- package/dist/templates/telegram-bot/packages/utils/package.json +12 -0
- package/dist/templates/telegram-bot/packages/utils/src/index.ts +2 -0
- package/dist/templates/telegram-bot/packages/utils/src/logger.ts +72 -0
- package/dist/templates/telegram-bot/packages/utils/src/result.ts +80 -0
- package/dist/templates/telegram-bot/tools/README.md +47 -0
- package/dist/templates/telegram-bot/tools/commands/doctor.ts +460 -0
- package/dist/templates/telegram-bot/tools/commands/index.ts +35 -0
- package/dist/templates/telegram-bot/tools/commands/ngrok.ts +207 -0
- package/dist/templates/telegram-bot/tools/commands/setup.ts +368 -0
- package/dist/templates/telegram-bot/tools/commands/status.ts +140 -0
- package/dist/templates/telegram-bot/tools/index.ts +16 -0
- package/dist/templates/telegram-bot/tools/package.json +12 -0
- package/dist/templates/telegram-bot/tools/utils/index.ts +13 -0
- package/dist/templates/telegram-bot/tsconfig.json +22 -0
- package/dist/templates/telegram-bot/vitest.config.ts +29 -0
- package/package.json +35 -0
- package/templates/monorepo/CLAUDE.md +164 -0
- package/templates/monorepo/LICENSE +21 -0
- package/templates/monorepo/MUST-FOLLOW-GUIDELINES.md +269 -0
- package/templates/monorepo/README.md +74 -0
- package/templates/monorepo/apps/example/package.json +19 -0
- package/templates/monorepo/apps/example/src/index.ts +23 -0
- package/templates/monorepo/apps/example/src/types/index.ts +7 -0
- package/templates/monorepo/apps/example/src/utils/index.ts +7 -0
- package/templates/monorepo/core/packages/main/package.json +41 -0
- package/templates/monorepo/core/packages/main/rolldown.config.ts +24 -0
- package/templates/monorepo/core/packages/main/src/index.ts +80 -0
- package/templates/monorepo/core/packages/main/src/types/constants.ts +15 -0
- package/templates/monorepo/core/packages/main/src/types/index.ts +8 -0
- package/templates/monorepo/core/packages/main/src/types/main.types.ts +25 -0
- package/templates/monorepo/core/packages/main/src/utils/index.ts +5 -0
- package/templates/monorepo/core/packages/utils/package.json +43 -0
- package/templates/monorepo/core/packages/utils/rolldown.config.ts +34 -0
- package/templates/monorepo/core/packages/utils/src/index.ts +2 -0
- package/templates/monorepo/core/packages/utils/src/logger.ts +68 -0
- package/templates/monorepo/core/packages/utils/src/result.ts +146 -0
- package/templates/monorepo/core/packages/utils/src/types/constants.ts +15 -0
- package/templates/monorepo/core/packages/utils/src/types/index.ts +8 -0
- package/templates/monorepo/core/packages/utils/src/types/utils.types.ts +32 -0
- package/templates/monorepo/core/packages/utils/src/utils/index.ts +5 -0
- package/templates/monorepo/oxlint.json +14 -0
- package/templates/monorepo/package.json +39 -0
- package/templates/monorepo/tsconfig.json +35 -0
- package/templates/telegram-bot/.oxlintrc.json +33 -0
- package/templates/telegram-bot/.prettierignore +5 -0
- package/templates/telegram-bot/.prettierrc +26 -0
- package/templates/telegram-bot/CLAUDE.deploy.md +356 -0
- package/templates/telegram-bot/CLAUDE.dev.md +266 -0
- package/templates/telegram-bot/CLAUDE.md +280 -0
- package/templates/telegram-bot/Dockerfile +46 -0
- package/templates/telegram-bot/README.md +245 -0
- package/templates/telegram-bot/apps/.gitkeep +0 -0
- package/templates/telegram-bot/bun.lock +208 -0
- package/templates/telegram-bot/core/.env.example +71 -0
- package/templates/telegram-bot/core/README.md +1067 -0
- package/templates/telegram-bot/core/package.json +15 -0
- package/templates/telegram-bot/core/src/config/env.ts +131 -0
- package/templates/telegram-bot/core/src/config/index.ts +97 -0
- package/templates/telegram-bot/core/src/config/logging.ts +110 -0
- package/templates/telegram-bot/core/src/handlers/control.ts +85 -0
- package/templates/telegram-bot/core/src/handlers/health.ts +83 -0
- package/templates/telegram-bot/core/src/handlers/logs.ts +126 -0
- package/templates/telegram-bot/core/src/index.ts +161 -0
- package/templates/telegram-bot/core/src/middleware/auth.ts +41 -0
- package/templates/telegram-bot/core/src/middleware/error-handler.ts +41 -0
- package/templates/telegram-bot/core/src/middleware/logging.ts +1 -0
- package/templates/telegram-bot/core/src/middleware/topics.ts +55 -0
- package/templates/telegram-bot/core/src/types/bot.ts +92 -0
- package/templates/telegram-bot/core/src/types/constants.ts +50 -0
- package/templates/telegram-bot/core/src/types/result.ts +1 -0
- package/templates/telegram-bot/core/src/utils/bot-manager.test.ts +111 -0
- package/templates/telegram-bot/core/src/utils/bot-manager.ts +201 -0
- package/templates/telegram-bot/core/src/utils/commands.ts +63 -0
- package/templates/telegram-bot/core/src/utils/formatters.ts +82 -0
- package/templates/telegram-bot/core/src/utils/instance-manager.ts +189 -0
- package/templates/telegram-bot/core/src/utils/memory.ts +33 -0
- package/templates/telegram-bot/core/src/utils/result.ts +26 -0
- package/templates/telegram-bot/core/src/utils/telegram.ts +31 -0
- package/templates/telegram-bot/core/src/utils/type-guards.ts +71 -0
- package/templates/telegram-bot/core/tsconfig.json +9 -0
- package/templates/telegram-bot/docker-compose.yml +37 -0
- package/templates/telegram-bot/docs/cli-commands.md +377 -0
- package/templates/telegram-bot/docs/development.md +363 -0
- package/templates/telegram-bot/docs/environment.md +460 -0
- package/templates/telegram-bot/docs/examples/middleware-auth.md +335 -0
- package/templates/telegram-bot/docs/examples/simple-command.md +207 -0
- package/templates/telegram-bot/docs/examples/webhook-setup.md +362 -0
- package/templates/telegram-bot/docs/getting-started.md +223 -0
- package/templates/telegram-bot/docs/troubleshooting.md +489 -0
- package/templates/telegram-bot/package.json +49 -0
- package/templates/telegram-bot/packages/utils/package.json +12 -0
- package/templates/telegram-bot/packages/utils/src/index.ts +2 -0
- package/templates/telegram-bot/packages/utils/src/logger.ts +72 -0
- package/templates/telegram-bot/packages/utils/src/result.ts +80 -0
- package/templates/telegram-bot/tools/README.md +47 -0
- package/templates/telegram-bot/tools/commands/doctor.ts +460 -0
- package/templates/telegram-bot/tools/commands/index.ts +35 -0
- package/templates/telegram-bot/tools/commands/ngrok.ts +207 -0
- package/templates/telegram-bot/tools/commands/setup.ts +368 -0
- package/templates/telegram-bot/tools/commands/status.ts +140 -0
- package/templates/telegram-bot/tools/index.ts +16 -0
- package/templates/telegram-bot/tools/package.json +12 -0
- package/templates/telegram-bot/tools/utils/index.ts +13 -0
- package/templates/telegram-bot/tsconfig.json +22 -0
- package/templates/telegram-bot/vitest.config.ts +29 -0
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
# CLAUDE.deploy.md - Guía de Deployment
|
|
2
|
+
|
|
3
|
+
> **Parte de**: [CLAUDE.md](./CLAUDE.md) | Ver también: [CLAUDE.dev.md](./CLAUDE.dev.md)
|
|
4
|
+
|
|
5
|
+
Guía de deployment para multi-entorno y multi-instancia.
|
|
6
|
+
|
|
7
|
+
**Documentación relacionada**:
|
|
8
|
+
- [docs/deployment/docker.mdx](./docs/deployment/docker.mdx)
|
|
9
|
+
- [docs/deployment/vps.mdx](./docs/deployment/vps.mdx)
|
|
10
|
+
- [docs/deployment/environments.mdx](./docs/deployment/environments.mdx)
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Multi-Environment Architecture
|
|
15
|
+
|
|
16
|
+
El template soporta múltiples entornos con archivos `.env` separados:
|
|
17
|
+
|
|
18
|
+
### Archivos de Entorno
|
|
19
|
+
|
|
20
|
+
| Archivo | Uso | Modo | Bot Token |
|
|
21
|
+
| ------- | --- | ---- | --------- |
|
|
22
|
+
| `core/.env.local` | Desarrollo local | Polling | Local dev token |
|
|
23
|
+
| `core/.env.staging` | Testing/Staging | Webhook | Test bot token |
|
|
24
|
+
| `core/.env.production` | Producción | Webhook | Real bot token |
|
|
25
|
+
|
|
26
|
+
### Selección de Entorno
|
|
27
|
+
|
|
28
|
+
La variable `TG_ENV` determina cuál archivo cargar:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# Default: local (carga .env.local)
|
|
32
|
+
bun run start
|
|
33
|
+
|
|
34
|
+
# Staging
|
|
35
|
+
TG_ENV=staging bun run start
|
|
36
|
+
|
|
37
|
+
# Production
|
|
38
|
+
TG_ENV=production bun run start
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Multi-Instance Management
|
|
44
|
+
|
|
45
|
+
### Sistema de Lock
|
|
46
|
+
|
|
47
|
+
El `InstanceManager` previene conflictos usando archivos PID/LOCK en `core/tmp/`:
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
core/tmp/
|
|
51
|
+
├── mks-bot-local.pid # Process ID
|
|
52
|
+
├── mks-bot-local.lock # Lock data (JSON)
|
|
53
|
+
├── mks-bot-staging.pid
|
|
54
|
+
└── mks-bot-staging.lock
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Contenido del Lock File
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"pid": 12345,
|
|
62
|
+
"instanceId": "1734567890-abc123",
|
|
63
|
+
"environment": "production",
|
|
64
|
+
"instanceName": "mks-bot-prod",
|
|
65
|
+
"startTime": "2025-01-06T10:30:00.000Z",
|
|
66
|
+
"nodeVersion": "v1.3.0",
|
|
67
|
+
"cwd": "/app"
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Detección de Conflictos
|
|
72
|
+
|
|
73
|
+
Al iniciar, el bot verifica si otra instancia está corriendo:
|
|
74
|
+
|
|
75
|
+
- **Si está corriendo**: Error `INSTANCE_CONFLICT` con detalles
|
|
76
|
+
- **Si no está corriendo**: Remueve lock stale y continua
|
|
77
|
+
- **Si está deshabilitado**: `TG_INSTANCE_CHECK=false` salta verificación
|
|
78
|
+
|
|
79
|
+
### Ver Instancias Corriendo
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
# Ver todas las instancias
|
|
83
|
+
bun run status
|
|
84
|
+
|
|
85
|
+
# Output como JSON
|
|
86
|
+
bun run cli status --json
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Output**:
|
|
90
|
+
```
|
|
91
|
+
┌─────────┬───────────────┬───────────────────┬─────────────┬────────┐
|
|
92
|
+
│ (index) │ PID │ Environment │ Name │ Status │
|
|
93
|
+
├─────────┼───────────────┼───────────────────┼─────────────┼────────┤
|
|
94
|
+
│ 0 │ 12345 │ 'production' │ 'mks-bot...' │'✓ Run' │
|
|
95
|
+
│ 1 │ 12346 │ 'staging' │ 'mks-bot...' │'✓ Run' │
|
|
96
|
+
└─────────┴───────────────┴───────────────────┴─────────────┴────────┘
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Docker Deployment
|
|
102
|
+
|
|
103
|
+
### Dockerfile Multi-Stage
|
|
104
|
+
|
|
105
|
+
El template incluye un Dockerfile multi-stage:
|
|
106
|
+
|
|
107
|
+
```dockerfile
|
|
108
|
+
# Build stage
|
|
109
|
+
FROM oven/bun:1.3 AS builder
|
|
110
|
+
WORKDIR /app
|
|
111
|
+
COPY package.json bun.lock ./
|
|
112
|
+
COPY core/package.json core/
|
|
113
|
+
RUN bun install --frozen-lockfile
|
|
114
|
+
COPY core/src core/src
|
|
115
|
+
|
|
116
|
+
# Production stage
|
|
117
|
+
FROM oven/bun:1.3 AS production
|
|
118
|
+
WORKDIR /app
|
|
119
|
+
COPY --from=builder /app/node_modules ./node_modules
|
|
120
|
+
COPY --from=builder /app/core/node_modules ./core/node_modules
|
|
121
|
+
COPY --from=builder /app/core/src ./core/src
|
|
122
|
+
RUN mkdir -p /app/core/tmp /app/core/logs
|
|
123
|
+
ENV TG_ENV=production
|
|
124
|
+
ENV NODE_ENV=production
|
|
125
|
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
|
126
|
+
CMD bun run cli status || exit 1
|
|
127
|
+
EXPOSE 3000
|
|
128
|
+
CMD ["bun", "run", "start"]
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Docker Compose
|
|
132
|
+
|
|
133
|
+
```yaml
|
|
134
|
+
version: '3.8'
|
|
135
|
+
|
|
136
|
+
services:
|
|
137
|
+
bot-local:
|
|
138
|
+
build: .
|
|
139
|
+
container_name: mks-bot-local
|
|
140
|
+
environment:
|
|
141
|
+
- TG_ENV=local
|
|
142
|
+
env_file:
|
|
143
|
+
- core/.env.local
|
|
144
|
+
volumes:
|
|
145
|
+
- ./core/src:/app/core/src:ro
|
|
146
|
+
- ./core/logs:/app/core/logs
|
|
147
|
+
command: bun run --watch src/index.ts
|
|
148
|
+
|
|
149
|
+
bot-staging:
|
|
150
|
+
build: .
|
|
151
|
+
container_name: mks-bot-staging
|
|
152
|
+
environment:
|
|
153
|
+
- TG_ENV=staging
|
|
154
|
+
env_file:
|
|
155
|
+
- core/.env.staging
|
|
156
|
+
ports:
|
|
157
|
+
- "3001:3000"
|
|
158
|
+
|
|
159
|
+
bot-production:
|
|
160
|
+
build: .
|
|
161
|
+
container_name: mks-bot-prod
|
|
162
|
+
environment:
|
|
163
|
+
- TG_ENV=production
|
|
164
|
+
env_file:
|
|
165
|
+
- core/.env.production
|
|
166
|
+
ports:
|
|
167
|
+
- "3000:3000"
|
|
168
|
+
restart: unless-stopped
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Build y Run
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
# Build imagen
|
|
175
|
+
docker build -t mks-telegram-bot .
|
|
176
|
+
|
|
177
|
+
# Run con docker-compose
|
|
178
|
+
docker-compose up bot-production
|
|
179
|
+
|
|
180
|
+
# Ver logs
|
|
181
|
+
docker-compose logs -f bot-production
|
|
182
|
+
|
|
183
|
+
# Ver healthcheck
|
|
184
|
+
docker inspect mks-bot-prod | jq '.[0].State.Health'
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## ngrok Integration
|
|
190
|
+
|
|
191
|
+
### ngrock para Testing Local
|
|
192
|
+
|
|
193
|
+
El comando `ngrok` tiene soporte multi-entorno:
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
# Development con ngrok (local environment)
|
|
197
|
+
bun run ngrok --environment local --start-bot
|
|
198
|
+
|
|
199
|
+
# Staging con ngrok (test bot)
|
|
200
|
+
bun run ngrok --environment staging --webhook-url
|
|
201
|
+
|
|
202
|
+
# Force start aunque haya conflicto
|
|
203
|
+
bun run ngrok --environment production --force
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Opciones Disponibles
|
|
207
|
+
|
|
208
|
+
| Opción | Default | Descripción |
|
|
209
|
+
| ------ | ------- | ----------- |
|
|
210
|
+
| `-p, --port <port>` | `3000` | Puerto a forward |
|
|
211
|
+
| `-e, --environment <env>` | `local` | Entorno: local/staging/production |
|
|
212
|
+
| `-w, --webhook-url` | `false` | Auto-update webhook URL en .env |
|
|
213
|
+
| `-s, --start-bot` | `false` | Auto-start bot después de ngrok |
|
|
214
|
+
| `-f, --force` | `false` | Force start aún si hay conflicto |
|
|
215
|
+
|
|
216
|
+
### Flujo con --environment
|
|
217
|
+
|
|
218
|
+
1. Carga el archivo `.env.{environment}` correspondiente
|
|
219
|
+
2. Detecta conflictos con instancias existentes
|
|
220
|
+
3. Inicia ngrok tunnel
|
|
221
|
+
4. Opcionalmente actualiza `TG_WEBHOOK_URL` en el .env
|
|
222
|
+
5. Opcionalmente inicia el bot con `TG_ENV` configurado
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
## VPS Deployment
|
|
227
|
+
|
|
228
|
+
### Preparar VPS
|
|
229
|
+
|
|
230
|
+
1. **Instalar dependencias**:
|
|
231
|
+
```bash
|
|
232
|
+
curl -fsSL https://bun.sh/install | bash
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
2. **Clonar repositorio**:
|
|
236
|
+
```bash
|
|
237
|
+
git clone <repo-url>
|
|
238
|
+
cd mks-telegram-bot
|
|
239
|
+
bun install
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
3. **Configurar entorno**:
|
|
243
|
+
```bash
|
|
244
|
+
cp core/.env.example core/.env.production
|
|
245
|
+
nano core/.env.production
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
4. **Crear servicio systemd** (opcional):
|
|
249
|
+
```ini
|
|
250
|
+
[Unit]
|
|
251
|
+
Description=mks-telegram-bot
|
|
252
|
+
After=network.target
|
|
253
|
+
|
|
254
|
+
[Service]
|
|
255
|
+
Type=simple
|
|
256
|
+
User=botuser
|
|
257
|
+
WorkingDirectory=/app/mks-telegram-bot
|
|
258
|
+
Environment="TG_ENV=production"
|
|
259
|
+
ExecStart=/usr/local/bin/bun run start
|
|
260
|
+
Restart=always
|
|
261
|
+
|
|
262
|
+
[Install]
|
|
263
|
+
WantedBy=multi-user.target
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Deployment con Docker (Recomendado)
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
# Build y taggear imagen
|
|
270
|
+
docker build -t mks-telegram-bot:latest .
|
|
271
|
+
docker tag mks-telegram-bot:latest registry.example.com/mks-bot:latest
|
|
272
|
+
|
|
273
|
+
# Push a registry (opcional)
|
|
274
|
+
docker push registry.example.com/mks-bot:latest
|
|
275
|
+
|
|
276
|
+
# En VPS
|
|
277
|
+
docker pull registry.example.com/mks-bot:latest
|
|
278
|
+
docker run -d \
|
|
279
|
+
--name mks-bot-prod \
|
|
280
|
+
--restart unless-stopped \
|
|
281
|
+
-p 3000:3000 \
|
|
282
|
+
--env-file core/.env.production \
|
|
283
|
+
registry.example.com/mks-bot:latest
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
## Troubleshooting Deployment
|
|
289
|
+
|
|
290
|
+
### Bot Doesn't Respond
|
|
291
|
+
|
|
292
|
+
1. Check `TG_BOT_TOKEN` is valid
|
|
293
|
+
2. Verify bot is running (`bun run status`)
|
|
294
|
+
3. Check `TG_AUTHORIZED_USER_IDS` for control commands
|
|
295
|
+
|
|
296
|
+
### Webhook Not Working
|
|
297
|
+
|
|
298
|
+
1. Verify `TG_WEBHOOK_URL` is HTTPS and publicly accessible
|
|
299
|
+
2. Check `TG_WEBHOOK_SECRET` matches (min 16 chars)
|
|
300
|
+
3. Verify firewall allows incoming connections
|
|
301
|
+
|
|
302
|
+
### Instance Conflict
|
|
303
|
+
|
|
304
|
+
**Error**: `INSTANCE_CONFLICT - Another instance is already running`
|
|
305
|
+
|
|
306
|
+
**Solutions**:
|
|
307
|
+
1. Stop the existing instance: `bun run cli kill <instance_name>`
|
|
308
|
+
2. Use a different instance name in your `.env` file
|
|
309
|
+
3. Disable instance check: `TG_INSTANCE_CHECK=false` (not recommended)
|
|
310
|
+
4. Remove stale lock files: `rm -f core/tmp/*.lock core/tmp/*.pid`
|
|
311
|
+
|
|
312
|
+
### Docker Issues
|
|
313
|
+
|
|
314
|
+
```bash
|
|
315
|
+
# Ver logs del container
|
|
316
|
+
docker logs mks-bot-prod
|
|
317
|
+
|
|
318
|
+
# Entrar al container
|
|
319
|
+
docker exec -it mks-bot-prod /bin/sh
|
|
320
|
+
|
|
321
|
+
# Ver healthcheck
|
|
322
|
+
docker inspect mks-bot-prod | jq '.[0].State.Health'
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### ngrok Tunnel Not Working
|
|
326
|
+
|
|
327
|
+
1. Verify ngrok is installed: `ngrok version`
|
|
328
|
+
2. Check ngrok is not already running on port 4040
|
|
329
|
+
3. Use `--force` flag if conflict detected
|
|
330
|
+
4. Check port is available: `lsof -i :3000`
|
|
331
|
+
|
|
332
|
+
---
|
|
333
|
+
|
|
334
|
+
## Environment Variables Reference
|
|
335
|
+
|
|
336
|
+
### Variables de Entorno
|
|
337
|
+
|
|
338
|
+
| Variable | Default | Descripción |
|
|
339
|
+
| -------- | ------- | ----------- |
|
|
340
|
+
| `TG_ENV` | `local` | Environment: local/staging/production |
|
|
341
|
+
| `TG_BOT_TOKEN` | required | Bot token from @BotFather |
|
|
342
|
+
| `TG_MODE` | `polling` | `polling` or `webhook` |
|
|
343
|
+
| `TG_WEBHOOK_URL` | - | Public HTTPS webhook URL |
|
|
344
|
+
| `TG_WEBHOOK_SECRET` | - | Secret token for validation |
|
|
345
|
+
| `TG_INSTANCE_NAME` | `mks-bot` | Unique instance name for locking |
|
|
346
|
+
| `TG_INSTANCE_CHECK` | `true` | Enable instance conflict detection |
|
|
347
|
+
| `LOG_LEVEL` | `info` | Log verbosity |
|
|
348
|
+
| `TG_DEBUG` | `false` | Enable debug mode |
|
|
349
|
+
|
|
350
|
+
---
|
|
351
|
+
|
|
352
|
+
## Ver También
|
|
353
|
+
|
|
354
|
+
- [CLAUDE.md](./CLAUDE.md) - Entry point principal
|
|
355
|
+
- [CLAUDE.dev.md](./CLAUDE.dev.md) - Desarrollo y patterns
|
|
356
|
+
- [README.md](./README.md) - Quick start del proyecto
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
# CLAUDE.dev - Guía de Desarrollo
|
|
2
|
+
|
|
3
|
+
> **Parte de**: [CLAUDE.md](./CLAUDE.md) | Ver también: [CLAUDE.deploy.md](./CLAUDE.deploy.md)
|
|
4
|
+
|
|
5
|
+
Guía de desarrollo para extender y modificar el bot template.
|
|
6
|
+
|
|
7
|
+
**Documentación relacionada**:
|
|
8
|
+
- [docs/development/setup.mdx](./docs/development/setup.mdx)
|
|
9
|
+
- [docs/development/patterns.mdx](./docs/development/patterns.mdx)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Development Standards
|
|
14
|
+
|
|
15
|
+
### Code Style
|
|
16
|
+
|
|
17
|
+
- TypeScript strict mode enabled
|
|
18
|
+
- `noUncheckedIndexedAccess: true` - arrays need undefined checks
|
|
19
|
+
- Semi: false, singleQuote: true
|
|
20
|
+
- Result type pattern para error handling
|
|
21
|
+
- Better Logger para logging (no console.*)
|
|
22
|
+
|
|
23
|
+
### Before Commit Checklist
|
|
24
|
+
|
|
25
|
+
- [ ] `bun run typecheck` → 0 errores
|
|
26
|
+
- [ ] `bun run lint` → 0 warnings, 0 errors
|
|
27
|
+
- [ ] `bun test` → all tests pass
|
|
28
|
+
- [ ] No `console.*` statements
|
|
29
|
+
- [ ] Código formateado (`bun run format:check`)
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Agregando Commands
|
|
34
|
+
|
|
35
|
+
### 1. Crear Handler
|
|
36
|
+
|
|
37
|
+
**Archivo**: `core/src/handlers/mycommand.ts`
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import type { Context } from 'telegraf'
|
|
41
|
+
|
|
42
|
+
export async function handleMyCommand(ctx: Context): Promise<void> {
|
|
43
|
+
await ctx.reply('Respuesta del comando')
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 2. Registrar en Bot
|
|
48
|
+
|
|
49
|
+
**Archivo**: `core/src/index.ts`
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { handleMyCommand } from './handlers/mycommand.js'
|
|
53
|
+
bot.command('mycommand', handleMyCommand)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Agregando Middleware
|
|
59
|
+
|
|
60
|
+
### 1. Crear Middleware
|
|
61
|
+
|
|
62
|
+
**Archivo**: `core/src/middleware/custom.ts`
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import type { Context, Middleware } from 'telegraf'
|
|
66
|
+
|
|
67
|
+
export function customMiddleware(): Middleware<Context> {
|
|
68
|
+
return async (ctx, next) => {
|
|
69
|
+
// Pre-processing
|
|
70
|
+
await next()
|
|
71
|
+
// Post-processing
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 2. Registrar Middleware
|
|
77
|
+
|
|
78
|
+
**Archivo**: `core/src/index.ts`
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { customMiddleware } from './middleware/custom.js'
|
|
82
|
+
bot.use(customMiddleware())
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## Patterns
|
|
88
|
+
|
|
89
|
+
### Result Type Pattern
|
|
90
|
+
|
|
91
|
+
Railway-oriented programming para error handling sin excepciones:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { ok, err, type Result, botError } from './types/result.js'
|
|
95
|
+
|
|
96
|
+
function divide(a: number, b: number): Result<number> {
|
|
97
|
+
if (b === 0) {
|
|
98
|
+
return err(botError('INVALID_ARGS', 'Cannot divide by zero'))
|
|
99
|
+
}
|
|
100
|
+
return ok(a / b)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const result = divide(10, 2)
|
|
104
|
+
if (result.ok) {
|
|
105
|
+
console.log(result.value) // 5
|
|
106
|
+
} else {
|
|
107
|
+
console.error(result.error.message)
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**Utilities disponibles**: `ok`, `err`, `isOk`, `isErr`, `unwrap`, `unwrapOr`, `map`, `mapErr`, `flatMap`, `tap`, `tapErr`, `match`, `collect`, `all`
|
|
112
|
+
|
|
113
|
+
### Singleton Pattern
|
|
114
|
+
|
|
115
|
+
- `BotManager` - Lifecycle y stats
|
|
116
|
+
- `LogStreamer` - Buffer de logs
|
|
117
|
+
- `InstanceManager` - Lock management
|
|
118
|
+
- `Config` - Cached configuration
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Logging System
|
|
123
|
+
|
|
124
|
+
### Usar Better Logger
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { botLogger, kv, badge, colorText, colors } from './middleware/logging.js'
|
|
128
|
+
|
|
129
|
+
botLogger.info('Información')
|
|
130
|
+
botLogger.success('Operación exitosa')
|
|
131
|
+
botLogger.warn('Advertencia')
|
|
132
|
+
botLogger.error('Error', error)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Crear Logger Especializado
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { component } from '@mks2508/better-logger'
|
|
139
|
+
|
|
140
|
+
const myLogger = component('MyModule')
|
|
141
|
+
myLogger.info('Módulo iniciado')
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Formatting Avanzado
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
// Key-Value formatting
|
|
148
|
+
botLogger.info(kv({ user: '123', action: 'start' }))
|
|
149
|
+
|
|
150
|
+
// Badge
|
|
151
|
+
botLogger.info(badge('CMD', 'rounded'))
|
|
152
|
+
|
|
153
|
+
// Colored text
|
|
154
|
+
botLogger.success(colorText('Success', colors.success))
|
|
155
|
+
|
|
156
|
+
// Duration
|
|
157
|
+
const { formatDuration } = await import('./middleware/logging.js')
|
|
158
|
+
botLogger.info(formatDuration(1234)) // "1.2s"
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## Testing
|
|
164
|
+
|
|
165
|
+
### Escribiendo Tests
|
|
166
|
+
|
|
167
|
+
Usa `bun test` con sintaxis similar a Jest:
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { describe, test, expect } from 'bun:test'
|
|
171
|
+
|
|
172
|
+
describe('Mi Módulo', () => {
|
|
173
|
+
test('debe hacer algo', () => {
|
|
174
|
+
const result = miFuncion()
|
|
175
|
+
expect(result).toBe('expected')
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Archivos de Test
|
|
181
|
+
|
|
182
|
+
- **Nombre**: `*.test.ts` o `*.spec.ts`
|
|
183
|
+
- **Ubicación**: Junto al archivo que testean
|
|
184
|
+
- **Ejemplo**: `core/src/types/result.test.ts`
|
|
185
|
+
|
|
186
|
+
### Ejecutar Tests
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
bun test # Ejecutar todos
|
|
190
|
+
bun test --verbose # Output detallado
|
|
191
|
+
bun test --watch # Watch mode
|
|
192
|
+
bun test path/to/test.ts # Test específico
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## Environment Variables (Desarrollo)
|
|
198
|
+
|
|
199
|
+
### Archivo: `core/.env.example`
|
|
200
|
+
|
|
201
|
+
Variables requeridas para desarrollo:
|
|
202
|
+
|
|
203
|
+
| Variable | Descripción |
|
|
204
|
+
| -------- | ----------- |
|
|
205
|
+
| `TG_BOT_TOKEN` | Token from @BotFather |
|
|
206
|
+
| `TG_MODE` | `polling` o `webhook` |
|
|
207
|
+
| `LOG_LEVEL` | `debug`, `info`, `warn`, `error` |
|
|
208
|
+
| `TG_DEBUG` | Habilitar debug mode |
|
|
209
|
+
| `TG_INSTANCE_CHECK` | Habilitar detección de instancias |
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Troubleshooting Development
|
|
214
|
+
|
|
215
|
+
### Typecheck Errors
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
# Ejecutar typecheck detallado
|
|
219
|
+
tsgo --traceback
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Tests Fallan
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
# Ejecutar tests con output detallado
|
|
226
|
+
bun test --verbose
|
|
227
|
+
|
|
228
|
+
# Ejecutar solo un archivo de test
|
|
229
|
+
bun test core/src/types/result.test.ts
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Bot No Responde
|
|
233
|
+
|
|
234
|
+
1. Verificar `TG_BOT_TOKEN` es válido
|
|
235
|
+
2. Chequear `/health` command
|
|
236
|
+
3. Verificar `TG_AUTHORIZED_USER_IDS` para control commands
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## Quick Reference
|
|
241
|
+
|
|
242
|
+
### Comandos de Desarrollo
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
bun run dev # Watch mode
|
|
246
|
+
bun run typecheck # Type check
|
|
247
|
+
bun run lint # Lint check
|
|
248
|
+
bun run format # Format code
|
|
249
|
+
bun test # Run tests
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### Archivos Clave para Desarrollo
|
|
253
|
+
|
|
254
|
+
- `core/src/index.ts` - Entry point, command registration
|
|
255
|
+
- `core/src/handlers/` - Command handlers
|
|
256
|
+
- `core/src/middleware/` - Telegraf middleware
|
|
257
|
+
- `core/src/config/` - Configuration and env validation
|
|
258
|
+
- `packages/utils/src/` - Shared utilities
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## Ver También
|
|
263
|
+
|
|
264
|
+
- [CLAUDE.md](./CLAUDE.md) - Entry point principal
|
|
265
|
+
- [CLAUDE.deploy.md](./CLAUDE.deploy.md) - Deployment y entornos
|
|
266
|
+
- [README.md](./README.md) - Quick start del proyecto
|