suthep 0.1.0-beta.1 → 0.2.0-beta.1

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.
Files changed (69) hide show
  1. package/README.md +167 -69
  2. package/dist/commands/down.js +179 -0
  3. package/dist/commands/down.js.map +1 -0
  4. package/dist/commands/redeploy.js +59 -0
  5. package/dist/commands/redeploy.js.map +1 -0
  6. package/dist/commands/up.js +213 -0
  7. package/dist/commands/up.js.map +1 -0
  8. package/dist/index.js +28 -0
  9. package/dist/index.js.map +1 -1
  10. package/dist/utils/deployment.js +10 -1
  11. package/dist/utils/deployment.js.map +1 -1
  12. package/dist/utils/docker.js +35 -0
  13. package/dist/utils/docker.js.map +1 -1
  14. package/dist/utils/nginx.js +10 -0
  15. package/dist/utils/nginx.js.map +1 -1
  16. package/docs/README.md +25 -82
  17. package/docs/english/01-introduction.md +84 -0
  18. package/docs/english/02-installation.md +200 -0
  19. package/docs/english/03-quick-start.md +256 -0
  20. package/docs/english/04-configuration.md +358 -0
  21. package/docs/english/05-commands.md +363 -0
  22. package/docs/english/06-examples.md +456 -0
  23. package/docs/english/07-troubleshooting.md +417 -0
  24. package/docs/english/08-advanced.md +411 -0
  25. package/docs/english/README.md +48 -0
  26. package/docs/thai/01-introduction.md +84 -0
  27. package/docs/thai/02-installation.md +200 -0
  28. package/docs/thai/03-quick-start.md +256 -0
  29. package/docs/thai/04-configuration.md +358 -0
  30. package/docs/thai/05-commands.md +363 -0
  31. package/docs/thai/06-examples.md +456 -0
  32. package/docs/thai/07-troubleshooting.md +417 -0
  33. package/docs/thai/08-advanced.md +411 -0
  34. package/docs/thai/README.md +48 -0
  35. package/example/README.md +282 -53
  36. package/example/suthep-complete.yml +103 -0
  37. package/example/suthep-docker-only.yml +71 -0
  38. package/example/suthep-no-docker.yml +51 -0
  39. package/example/{muacle.yml → suthep-path-routing.yml} +20 -5
  40. package/example/suthep.example.yml +89 -0
  41. package/package.json +1 -1
  42. package/src/commands/down.ts +240 -0
  43. package/src/commands/redeploy.ts +78 -0
  44. package/src/commands/up.ts +271 -0
  45. package/src/index.ts +53 -0
  46. package/suthep-0.1.0-beta.1.tgz +0 -0
  47. package/suthep.example.yml +5 -0
  48. package/suthep.yml +39 -0
  49. package/docs/TRANSLATIONS.md +0 -211
  50. package/docs/en/README.md +0 -76
  51. package/docs/en/api-reference.md +0 -545
  52. package/docs/en/architecture.md +0 -369
  53. package/docs/en/commands.md +0 -273
  54. package/docs/en/configuration.md +0 -347
  55. package/docs/en/developer-guide.md +0 -588
  56. package/docs/en/docker-ports-config.md +0 -333
  57. package/docs/en/examples.md +0 -537
  58. package/docs/en/getting-started.md +0 -202
  59. package/docs/en/port-binding.md +0 -268
  60. package/docs/en/troubleshooting.md +0 -441
  61. package/docs/th/README.md +0 -64
  62. package/docs/th/commands.md +0 -202
  63. package/docs/th/configuration.md +0 -325
  64. package/docs/th/getting-started.md +0 -203
  65. package/example/docker-compose.yml +0 -76
  66. package/example/docker-ports-example.yml +0 -81
  67. package/example/port-binding-example.yml +0 -45
  68. package/example/suthep.yml +0 -46
  69. package/example/suthep=1.yml +0 -46
