@simplens/onboard 1.0.1 → 1.0.2

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 (82) hide show
  1. package/README.md +331 -214
  2. package/dist/__tests__/env-config.test.d.ts +2 -0
  3. package/dist/__tests__/env-config.test.d.ts.map +1 -0
  4. package/dist/__tests__/env-config.test.js +23 -0
  5. package/dist/__tests__/env-config.test.js.map +1 -0
  6. package/dist/__tests__/infra-prompts.test.d.ts +2 -0
  7. package/dist/__tests__/infra-prompts.test.d.ts.map +1 -0
  8. package/dist/__tests__/infra-prompts.test.js +43 -0
  9. package/dist/__tests__/infra-prompts.test.js.map +1 -0
  10. package/dist/__tests__/infra.test.d.ts +2 -0
  11. package/dist/__tests__/infra.test.d.ts.map +1 -0
  12. package/dist/__tests__/infra.test.js +14 -0
  13. package/dist/__tests__/infra.test.js.map +1 -0
  14. package/dist/__tests__/nginx.test.d.ts +2 -0
  15. package/dist/__tests__/nginx.test.d.ts.map +1 -0
  16. package/dist/__tests__/nginx.test.js +16 -0
  17. package/dist/__tests__/nginx.test.js.map +1 -0
  18. package/dist/env-config.d.ts +27 -12
  19. package/dist/env-config.d.ts.map +1 -1
  20. package/dist/env-config.js +253 -128
  21. package/dist/env-config.js.map +1 -1
  22. package/dist/index.js +340 -69
  23. package/dist/index.js.map +1 -1
  24. package/dist/infra.d.ts +19 -8
  25. package/dist/infra.d.ts.map +1 -1
  26. package/dist/infra.js +267 -128
  27. package/dist/infra.js.map +1 -1
  28. package/dist/plugins.d.ts +5 -10
  29. package/dist/plugins.d.ts.map +1 -1
  30. package/dist/plugins.js +75 -44
  31. package/dist/plugins.js.map +1 -1
  32. package/dist/services.d.ts +1 -23
  33. package/dist/services.d.ts.map +1 -1
  34. package/dist/services.js +47 -62
  35. package/dist/services.js.map +1 -1
  36. package/dist/templates.d.ts +2 -1
  37. package/dist/templates.d.ts.map +1 -1
  38. package/dist/templates.js +203 -191
  39. package/dist/templates.js.map +1 -1
  40. package/dist/types/domain.d.ts +2 -0
  41. package/dist/types/domain.d.ts.map +1 -1
  42. package/dist/ui.d.ts +45 -0
  43. package/dist/ui.d.ts.map +1 -0
  44. package/dist/ui.js +93 -0
  45. package/dist/ui.js.map +1 -0
  46. package/dist/utils/logger.d.ts +1 -0
  47. package/dist/utils/logger.d.ts.map +1 -1
  48. package/dist/utils/logger.js +32 -7
  49. package/dist/utils/logger.js.map +1 -1
  50. package/dist/utils.d.ts +8 -0
  51. package/dist/utils.d.ts.map +1 -1
  52. package/dist/utils.js +66 -2
  53. package/dist/utils.js.map +1 -1
  54. package/dist/validators.d.ts +1 -52
  55. package/dist/validators.d.ts.map +1 -1
  56. package/dist/validators.js +10 -57
  57. package/dist/validators.js.map +1 -1
  58. package/package.json +3 -5
  59. package/src/__tests__/env-config.test.ts +28 -0
  60. package/src/__tests__/errors.test.ts +187 -187
  61. package/src/__tests__/infra-prompts.test.ts +54 -0
  62. package/src/__tests__/infra.test.ts +15 -0
  63. package/src/__tests__/utils.test.ts +142 -142
  64. package/src/__tests__/validators.test.ts +195 -195
  65. package/src/config/constants.ts +86 -86
  66. package/src/config/index.ts +1 -1
  67. package/src/env-config.ts +455 -311
  68. package/src/index.ts +534 -202
  69. package/src/infra.ts +404 -245
  70. package/src/plugins.ts +221 -190
  71. package/src/services.ts +175 -190
  72. package/src/templates.ts +209 -196
  73. package/src/types/domain.ts +129 -127
  74. package/src/types/errors.ts +173 -173
  75. package/src/types/index.ts +2 -2
  76. package/src/ui.ts +91 -0
  77. package/src/utils/index.ts +1 -1
  78. package/src/utils/logger.ts +144 -118
  79. package/src/utils.ts +183 -105
  80. package/src/validators.ts +145 -192
  81. package/tsconfig.json +18 -18
  82. package/vitest.config.ts +22 -22
