servherd 0.0.1 → 1.0.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.
Files changed (95) hide show
  1. package/CONTRIBUTING.md +250 -0
  2. package/LICENSE +21 -0
  3. package/README.md +653 -29
  4. package/dist/cli/commands/config.d.ts +35 -0
  5. package/dist/cli/commands/config.js +336 -0
  6. package/dist/cli/commands/info.d.ts +37 -0
  7. package/dist/cli/commands/info.js +98 -0
  8. package/dist/cli/commands/list.d.ts +26 -0
  9. package/dist/cli/commands/list.js +86 -0
  10. package/dist/cli/commands/logs.d.ts +46 -0
  11. package/dist/cli/commands/logs.js +292 -0
  12. package/dist/cli/commands/mcp.d.ts +5 -0
  13. package/dist/cli/commands/mcp.js +17 -0
  14. package/dist/cli/commands/refresh.d.ts +20 -0
  15. package/dist/cli/commands/refresh.js +139 -0
  16. package/dist/cli/commands/remove.d.ts +20 -0
  17. package/dist/cli/commands/remove.js +144 -0
  18. package/dist/cli/commands/restart.d.ts +25 -0
  19. package/dist/cli/commands/restart.js +177 -0
  20. package/dist/cli/commands/start.d.ts +37 -0
  21. package/dist/cli/commands/start.js +293 -0
  22. package/dist/cli/commands/stop.d.ts +20 -0
  23. package/dist/cli/commands/stop.js +108 -0
  24. package/dist/cli/index.d.ts +9 -0
  25. package/dist/cli/index.js +160 -0
  26. package/dist/cli/output/formatters.d.ts +117 -0
  27. package/dist/cli/output/formatters.js +454 -0
  28. package/dist/cli/output/json-formatter.d.ts +22 -0
  29. package/dist/cli/output/json-formatter.js +40 -0
  30. package/dist/index.d.ts +15 -0
  31. package/dist/index.js +25 -0
  32. package/dist/mcp/index.d.ts +14 -0
  33. package/dist/mcp/index.js +352 -0
  34. package/dist/mcp/resources/servers.d.ts +14 -0
  35. package/dist/mcp/resources/servers.js +128 -0
  36. package/dist/mcp/tools/config.d.ts +33 -0
  37. package/dist/mcp/tools/config.js +88 -0
  38. package/dist/mcp/tools/info.d.ts +36 -0
  39. package/dist/mcp/tools/info.js +65 -0
  40. package/dist/mcp/tools/list.d.ts +36 -0
  41. package/dist/mcp/tools/list.js +49 -0
  42. package/dist/mcp/tools/logs.d.ts +44 -0
  43. package/dist/mcp/tools/logs.js +55 -0
  44. package/dist/mcp/tools/refresh.d.ts +33 -0
  45. package/dist/mcp/tools/refresh.js +54 -0
  46. package/dist/mcp/tools/remove.d.ts +23 -0
  47. package/dist/mcp/tools/remove.js +43 -0
  48. package/dist/mcp/tools/restart.d.ts +23 -0
  49. package/dist/mcp/tools/restart.js +42 -0
  50. package/dist/mcp/tools/start.d.ts +38 -0
  51. package/dist/mcp/tools/start.js +73 -0
  52. package/dist/mcp/tools/stop.d.ts +23 -0
  53. package/dist/mcp/tools/stop.js +40 -0
  54. package/dist/services/config.service.d.ts +80 -0
  55. package/dist/services/config.service.js +227 -0
  56. package/dist/services/port.service.d.ts +82 -0
  57. package/dist/services/port.service.js +151 -0
  58. package/dist/services/process.service.d.ts +61 -0
  59. package/dist/services/process.service.js +220 -0
  60. package/dist/services/registry.service.d.ts +50 -0
  61. package/dist/services/registry.service.js +157 -0
  62. package/dist/types/config.d.ts +107 -0
  63. package/dist/types/config.js +44 -0
  64. package/dist/types/errors.d.ts +102 -0
  65. package/dist/types/errors.js +197 -0
  66. package/dist/types/pm2.d.ts +50 -0
  67. package/dist/types/pm2.js +4 -0
  68. package/dist/types/registry.d.ts +230 -0
  69. package/dist/types/registry.js +33 -0
  70. package/dist/utils/ci-detector.d.ts +31 -0
  71. package/dist/utils/ci-detector.js +68 -0
  72. package/dist/utils/config-drift.d.ts +71 -0
  73. package/dist/utils/config-drift.js +128 -0
  74. package/dist/utils/error-handler.d.ts +21 -0
  75. package/dist/utils/error-handler.js +38 -0
  76. package/dist/utils/log-follower.d.ts +10 -0
  77. package/dist/utils/log-follower.js +98 -0
  78. package/dist/utils/logger.d.ts +11 -0
  79. package/dist/utils/logger.js +24 -0
  80. package/dist/utils/names.d.ts +7 -0
  81. package/dist/utils/names.js +20 -0
  82. package/dist/utils/template.d.ts +88 -0
  83. package/dist/utils/template.js +180 -0
  84. package/dist/utils/time-parser.d.ts +19 -0
  85. package/dist/utils/time-parser.js +54 -0
  86. package/docs/ci-cd.md +408 -0
  87. package/docs/configuration.md +325 -0
  88. package/docs/mcp-integration.md +411 -0
  89. package/examples/basic-usage/README.md +187 -0
  90. package/examples/ci-github-actions/workflow.yml +195 -0
  91. package/examples/mcp-claude-code/README.md +213 -0
  92. package/examples/multi-server/README.md +270 -0
  93. package/examples/storybook/README.md +187 -0
  94. package/examples/vite-project/README.md +251 -0
  95. package/package.json +123 -6