@@ -1,588 +0,0 @@
1
- # Developer Guide
2
-
3
- This guide is for developers who want to contribute to Suthep, understand its codebase, or extend its functionality.
4
-
5
- ## Table of Contents
6
-
7
- 1. [Development Setup](#development-setup)
8
- 2. [Project Structure](#project-structure)
9
- 3. [Architecture Overview](#architecture-overview)
10
- 4. [Code Organization](#code-organization)
11
- 5. [Development Workflow](#development-workflow)
12
- 6. [Building and Testing](#building-and-testing)
13
- 7. [Adding New Features](#adding-new-features)
14
- 8. [Code Style and Guidelines](#code-style-and-guidelines)
15
- 9. [Debugging](#debugging)
16
- 10. [Contributing](#contributing)
17
-
18
- ## Development Setup
19
-
20
- ### Prerequisites
21
-
22
- - **Node.js 16+** - [Download Node.js](https://nodejs.org/)
23
- - **npm** or **yarn** - Package manager
24
- - **TypeScript** - Already included as a dev dependency
25
- - **Git** - Version control
26
-
27
- ### Initial Setup
28
-
29
- 1. **Clone the repository**:
30
- ```bash
31
- git clone <repository-url>
32
- cd suthep
33
- ```
34
-
35
- 2. **Install dependencies**:
36
- ```bash
37
- npm install
38
- ```
39
-
40
- 3. **Link the CLI tool for local development**:
41
- ```bash
42
- npm link
43
- ```
44
- This makes the `suthep` command available globally, pointing to your local development version.
45
-
46
- 4. **Verify setup**:
47
- ```bash
48
- suthep --version
49
- suthep --help
50
- ```
51
-
52
- ### Development Dependencies
53
-
54
- The project uses:
55
- - **TypeScript** - Type-safe JavaScript
56
- - **Vite** - Build tool and bundler
57
- - **Chalk** - Terminal string styling
58
- - **Commander** - CLI framework
59
- - **Execa** - Process execution
60
- - **Inquirer** - Interactive CLI prompts
61
- - **js-yaml** - YAML parsing
62
-
63
- ## Project Structure
64
-
65
- ```
66
- suthep/
67
- ├── src/ # Source code
68
- │ ├── commands/ # CLI commands
69
- │ │ ├── deploy.ts # Deploy command
70
- │ │ ├── init.ts # Init command
71
- │ │ └── setup.ts # Setup command
72
- │ ├── types/ # TypeScript type definitions
73
- │ │ └── config.ts # Configuration types
74
- │ ├── utils/ # Utility modules
75
- │ │ ├── certbot.ts # Certbot/SSL utilities
76
- │ │ ├── config-loader.ts # Configuration loading
77
- │ │ ├── deployment.ts # Deployment strategies
78
- │ │ ├── docker.ts # Docker management
79
- │ │ └── nginx.ts # Nginx configuration
80
- │ └── index.ts # Entry point
81
- ├── dist/ # Compiled output (generated)
82
- ├── docs/ # Documentation
83
- ├── example/ # Example configurations
84
- ├── package.json # Project metadata and dependencies
85
- ├── tsconfig.json # TypeScript configuration
86
- ├── vite.config.ts # Vite build configuration
87
- └── README.md # Project readme
88
- ```
89
-
90
- ## Architecture Overview
91
-
92
- Suthep follows a modular architecture with clear separation of concerns:
93
-
94
- ### Core Components
95
-
96
- 1. **CLI Layer** (`src/commands/`)
97
- - Handles user interaction
98
- - Parses command-line arguments
99
- - Orchestrates deployment flow
100
-
101
- 2. **Configuration Layer** (`src/utils/config-loader.ts`)
102
- - Loads and validates YAML configuration
103
- - Provides type-safe configuration objects
104
-
105
- 3. **Service Managers** (`src/utils/`)
106
- - **Docker Manager**: Container lifecycle management
107
- - **Nginx Manager**: Reverse proxy configuration
108
- - **Certbot Manager**: SSL certificate management
109
- - **Deployment Manager**: Deployment strategies
110
-
111
- 4. **Type System** (`src/types/`)
112
- - TypeScript interfaces for all configuration types
113
- - Ensures type safety across the codebase
114
-
115
- ### Data Flow
116
-
117
- ```
118
- User Command
119
-
120
- CLI Command Handler
121
-
122
- Configuration Loader
123
-
124
- Service Managers (Docker, Nginx, Certbot)
125
-
126
- External Tools (Docker, Nginx, Certbot)
127
- ```
128
-
129
- ## Code Organization
130
-
131
- ### Commands (`src/commands/`)
132
-
133
- Each command is a self-contained module:
134
-
135
- - **`deploy.ts`**: Main deployment orchestration
136
- - Groups services by domain
137
- - Manages Docker containers
138
- - Generates Nginx configs
139
- - Handles SSL certificates
140
- - Performs health checks
141
-
142
- - **`init.ts`**: Interactive configuration creation
143
- - Prompts for project details
144
- - Guides service configuration
145
- - Generates `suthep.yml`
146
-
147
- - **`setup.ts`**: Prerequisites installation
148
- - Installs Nginx
149
- - Installs Certbot
150
- - Configures system services
151
-
152
- ### Utilities (`src/utils/`)
153
-
154
- **`docker.ts`** - Docker Container Management
155
- - `startDockerContainer()`: Start or recreate containers
156
- - `stopDockerContainer()`: Stop containers
157
- - `isContainerRunning()`: Check container status
158
- - `getContainerLogs()`: Retrieve logs
159
- - `needsRecreate()`: Check if container needs recreation
160
- - `getContainerPortMapping()`: Get port mappings
161
-
162
- **`nginx.ts`** - Nginx Configuration
163
- - `generateNginxConfig()`: Single service config
164
- - `generateMultiServiceNginxConfig()`: Multiple services on same domain
165
- - `writeNginxConfig()`: Write config files
166
- - `enableSite()`: Enable Nginx site
167
- - `reloadNginx()`: Reload Nginx configuration
168
-
169
- **`certbot.ts`** - SSL Certificate Management
170
- - `requestCertificate()`: Request new certificate
171
- - `certificateExists()`: Check if certificate exists
172
- - `renewCertificates()`: Renew certificates
173
- - `checkCertificateExpiration()`: Check expiration date
174
-
175
- **`deployment.ts`** - Deployment Strategies
176
- - `deployService()`: Main deployment function
177
- - `rollingDeploy()`: Rolling deployment strategy
178
- - `blueGreenDeploy()`: Blue-green deployment strategy
179
- - `performHealthCheck()`: Health check validation
180
-
181
- **`config-loader.ts`** - Configuration Management
182
- - `loadConfig()`: Load and validate YAML config
183
- - `saveConfig()`: Save configuration to file
184
- - `validateConfig()`: Validate configuration structure
185
-
186
- ### Types (`src/types/config.ts`)
187
-
188
- All configuration types are defined as TypeScript interfaces:
189
-
190
- ```typescript
191
- interface ServiceConfig {
192
- name: string
193
- port: number
194
- domains: string[]
195
- path?: string
196
- docker?: DockerConfig
197
- healthCheck?: HealthCheckConfig
198
- environment?: Record<string, string>
199
- }
200
- ```
201
-
202
- ## Development Workflow
203
-
204
- ### Making Changes
205
-
206
- 1. **Create a feature branch**:
207
- ```bash
208
- git checkout -b feature/your-feature-name
209
- ```
210
-
211
- 2. **Make your changes**:
212
- - Edit source files in `src/`
213
- - Follow existing code patterns
214
- - Add comments for complex logic
215
-
216
- 3. **Build and test**:
217
- ```bash
218
- npm run build
219
- npm test
220
- ```
221
-
222
- 4. **Test manually**:
223
- ```bash
224
- # After building, test the CLI
225
- suthep --help
226
- suthep init
227
- suthep deploy --file example/suthep.yml
228
- ```
229
-
230
- 5. **Commit your changes**:
231
- ```bash
232
- git add .
233
- git commit -m "feat: description of your changes"
234
- ```
235
-
236
- ### Development Scripts
237
-
238
- - **`npm run build`**: Compile TypeScript to JavaScript
239
- - **`npm run dev`**: Start development mode (if configured)
240
- - **`npm test`**: Run tests (currently builds and shows help)
241
-
242
- ### Hot Reloading
243
-
244
- Since the CLI is linked globally, you need to rebuild after changes:
245
-
246
- ```bash
247
- npm run build
248
- ```
249
-
250
- The `suthep` command will use the newly built version.
251
-
252
- ## Building and Testing
253
-
254
- ### Build Process
255
-
256
- 1. **TypeScript Compilation**:
257
- ```bash
258
- tsc
259
- ```
260
- Compiles TypeScript files to JavaScript in the `dist/` directory.
261
-
262
- 2. **Vite Bundling**:
263
- ```bash
264
- vite build
265
- ```
266
- Bundles the code for production.
267
-
268
- 3. **Complete Build**:
269
- ```bash
270
- npm run build
271
- ```
272
- Runs both compilation and bundling.
273
-
274
- ### Testing
275
-
276
- Currently, testing is minimal. To test:
277
-
278
- 1. **Build the project**:
279
- ```bash
280
- npm run build
281
- ```
282
-
283
- 2. **Test commands manually**:
284
- ```bash
285
- # Test help
286
- suthep --help
287
-
288
- # Test init
289
- suthep init --file test-config.yml
290
-
291
- # Test deploy (with a test config)
292
- suthep deploy --file example/suthep.yml --nginx=false --https=false
293
- ```
294
-
295
- ### Adding Tests
296
-
297
- To add proper testing:
298
-
299
- 1. Install a testing framework:
300
- ```bash
301
- npm install --save-dev jest @types/jest
302
- ```
303
-
304
- 2. Create test files:
305
- ```
306
- src/
307
- ├── utils/
308
- │ ├── docker.ts
309
- │ └── docker.test.ts
310
- ```
311
-
312
- 3. Write tests for utility functions
313
-
314
- ## Adding New Features
315
-
316
- ### Adding a New Command
317
-
318
- 1. **Create command file** (`src/commands/newcommand.ts`):
319
- ```typescript
320
- import chalk from 'chalk'
321
- import type { DeployOptions } from '../types/config'
322
-
323
- interface NewCommandOptions {
324
- flag: string
325
- }
326
-
327
- export async function newCommand(options: NewCommandOptions): Promise<void> {
328
- console.log(chalk.blue.bold('\n🚀 New Command\n'))
329
- // Implementation
330
- }
331
- ```
332
-
333
- 2. **Register in `src/index.ts`**:
334
- ```typescript
335
- import { newCommand } from './commands/newcommand'
336
-
337
- program
338
- .command('newcommand')
339
- .description('Description of new command')
340
- .option('-f, --flag <value>', 'Flag description')
341
- .action(async (options) => {
342
- await newCommand(options)
343
- })
344
- ```
345
-
346
- 3. **Update documentation** in `docs/commands.md`
347
-
348
- ### Adding a New Utility Function
349
-
350
- 1. **Add to appropriate utility file** or create new one
351
- 2. **Export the function**
352
- 3. **Add TypeScript types**
353
- 4. **Update API documentation** in `docs/api-reference.md`
354
-
355
- ### Extending Configuration
356
-
357
- 1. **Update types** (`src/types/config.ts`):
358
- ```typescript
359
- export interface ServiceConfig {
360
- // ... existing fields
361
- newField?: string
362
- }
363
- ```
364
-
365
- 2. **Update config loader** (`src/utils/config-loader.ts`) to handle new field
366
- 3. **Update validation** if needed
367
- 4. **Update documentation** in `docs/configuration.md`
368
-
369
- ## Code Style and Guidelines
370
-
371
- ### TypeScript Guidelines
372
-
373
- - **Use TypeScript types**: Always define types for function parameters and return values
374
- - **Prefer interfaces over types**: For object shapes, use interfaces
375
- - **Use strict mode**: TypeScript strict mode is enabled
376
- - **Avoid `any`**: Use proper types or `unknown` if type is truly unknown
377
-
378
- ### Naming Conventions
379
-
380
- - **Files**: Use kebab-case (`config-loader.ts`)
381
- - **Functions**: Use camelCase (`startDockerContainer`)
382
- - **Interfaces**: Use PascalCase (`ServiceConfig`)
383
- - **Constants**: Use UPPER_SNAKE_CASE (`MAX_RETRIES`)
384
-
385
- ### Code Organization
386
-
387
- - **One function per responsibility**: Keep functions focused
388
- - **Extract utilities**: Reusable logic should be in utility functions
389
- - **Error handling**: Always handle errors appropriately
390
- - **Logging**: Use `chalk` for colored output, provide clear messages
391
-
392
- ### Example Code Style
393
-
394
- ```typescript
395
- /**
396
- * Start or connect to a Docker container for a service
397
- * @param service - Service configuration
398
- * @throws Error if container cannot be started
399
- */
400
- export async function startDockerContainer(
401
- service: ServiceConfig
402
- ): Promise<void> {
403
- if (!service.docker) {
404
- return
405
- }
406
-
407
- const { image, container, port } = service.docker
408
-
409
- try {
410
- // Implementation
411
- } catch (error) {
412
- throw new Error(
413
- `Failed to start Docker container: ${
414
- error instanceof Error ? error.message : error
415
- }`
416
- )
417
- }
418
- }
419
- ```
420
-
421
- ## Debugging
422
-
423
- ### Enable Debug Logging
424
-
425
- Add debug logging to functions:
426
-
427
- ```typescript
428
- import chalk from 'chalk'
429
-
430
- console.log(chalk.dim(`Debug: ${variable}`))
431
- ```
432
-
433
- ### Common Debugging Scenarios
434
-
435
- 1. **Docker Issues**:
436
- ```bash
437
- # Check container status
438
- docker ps -a
439
- docker logs <container-name>
440
- ```
441
-
442
- 2. **Nginx Issues**:
443
- ```bash
444
- # Test configuration
445
- sudo nginx -t
446
-
447
- # Check error logs
448
- sudo tail -f /var/log/nginx/error.log
449
- ```
450
-
451
- 3. **Certbot Issues**:
452
- ```bash
453
- # Check certificates
454
- sudo certbot certificates
455
-
456
- # Check logs
457
- sudo tail -f /var/log/letsencrypt/letsencrypt.log
458
- ```
459
-
460
- ### Using Node.js Debugger
461
-
462
- 1. **Add debugger statement**:
463
- ```typescript
464
- debugger
465
- ```
466
-
467
- 2. **Run with inspect**:
468
- ```bash
469
- node --inspect dist/index.js deploy
470
- ```
471
-
472
- 3. **Connect with Chrome DevTools**: `chrome://inspect`
473
-
474
- ## Contributing
475
-
476
- ### Before Contributing
477
-
478
- 1. **Check existing issues**: See if your feature/bug is already reported
479
- 2. **Discuss major changes**: Open an issue first for significant changes
480
- 3. **Follow the code style**: Match existing code patterns
481
-
482
- ### Pull Request Process
483
-
484
- 1. **Fork the repository**
485
- 2. **Create a feature branch**
486
- 3. **Make your changes**
487
- 4. **Test thoroughly**
488
- 5. **Update documentation**
489
- 6. **Submit pull request**
490
-
491
- ### Commit Message Format
492
-
493
- Use conventional commits:
494
-
495
- - `feat:` - New feature
496
- - `fix:` - Bug fix
497
- - `docs:` - Documentation changes
498
- - `style:` - Code style changes
499
- - `refactor:` - Code refactoring
500
- - `test:` - Test additions/changes
501
- - `chore:` - Build process or auxiliary tool changes
502
-
503
- Examples:
504
- ```
505
- feat: add support for custom nginx templates
506
- fix: handle missing docker image gracefully
507
- docs: update developer guide
508
- ```
509
-
510
- ### Code Review Checklist
511
-
512
- - [ ] Code follows existing patterns
513
- - [ ] TypeScript types are properly defined
514
- - [ ] Error handling is appropriate
515
- - [ ] Logging is clear and helpful
516
- - [ ] Documentation is updated
517
- - [ ] Tests pass (if applicable)
518
- - [ ] No console.log statements (use chalk for user-facing output)
519
-
520
- ## Key Implementation Details
521
-
522
- ### Docker Container Management
523
-
524
- Containers are managed with the following logic:
525
-
526
- 1. **Check existence**: Use `docker inspect` to check if container exists
527
- 2. **Recreate if needed**: Always recreate on redeploy for fresh state
528
- 3. **Port mapping**: Format is `hostPort:containerPort`
529
- 4. **Environment variables**: Injected via `-e` flags
530
-
531
- ### Nginx Configuration
532
-
533
- - **One config per domain**: Services on the same domain share one config file
534
- - **Upstream naming**: Format is `domain_service` (e.g., `muacle_com_api`)
535
- - **Path-based routing**: Services are routed by their `path` configuration
536
- - **HTTPS support**: Automatically configured when `--https` flag is used
537
-
538
- ### SSL Certificate Management
539
-
540
- - **Check before request**: Always check if certificate exists first
541
- - **Skip if exists**: Don't request if certificate already exists
542
- - **Domain-based**: One certificate per domain
543
- - **Automatic renewal**: Certbot handles renewal automatically
544
-
545
- ### Deployment Strategies
546
-
547
- - **Rolling**: Gradually replace old instances (currently simplified)
548
- - **Blue-Green**: Maintain two environments (currently simplified)
549
-
550
- ## Troubleshooting Development Issues
551
-
552
- ### Build Errors
553
-
554
- - **TypeScript errors**: Check `tsconfig.json` settings
555
- - **Module not found**: Ensure all dependencies are installed
556
- - **Import errors**: Check import paths match file structure
557
-
558
- ### Runtime Errors
559
-
560
- - **Permission denied**: Ensure sudo access for Nginx/Certbot operations
561
- - **Command not found**: Rebuild and relink: `npm run build && npm link`
562
- - **Configuration errors**: Validate YAML syntax
563
-
564
- ### Common Issues
565
-
566
- 1. **Changes not reflected**: Rebuild the project (`npm run build`)
567
- 2. **Linked version outdated**: Run `npm link` again
568
- 3. **Type errors**: Check TypeScript compiler output
569
-
570
- ## Resources
571
-
572
- - [TypeScript Documentation](https://www.typescriptlang.org/docs/)
573
- - [Node.js Documentation](https://nodejs.org/docs/)
574
- - [Docker Documentation](https://docs.docker.com/)
575
- - [Nginx Documentation](https://nginx.org/en/docs/)
576
- - [Certbot Documentation](https://eff-certbot.readthedocs.io/)
577
-
578
- ## Getting Help
579
-
580
- - Check existing [documentation](./README.md)
581
- - Review [troubleshooting guide](./troubleshooting.md)
582
- - Open an issue on GitHub
583
- - Check [API reference](./api-reference.md) for function details
584
-
585
- ---
586
-
587
- Happy coding! 🚀
588
-