package/src/env-config.ts CHANGED
@@ -1,311 +1,455 @@
1
- import inquirer from 'inquirer';
2
- import { readFile, writeFile, appendFile, logInfo, logSuccess, logWarning } from './utils.js';
3
- import path from 'path';
4
- import { CRITICAL_ENV_KEYS, VALIDATION } from './config/constants.js';
5
- import type { EnvVariable } from './types/domain.js';
6
-
7
- /**
8
- * Load and parse .env.example from embedded template
9
- */
10
- export async function loadEnvExample(): Promise<EnvVariable[]> {
11
- // Embedded .env template - always available regardless of installation
12
- const envTemplate = `
13
- NODE_ENV=production
14
- # ============================================
15
- # API SERVER
16
- # ============================================
17
- NS_API_KEY=
18
- PORT=3000
19
- MAX_BATCH_REQ_LIMIT=1000
20
-
21
- # ============================================
22
- # DATABASE
23
- # ============================================
24
- MONGO_URI=
25
-
26
- # ============================================
27
- # KAFKA
28
- # ============================================
29
- BROKERS=
30
-
31
- # Kafka Topic Partitions (Core Topics)
32
- DELAYED_PARTITION=1
33
- NOTIFICATION_STATUS_PARTITION=1
34
-
35
- # ============================================
36
- # REDIS
37
- # ============================================
38
- REDIS_URL=
39
-
40
- # ============================================
41
- # PLUGIN SYSTEM
42
- # ============================================
43
- SIMPLENS_CONFIG_PATH=./simplens.config.yaml
44
- PROCESSOR_CHANNEL=all
45
-
46
- # ============================================
47
- # BACKGROUND WORKER
48
- # ============================================
49
- OUTBOX_POLL_INTERVAL_MS=5000
50
- OUTBOX_CLEANUP_INTERVAL_MS=60000
51
- OUTBOX_BATCH_SIZE=100
52
- OUTBOX_RETENTION_MS=300000
53
- OUTBOX_CLAIM_TIMEOUT_MS=30000
54
-
55
- # ============================================
56
- # RETRY & IDEMPOTENCY
57
- # ============================================
58
- IDEMPOTENCY_TTL_SECONDS=86400
59
- MAX_RETRY_COUNT=5
60
- PROCESSING_TTL_SECONDS=120
61
-
62
- # ============================================
63
- # DELAYED NOTIFICATIONS
64
- # ============================================
65
- DELAYED_POLL_INTERVAL_MS=1000
66
- DELAYED_BATCH_SIZE=10
67
- MAX_POLLER_RETRIES=3
68
-
69
- # ============================================
70
- # RECOVERY SERVICE
71
- # ============================================
72
- RECOVERY_POLL_INTERVAL_MS=60000
73
- PROCESSING_STUCK_THRESHOLD_MS=300000
74
- PENDING_STUCK_THRESHOLD_MS=300000
75
- RECOVERY_BATCH_SIZE=50
76
- RECOVERY_CLAIM_TIMEOUT_MS=60000
77
-
78
- # ============================================
79
- # CLEANUP
80
- # ============================================
81
- CLEANUP_RESOLVED_ALERTS_RETENTION_MS=86400000
82
- CLEANUP_PROCESSED_STATUS_OUTBOX_RETENTION_MS=86400000
83
-
84
- # ============================================
85
- # LOGGING
86
- # ============================================
87
- LOKI_URL=
88
- LOG_LEVEL=info
89
- LOG_TO_FILE=true
90
-
91
- # ============================================
92
- # ADMIN DASHBOARD
93
- # ============================================
94
- AUTH_SECRET=
95
- ADMIN_USERNAME=admin
96
- ADMIN_PASSWORD=
97
- AUTH_TRUST_HOST=true
98
- API_BASE_URL=http://api:3000
99
- WEBHOOK_HOST=dashboard
100
- WEBHOOK_PORT=3002
101
- DASHBOARD_PORT=3002
102
- `;
103
-
104
- return parseEnvContent(envTemplate);
105
- }
106
-
107
- /**
108
- * Parse .env content into structured format
109
- */
110
- function parseEnvContent(content: string): EnvVariable[] {
111
- const lines = content.split('\n');
112
- const variables: EnvVariable[] = [];
113
- let currentComment = '';
114
-
115
- for (const line of lines) {
116
- const trimmed = line.trim();
117
-
118
- // Capture comments as descriptions
119
- if (trimmed.startsWith('#') && !trimmed.includes('====')) {
120
- currentComment = trimmed.replace(/^#\s*/, '');
121
- continue;
122
- }
123
-
124
- // Skip empty lines and section dividers
125
- if (!trimmed || trimmed.includes('====')) {
126
- currentComment = '';
127
- continue;
128
- }
129
-
130
- // Parse key=value pairs
131
- const match = trimmed.match(/^([A-Z_]+)=(.*)$/);
132
- if (match) {
133
- const [, key, value] = match;
134
- variables.push({
135
- key,
136
- value: value || '',
137
- description: currentComment || undefined,
138
- required: CRITICAL_ENV_KEYS.includes(key) || !value,
139
- });
140
- currentComment = '';
141
- }
142
- }
143
-
144
- return variables;
145
- }
146
-
147
- /**
148
- * Prompts user for environment variable values based on the selected mode.
149
- *
150
- * @param mode - 'default' prompts only for critical vars, 'interactive' prompts for all
151
- * @param infraServices - List of selected infrastructure service IDs
152
- * @param infraHost - Host for infrastructure services
153
- * @returns Map of environment variable keys to values
154
- *
155
- * @remarks
156
- * Critical variables (always prompted): NS_API_KEY, MONGO_URI, BROKERS, etc.
157
- * Interactive mode prompts for all variables including optional ones.
158
- *
159
- * @example
160
- * ```ts
161
- * const envVars = await promptEnvVariables('default', ['mongo', 'kafka'], 'localhost');
162
- * // Prompts only for critical variables
163
- * ```
164
- */
165
- export async function promptEnvVariables(
166
- mode: 'default' | 'interactive',
167
- infraServices: string[]
168
- ): Promise<Map<string, string>> {
169
- logInfo('Configuring environment variables...');
170
-
171
- const envVars = await loadEnvExample();
172
- const result = new Map<string, string>();
173
-
174
- // Auto-fill infra connection URLs based on selected services using Docker service names
175
- const autoInfraUrls: Record<string, string> = {
176
- MONGO_URI: infraServices.includes('mongo')
177
- ? `mongodb://mongo:27017/simplens?replicaSet=rs0`
178
- : '',
179
- BROKERS: infraServices.includes('kafka')
180
- ? 'kafka:9093'
181
- : '',
182
- REDIS_URL: infraServices.includes('redis')
183
- ? 'redis://redis:6379'
184
- : '',
185
- LOKI_URL: infraServices.includes('loki')
186
- ? 'http://loki:3100'
187
- : '',
188
- };
189
-
190
- if (mode === 'default') {
191
- // Use defaults, only prompt for critical values
192
- for (const envVar of envVars) {
193
- // Use auto-filled infra URLs
194
- if (autoInfraUrls[envVar.key]) {
195
- result.set(envVar.key, autoInfraUrls[envVar.key]);
196
- continue;
197
- }
198
-
199
- // Use default value if available
200
- if (envVar.value && !CRITICAL_ENV_KEYS.includes(envVar.key)) {
201
- result.set(envVar.key, envVar.value);
202
- continue;
203
- }
204
-
205
- // Prompt for critical values (only if not auto-filled)
206
- if (CRITICAL_ENV_KEYS.includes(envVar.key)) {
207
- const answer = await inquirer.prompt<{ value: string }>([
208
- {
209
- type: envVar.key.includes('PASSWORD') ? 'password' : 'input',
210
- name: 'value',
211
- message: `${envVar.key}${envVar.description ? ` (${envVar.description})` : ''}:`,
212
- default: getSuggestedValue(envVar.key),
213
- validate: (input: string) => {
214
- if (!input && envVar.required) {
215
- return `${envVar.key} is required`;
216
- }
217
- return true;
218
- },
219
- },
220
- ]);
221
- result.set(envVar.key, answer.value);
222
- }
223
- }
224
- } else {
225
- // Interactive mode: prompt for everything
226
- logInfo('Interactive mode: You will be prompted for each environment variable.');
227
-
228
- for (const envVar of envVars) {
229
- const defaultValue = autoInfraUrls[envVar.key] || envVar.value || getSuggestedValue(envVar.key);
230
-
231
- const answer = await inquirer.prompt<{ value: string }>([
232
- {
233
- type: envVar.key.includes('PASSWORD') ? 'password' : 'input',
234
- name: 'value',
235
- message: `${envVar.key}${envVar.description ? ` (${envVar.description})` : ''}:`,
236
- default: defaultValue,
237
- validate: (input: string) => {
238
- if (!input && envVar.required) {
239
- return `${envVar.key} is required`;
240
- }
241
- return true;
242
- },
243
- },
244
- ]);
245
- result.set(envVar.key, answer.value);
246
- }
247
- }
248
-
249
- logSuccess('Environment variables configured');
250
- return result;
251
- }
252
-
253
- /**
254
- * Get suggested value for specific keys
255
- */
256
- function getSuggestedValue(key: string): string {
257
- if (key === 'NS_API_KEY' || key === 'AUTH_SECRET') {
258
- return `Replace with: openssl rand -base64 32`;
259
- }
260
- if (key === 'NODE_ENV') {
261
- return 'production';
262
- }
263
- if (key === 'ADMIN_USERNAME') {
264
- return 'admin';
265
- }
266
- return '';
267
- }
268
-
269
- /**
270
- * Generate .env file from variables
271
- */
272
- export async function generateEnvFile(
273
- targetDir: string,
274
- envVars: Map<string, string>
275
- ): Promise<void> {
276
- const envPath = path.join(targetDir, '.env');
277
-
278
- let content = '# SimpleNS Environment Configuration\n';
279
- content += '# Generated by @simplens/onboard\n\n';
280
-
281
- for (const [key, value] of envVars.entries()) {
282
- content += `${key}=${value}\n`;
283
- }
284
-
285
- await writeFile(envPath, content);
286
- logSuccess('Generated .env file');
287
- }
288
-
289
- /**
290
- * Append plugin credentials to .env file
291
- */
292
- export async function appendPluginEnv(
293
- targetDir: string,
294
- pluginEnvVars: Map<string, string>
295
- ): Promise<void> {
296
- // Only append if there are actually credentials to add
297
- if (pluginEnvVars.size === 0) {
298
- logInfo('No plugin credentials to add');
299
- return;
300
- }
301
-
302
- const envPath = path.join(targetDir, '.env');
303
-
304
- let content = '\n# Plugin Credentials\n';
305
- for (const [key, value] of pluginEnvVars.entries()) {
306
- content += `${key}=${value}\n`;
307
- }
308
-
309
- await appendFile(envPath, content);
310
- logSuccess('Added plugin credentials to .env');
311
- }
1
+ import { writeFile, appendFile, logInfo, logSuccess, logWarning } from './utils.js';
2
+ import { text, password } from '@clack/prompts';
3
+ import { handleCancel } from './ui.js';
4
+ import path from 'path';
5
+ import crypto from 'crypto';
6
+ import { CRITICAL_ENV_KEYS } from './config/constants.js';
7
+ import type { EnvVariable } from './types/domain.js';
8
+
9
+ export const DEFAULT_BASE_PATH = '';
10
+
11
+ /**
12
+ * Generate a secure random string for credentials
13
+ */
14
+ export function generateSecureRandom(length: number = 32): string {
15
+ return crypto.randomBytes(length).toString('base64').slice(0, length);
16
+ }
17
+
18
+ /**
19
+ * Generate default value for a critical environment variable
20
+ */
21
+ export function generateDefaultValue(key: string): string {
22
+ if (key === 'NS_API_KEY') {
23
+ return `sk_${generateSecureRandom(48)}`;
24
+ }
25
+ if (key === 'AUTH_SECRET') {
26
+ return generateSecureRandom(64);
27
+ }
28
+ if (key === 'ADMIN_PASSWORD') {
29
+ return `Admin${generateSecureRandom(16)}`;
30
+ }
31
+ return '';
32
+ }
33
+
34
+ /**
35
+ * Validate BASE_PATH value.
36
+ * Accepts:
37
+ * - Empty value for root path
38
+ * - Slash-prefixed lowercase segments (e.g. /dashboard, /admin/v1)
39
+ */
40
+ export function validateBasePath(input: string): true | string {
41
+ const value = input.trim();
42
+
43
+ if (!value) {
44
+ return true;
45
+ }
46
+
47
+ if (!value.startsWith('/')) {
48
+ return 'Base path must start with / (example: /dashboard)';
49
+ }
50
+
51
+ if (value.endsWith('/')) {
52
+ return 'Base path must not end with /';
53
+ }
54
+
55
+ if (!/^\/[a-z0-9-]+(?:\/[a-z0-9-]+)*$/.test(value)) {
56
+ return 'Use lowercase letters, numbers, hyphens, and "/" separators only';
57
+ }
58
+
59
+ return true;
60
+ }
61
+
62
+ /**
63
+ * Normalize BASE_PATH for consistent downstream use.
64
+ */
65
+ export function normalizeBasePath(input: string): string {
66
+ return input.trim();
67
+ }
68
+
69
+ /**
70
+ * Prompt BASE_PATH once at the beginning of onboarding.
71
+ */
72
+ export async function promptBasePath(defaultValue: string = DEFAULT_BASE_PATH): Promise<string> {
73
+ const result = await text({
74
+ message: 'BASE_PATH for dashboard (leave empty for root, example: /dashboard):',
75
+ placeholder: defaultValue || 'leave empty for root',
76
+ defaultValue,
77
+ validate: (value: string | undefined) => {
78
+ const v = validateBasePath(value ?? '');
79
+ return v === true ? undefined : v;
80
+ },
81
+ withGuide: true,
82
+ });
83
+
84
+ handleCancel(result);
85
+ return normalizeBasePath(result as string);
86
+ }
87
+
88
+ /**
89
+ * Load and parse .env.example from embedded template
90
+ */
91
+ export async function loadEnvExample(): Promise<EnvVariable[]> {
92
+ // Embedded .env template - always available regardless of installation
93
+ const envTemplate = `
94
+ NODE_ENV=production
95
+ # ============================================
96
+ # API SERVER
97
+ # ============================================
98
+ NS_API_KEY=
99
+ PORT=3000
100
+ MAX_BATCH_REQ_LIMIT=1000
101
+
102
+ # ============================================
103
+ # DATABASE
104
+ # ============================================
105
+ MONGO_URI=
106
+
107
+ # ============================================
108
+ # KAFKA
109
+ # ============================================
110
+ BROKERS=
111
+
112
+ # Kafka Topic Partitions (Core Topics)
113
+ DELAYED_PARTITION=1
114
+ NOTIFICATION_STATUS_PARTITION=1
115
+
116
+ # ============================================
117
+ # REDIS
118
+ # ============================================
119
+ REDIS_URL=
120
+
121
+ # ============================================
122
+ # PLUGIN SYSTEM
123
+ # ============================================
124
+ SIMPLENS_CONFIG_PATH=./simplens.config.yaml
125
+ PROCESSOR_CHANNEL=all
126
+
127
+ # ============================================
128
+ # BACKGROUND WORKER
129
+ # ============================================
130
+ OUTBOX_POLL_INTERVAL_MS=5000
131
+ OUTBOX_CLEANUP_INTERVAL_MS=60000
132
+ OUTBOX_BATCH_SIZE=100
133
+ OUTBOX_RETENTION_MS=300000
134
+ OUTBOX_CLAIM_TIMEOUT_MS=30000
135
+
136
+ # ============================================
137
+ # RETRY & IDEMPOTENCY
138
+ # ============================================
139
+ IDEMPOTENCY_TTL_SECONDS=86400
140
+ MAX_RETRY_COUNT=5
141
+ PROCESSING_TTL_SECONDS=120
142
+
143
+ # ============================================
144
+ # DELAYED NOTIFICATIONS
145
+ # ============================================
146
+ DELAYED_POLL_INTERVAL_MS=1000
147
+ DELAYED_BATCH_SIZE=10
148
+ MAX_POLLER_RETRIES=3
149
+
150
+ # ============================================
151
+ # RECOVERY SERVICE
152
+ # ============================================
153
+ RECOVERY_POLL_INTERVAL_MS=60000
154
+ PROCESSING_STUCK_THRESHOLD_MS=300000
155
+ PENDING_STUCK_THRESHOLD_MS=300000
156
+ RECOVERY_BATCH_SIZE=50
157
+ RECOVERY_CLAIM_TIMEOUT_MS=60000
158
+
159
+ # ============================================
160
+ # CLEANUP
161
+ # ============================================
162
+ CLEANUP_RESOLVED_ALERTS_RETENTION_MS=86400000
163
+ CLEANUP_PROCESSED_STATUS_OUTBOX_RETENTION_MS=86400000
164
+
165
+ # ============================================
166
+ # LOGGING
167
+ # ============================================
168
+ LOKI_URL=
169
+ LOG_LEVEL=info
170
+ LOG_TO_FILE=true
171
+
172
+ # ============================================
173
+ # DOCKER IMAGE VERSIONS
174
+ # ============================================
175
+ CORE_VERSION=latest
176
+ DASHBOARD_VERSION=latest
177
+
178
+ # ============================================
179
+ # ADMIN DASHBOARD
180
+ # ============================================
181
+ AUTH_SECRET=
182
+ ADMIN_USERNAME=admin
183
+ ADMIN_PASSWORD=
184
+ AUTH_TRUST_HOST=true
185
+ API_BASE_URL=http://api:3000
186
+ WEBHOOK_HOST=dashboard
187
+ WEBHOOK_PORT=3002
188
+ BASE_PATH=
189
+ DASHBOARD_PORT=3002
190
+ `;
191
+
192
+ return parseEnvContent(envTemplate);
193
+ }
194
+
195
+ /**
196
+ * Parse .env content into structured format
197
+ */
198
+ function parseEnvContent(content: string): EnvVariable[] {
199
+ const lines = content.split('\n');
200
+ const variables: EnvVariable[] = [];
201
+ let currentComment = '';
202
+
203
+ for (const line of lines) {
204
+ const trimmed = line.trim();
205
+
206
+ // Capture comments as descriptions
207
+ if (trimmed.startsWith('#') && !trimmed.includes('====')) {
208
+ currentComment = trimmed.replace(/^#\s*/, '');
209
+ continue;
210
+ }
211
+
212
+ // Skip empty lines and section dividers
213
+ if (!trimmed || trimmed.includes('====')) {
214
+ currentComment = '';
215
+ continue;
216
+ }
217
+
218
+ // Parse key=value pairs
219
+ const match = trimmed.match(/^([A-Z_]+)=(.*)$/);
220
+ if (match) {
221
+ const [, key, value] = match;
222
+ variables.push({
223
+ key,
224
+ value: value || '',
225
+ description: currentComment || undefined,
226
+ required: CRITICAL_ENV_KEYS.includes(key) || !value,
227
+ });
228
+ currentComment = '';
229
+ }
230
+ }
231
+
232
+ return variables;
233
+ }
234
+
235
+ /**
236
+ * Prompts user for environment variable values based on the selected mode.
237
+ *
238
+ * @param mode - 'default' prompts only for critical vars, 'interactive' prompts for all
239
+ * @param infraServices - List of selected infrastructure service IDs
240
+ * @param basePath - BASE_PATH value already collected
241
+ * @param fullMode - If true, auto-generate critical values without prompting
242
+ * @returns Map of environment variable keys to values
243
+ */
244
+ export async function promptEnvVariables(
245
+ mode: 'default' | 'interactive',
246
+ infraServices: string[],
247
+ basePath: string = DEFAULT_BASE_PATH,
248
+ fullMode: boolean = false
249
+ ): Promise<Map<string, string>> {
250
+ logInfo('Configuring environment variables...');
251
+
252
+ const envVars = await loadEnvExample();
253
+ const result = new Map<string, string>();
254
+ const normalizedBasePath = normalizeBasePath(basePath);
255
+ const basePathLabel = normalizedBasePath || '(root)';
256
+ logInfo(`BASE_PATH selected: ${basePathLabel}`);
257
+
258
+ // Auto-fill infra connection URLs based on selected services using Docker service names
259
+ const autoInfraUrls: Record<string, string> = {
260
+ MONGO_URI: infraServices.includes('mongo')
261
+ ? `mongodb://mongo:27017/simplens?replicaSet=rs0`
262
+ : '',
263
+ BROKERS: infraServices.includes('kafka')
264
+ ? 'kafka:9093'
265
+ : '',
266
+ REDIS_URL: infraServices.includes('redis')
267
+ ? 'redis://redis:6379'
268
+ : '',
269
+ LOKI_URL: infraServices.includes('loki')
270
+ ? 'http://loki:3100'
271
+ : '',
272
+ };
273
+
274
+ if (mode === 'default') {
275
+ // Use defaults, only prompt for critical values
276
+ for (const envVar of envVars) {
277
+ // Use auto-filled infra URLs
278
+ if (autoInfraUrls[envVar.key]) {
279
+ result.set(envVar.key, autoInfraUrls[envVar.key]);
280
+ continue;
281
+ }
282
+
283
+ // BASE_PATH is collected upfront in onboarding flow
284
+ if (envVar.key === 'BASE_PATH') {
285
+ result.set(envVar.key, normalizedBasePath);
286
+ continue;
287
+ }
288
+
289
+ // Use default value if available
290
+ if (envVar.value && !CRITICAL_ENV_KEYS.includes(envVar.key)) {
291
+ result.set(envVar.key, envVar.value);
292
+ continue;
293
+ }
294
+
295
+ // Prompt for critical values (only if not auto-filled)
296
+ if (CRITICAL_ENV_KEYS.includes(envVar.key)) {
297
+ if (fullMode) {
298
+ // In full mode, auto-generate critical values
299
+ const defaultValue = generateDefaultValue(envVar.key);
300
+ if (defaultValue) {
301
+ result.set(envVar.key, defaultValue);
302
+ }
303
+ } else {
304
+ // Show changelog info for version variables
305
+ if (envVar.key === 'CORE_VERSION' || envVar.key === 'DASHBOARD_VERSION') {
306
+ logInfo('ℹ️ Visit https://simplens.in/changelog for version information');
307
+ }
308
+
309
+ const promptMessage = `${envVar.key}${envVar.description ? ` (${envVar.description})` : ''}:`;
310
+ const isPasswordField = envVar.key.includes('PASSWORD');
311
+
312
+ let answer: string | symbol;
313
+ if (isPasswordField) {
314
+ answer = await password({
315
+ message: promptMessage,
316
+ validate: (input: string | undefined) => {
317
+ if (!input && envVar.required) {
318
+ return `${envVar.key} is required`;
319
+ }
320
+ return undefined;
321
+ },
322
+ });
323
+ } else {
324
+ answer = await text({
325
+ message: promptMessage,
326
+ placeholder: getSuggestedValue(envVar.key) || undefined,
327
+ defaultValue: getSuggestedValue(envVar.key) || undefined,
328
+ validate: (input: string | undefined) => {
329
+ if (!input && envVar.required) {
330
+ return `${envVar.key} is required`;
331
+ }
332
+ return undefined;
333
+ },
334
+ });
335
+ }
336
+
337
+ handleCancel(answer);
338
+ result.set(envVar.key, answer as string);
339
+ }
340
+ }
341
+ }
342
+ } else {
343
+ // Interactive mode: prompt for everything
344
+ logInfo('Interactive mode: You will be prompted for each environment variable.');
345
+
346
+ for (const envVar of envVars) {
347
+ const defaultValue = autoInfraUrls[envVar.key] || envVar.value || getSuggestedValue(envVar.key);
348
+
349
+ // BASE_PATH is collected upfront in onboarding flow
350
+ if (envVar.key === 'BASE_PATH') {
351
+ result.set(envVar.key, normalizedBasePath);
352
+ continue;
353
+ }
354
+
355
+ // Show changelog info for version variables
356
+ if (envVar.key === 'CORE_VERSION' || envVar.key === 'DASHBOARD_VERSION') {
357
+ logInfo('ℹ️ Visit https://simplens.in/changelog for version information');
358
+ }
359
+
360
+ const promptMessage = `${envVar.key}${envVar.description ? ` (${envVar.description})` : ''}:`;
361
+ const isPasswordField = envVar.key.includes('PASSWORD');
362
+
363
+ let answer: string | symbol;
364
+ if (isPasswordField) {
365
+ answer = await password({
366
+ message: promptMessage,
367
+ validate: (input: string | undefined) => {
368
+ if (!input && envVar.required) {
369
+ return `${envVar.key} is required`;
370
+ }
371
+ return undefined;
372
+ },
373
+ });
374
+ } else {
375
+ answer = await text({
376
+ message: promptMessage,
377
+ placeholder: defaultValue || undefined,
378
+ defaultValue: defaultValue || undefined,
379
+ validate: (input: string | undefined) => {
380
+ if (!input && envVar.required) {
381
+ return `${envVar.key} is required`;
382
+ }
383
+ return undefined;
384
+ },
385
+ });
386
+ }
387
+
388
+ handleCancel(answer);
389
+ result.set(envVar.key, answer as string);
390
+ }
391
+ }
392
+
393
+ logSuccess('Environment variables configured');
394
+ return result;
395
+ }
396
+
397
+ /**
398
+ * Get suggested value for specific keys
399
+ */
400
+ function getSuggestedValue(key: string): string {
401
+ if (key === 'NS_API_KEY' || key === 'AUTH_SECRET') {
402
+ return `Replace with: openssl rand -base64 32`;
403
+ }
404
+ if (key === 'NODE_ENV') {
405
+ return 'production';
406
+ }
407
+ if (key === 'ADMIN_USERNAME') {
408
+ return 'admin';
409
+ }
410
+ return '';
411
+ }
412
+
413
+ /**
414
+ * Generate .env file from variables
415
+ */
416
+ export async function generateEnvFile(
417
+ targetDir: string,
418
+ envVars: Map<string, string>
419
+ ): Promise<void> {
420
+ const envPath = path.join(targetDir, '.env');
421
+
422
+ let content = '# SimpleNS Environment Configuration\n';
423
+ content += '# Generated by @simplens/onboard\n\n';
424
+
425
+ for (const [key, value] of envVars.entries()) {
426
+ content += `${key}=${value}\n`;
427
+ }
428
+
429
+ await writeFile(envPath, content);
430
+ logSuccess('Generated .env file');
431
+ }
432
+
433
+ /**
434
+ * Append plugin credentials to .env file
435
+ */
436
+ export async function appendPluginEnv(
437
+ targetDir: string,
438
+ pluginEnvVars: Map<string, string>
439
+ ): Promise<void> {
440
+ // Only append if there are actually credentials to add
441
+ if (pluginEnvVars.size === 0) {
442
+ logInfo('No plugin credentials to add');
443
+ return;
444
+ }
445
+
446
+ const envPath = path.join(targetDir, '.env');
447
+
448
+ let content = '\n# Plugin Credentials\n';
449
+ for (const [key, value] of pluginEnvVars.entries()) {
450
+ content += `${key}=${value}\n`;
451
+ }
452
+
453
+ await appendFile(envPath, content);
454
+ logSuccess('Added plugin credentials to .env');
455
+ }