@@ -0,0 +1,325 @@
1
+ # Configuration Reference
2
+
3
+ servherd uses a layered configuration system that allows settings to be defined at multiple levels with clear precedence.
4
+
5
+ ## Configuration Precedence
6
+
7
+ Settings are resolved in this order (highest priority first):
8
+
9
+ 1. **Command-line arguments** - Override everything
10
+ 2. **Environment variables** - Override file-based config
11
+ 3. **Project config file** - `.servherdrc.json` in project root
12
+ 4. **User config file** - `~/.servherd/config.json`
13
+ 5. **Default values** - Built-in defaults
14
+
15
+ ## Global Configuration File
16
+
17
+ **Location:** `~/.servherd/config.json`
18
+
19
+ This file stores user-level configuration that applies to all projects.
20
+
21
+ ### Full Schema
22
+
23
+ ```json
24
+ {
25
+ "version": "1",
26
+ "hostname": "localhost",
27
+ "protocol": "http",
28
+ "portRange": {
29
+ "min": 3000,
30
+ "max": 9999
31
+ },
32
+ "tempDir": "~/.servherd/tmp",
33
+ "pm2": {
34
+ "logDir": "~/.servherd/logs",
35
+ "pidDir": "~/.servherd/pids"
36
+ },
37
+ "httpsCert": "/path/to/cert.pem",
38
+ "httpsKey": "/path/to/key.pem",
39
+ "refreshOnChange": "on-start"
40
+ }
41
+ ```
42
+
43
+ ### Configuration Options
44
+
45
+ #### `version`
46
+
47
+ - **Type:** `string`
48
+ - **Default:** `"1"`
49
+ - **Description:** Configuration file format version. Used for future migrations.
50
+
51
+ #### `hostname`
52
+
53
+ - **Type:** `string`
54
+ - **Default:** `"localhost"`
55
+ - **Description:** Default hostname used for server URLs and the `{{hostname}}` template variable.
56
+
57
+ **Example:**
58
+ ```json
59
+ {
60
+ "hostname": "dev.local"
61
+ }
62
+ ```
63
+
64
+ #### `protocol`
65
+
66
+ - **Type:** `"http" | "https"`
67
+ - **Default:** `"http"`
68
+ - **Description:** Default protocol for server URLs.
69
+
70
+ #### `portRange.min`
71
+
72
+ - **Type:** `number`
73
+ - **Default:** `3000`
74
+ - **Description:** Minimum port number for automatic port assignment.
75
+
76
+ #### `portRange.max`
77
+
78
+ - **Type:** `number`
79
+ - **Default:** `9999`
80
+ - **Description:** Maximum port number for automatic port assignment.
81
+
82
+ **Example - Restrict to specific range:**
83
+ ```json
84
+ {
85
+ "portRange": {
86
+ "min": 8000,
87
+ "max": 8999
88
+ }
89
+ }
90
+ ```
91
+
92
+ #### `tempDir`
93
+
94
+ - **Type:** `string`
95
+ - **Default:** `"~/.servherd/tmp"`
96
+ - **Description:** Directory for temporary files.
97
+
98
+ #### `pm2.logDir`
99
+
100
+ - **Type:** `string`
101
+ - **Default:** `"~/.servherd/logs"`
102
+ - **Description:** Directory where PM2 stores server log files.
103
+
104
+ #### `pm2.pidDir`
105
+
106
+ - **Type:** `string`
107
+ - **Default:** `"~/.servherd/pids"`
108
+ - **Description:** Directory where PM2 stores process ID files.
109
+
110
+ #### `httpsCert`
111
+
112
+ - **Type:** `string` (optional)
113
+ - **Default:** `undefined`
114
+ - **Description:** Path to HTTPS certificate file. Used when protocol is `https` and exposed via the `{{https-cert}}` template variable.
115
+
116
+ #### `httpsKey`
117
+
118
+ - **Type:** `string` (optional)
119
+ - **Default:** `undefined`
120
+ - **Description:** Path to HTTPS private key file. Used when protocol is `https` and exposed via the `{{https-key}}` template variable.
121
+
122
+ #### `refreshOnChange`
123
+
124
+ - **Type:** `"manual" | "prompt" | "auto" | "on-start"`
125
+ - **Default:** `"on-start"`
126
+ - **Description:** Controls how servers are updated when configuration values change (e.g., hostname, httpsCert, httpsKey).
127
+
128
+ | Mode | Behavior |
129
+ |------|----------|
130
+ | `manual` | No automatic refresh. Use `servherd config --refresh-all` or `--refresh <name>` to manually apply config changes. |
131
+ | `prompt` | Prompts the user when config is changed via CLI, asking if affected servers should be refreshed. |
132
+ | `auto` | Automatically refreshes all affected servers when config is changed via CLI. |
133
+ | `on-start` | Applies the new config values the next time each server is started or restarted (default). |
134
+
135
+ **Example:**
136
+ ```json
137
+ {
138
+ "refreshOnChange": "prompt"
139
+ }
140
+ ```
141
+
142
+ ## Config Drift Detection
143
+
144
+ When a server is started, servherd captures a snapshot of the configuration values it uses (based on template variables in the command). If the global configuration changes after a server is started, the server has "config drift" - it's running with old values.
145
+
146
+ ### How it Works
147
+
148
+ 1. When a server starts, servherd records which template variables the command uses (e.g., `{{hostname}}`, `{{https-cert}}`)
149
+ 2. It stores a snapshot of those config values at startup time
150
+ 3. The `servherd list` command shows a ⚡ indicator next to servers with config drift
151
+ 4. The `servherd info` command shows drift details for a specific server
152
+ 5. Use `servherd config --refresh-all` or `--refresh <name>` to restart servers with updated config values
153
+
154
+ ### Refresh Commands
155
+
156
+ ```bash
157
+ # Check which servers have config drift (look for ⚡ indicator)
158
+ servherd list
159
+
160
+ # Refresh a specific server with new config values
161
+ servherd config --refresh my-server
162
+
163
+ # Refresh all servers with config drift
164
+ servherd config --refresh-all
165
+
166
+ # Refresh servers with a specific tag
167
+ servherd config --refresh-all --tag frontend
168
+
169
+ # Preview what would be refreshed without making changes
170
+ servherd config --refresh-all --dry-run
171
+ ```
172
+
173
+ ## Server Registry
174
+
175
+ **Location:** `~/.servherd/registry.json`
176
+
177
+ The registry stores information about all managed servers. You typically don't edit this file directly.
178
+
179
+ ### Registry Structure
180
+
181
+ ```json
182
+ {
183
+ "version": "1",
184
+ "servers": {
185
+ "server-id-1": {
186
+ "id": "uuid-string",
187
+ "name": "brave-tiger",
188
+ "command": "npm start --port {{port}}",
189
+ "resolvedCommand": "npm start --port 3000",
190
+ "cwd": "/home/user/project",
191
+ "port": 3000,
192
+ "protocol": "http",
193
+ "hostname": "localhost",
194
+ "env": {},
195
+ "tags": ["frontend", "dev"],
196
+ "description": "Main development server",
197
+ "createdAt": "2024-01-15T10:30:00.000Z",
198
+ "pm2Name": "servherd-brave-tiger"
199
+ }
200
+ }
201
+ }
202
+ ```
203
+
204
+ ### Server Entry Fields
205
+
206
+ | Field | Type | Description |
207
+ |-------|------|-------------|
208
+ | `id` | string | Unique identifier (UUID) |
209
+ | `name` | string | Human-readable name |
210
+ | `command` | string | Original command with templates |
211
+ | `resolvedCommand` | string | Command with templates resolved |
212
+ | `cwd` | string | Working directory |
213
+ | `port` | number | Assigned port number |
214
+ | `protocol` | string | Protocol (http/https) |
215
+ | `hostname` | string | Hostname |
216
+ | `env` | object | Environment variables |
217
+ | `tags` | string[] | Tags for organization |
218
+ | `description` | string | Server description |
219
+ | `createdAt` | string | ISO timestamp of creation |
220
+ | `pm2Name` | string | PM2 process name |
221
+
222
+ ## Environment Variables
223
+
224
+ Override any configuration option using environment variables:
225
+
226
+ ### Core Settings
227
+
228
+ | Variable | Config Key | Example |
229
+ |----------|------------|---------|
230
+ | `SERVHERD_HOSTNAME` | `hostname` | `dev.local` |
231
+ | `SERVHERD_PROTOCOL` | `protocol` | `https` |
232
+ | `SERVHERD_PORT_MIN` | `portRange.min` | `8000` |
233
+ | `SERVHERD_PORT_MAX` | `portRange.max` | `8999` |
234
+
235
+ ### Directory Settings
236
+
237
+ | Variable | Config Key | Example |
238
+ |----------|------------|---------|
239
+ | `SERVHERD_CONFIG_DIR` | config directory | `/opt/servherd` |
240
+ | `SERVHERD_TEMP_DIR` | `tempDir` | `/tmp/servherd` |
241
+ | `SERVHERD_LOG_DIR` | `pm2.logDir` | `/var/log/servherd` |
242
+ | `SERVHERD_PID_DIR` | `pm2.pidDir` | `/var/run/servherd` |
243
+
244
+ ### Behavior Settings
245
+
246
+ | Variable | Description | Values |
247
+ |----------|-------------|--------|
248
+ | `CI` | Enable CI mode | `true` |
249
+ | `LOG_LEVEL` | Logging verbosity | `debug`, `info`, `warn`, `error` |
250
+ | `NO_COLOR` | Disable color output | `1` |
251
+
252
+ ## Template Variables
253
+
254
+ Template variables can be used in server commands and environment variables:
255
+
256
+ | Variable | Description | Example Value |
257
+ |----------|-------------|---------------|
258
+ | `{{port}}` | Assigned port number | `3000` |
259
+ | `{{hostname}}` | Configured hostname | `localhost` |
260
+ | `{{url}}` | Full URL | `http://localhost:3000` |
261
+ | `{{https-cert}}` | Path to HTTPS certificate | `/path/to/cert.pem` |
262
+ | `{{https-key}}` | Path to HTTPS private key | `/path/to/key.pem` |
263
+
264
+ **Usage Example:**
265
+ ```bash
266
+ # Basic server with port and hostname
267
+ servherd start -- npm run dev --port {{port}} --host {{hostname}}
268
+
269
+ # HTTPS server with certificate paths
270
+ servherd start -- node server.js --cert {{https-cert}} --key {{https-key}}
271
+ ```
272
+
273
+ ## Port Assignment Algorithm
274
+
275
+ Ports are assigned deterministically using FNV-1a hashing:
276
+
277
+ 1. Hash the combination of `cwd` (working directory) and `command`
278
+ 2. Map the hash to the configured port range
279
+ 3. Check if the port is available
280
+ 4. If not, increment and retry (up to 100 attempts)
281
+
282
+ This means:
283
+ - The same project/command always gets the same port (if available)
284
+ - Different projects get different ports
285
+ - Ports are predictable across sessions
286
+
287
+ ## Managing Configuration via CLI
288
+
289
+ ```bash
290
+ # View all configuration
291
+ servherd config --show
292
+
293
+ # Get a specific value
294
+ servherd config --get hostname
295
+ servherd config --get portRange.min
296
+
297
+ # Set a value
298
+ servherd config --set hostname --value myhost.local
299
+ servherd config --set portRange.min --value 4000
300
+ servherd config --set refreshOnChange --value prompt
301
+
302
+ # Reset to defaults
303
+ servherd config --reset
304
+
305
+ # Refresh servers with config drift
306
+ servherd config --refresh my-server # Refresh specific server
307
+ servherd config --refresh-all # Refresh all servers with drift
308
+ servherd config --refresh-all --tag api # Refresh by tag
309
+ servherd config --refresh-all --dry-run # Preview changes
310
+ ```
311
+
312
+ ## CI/CD Configuration
313
+
314
+ For CI environments, use environment variables:
315
+
316
+ ```yaml
317
+ # GitHub Actions example
318
+ env:
319
+ CI: true
320
+ SERVHERD_HOSTNAME: ci.local
321
+ SERVHERD_PORT_MIN: 8000
322
+ SERVHERD_PORT_MAX: 8100
323
+ ```
324
+
325
+ See [CI/CD Integration](ci-cd.md) for detailed examples.