@aifabrix/builder 2.40.2 → 2.41.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 (103) hide show
  1. package/README.md +6 -4
  2. package/integration/hubspot/test.js +1 -1
  3. package/lib/api/credential.api.js +40 -0
  4. package/lib/api/dev.api.js +423 -0
  5. package/lib/api/types/credential.types.js +23 -0
  6. package/lib/api/types/dev.types.js +140 -0
  7. package/lib/app/config.js +21 -0
  8. package/lib/app/down.js +2 -1
  9. package/lib/app/index.js +9 -0
  10. package/lib/app/push.js +36 -12
  11. package/lib/app/readme.js +1 -3
  12. package/lib/app/run-env-compose.js +201 -0
  13. package/lib/app/run-helpers.js +121 -118
  14. package/lib/app/run.js +148 -28
  15. package/lib/app/show.js +5 -2
  16. package/lib/build/index.js +11 -3
  17. package/lib/cli/setup-app.js +140 -14
  18. package/lib/cli/setup-dev.js +180 -17
  19. package/lib/cli/setup-environment.js +4 -2
  20. package/lib/cli/setup-external-system.js +71 -21
  21. package/lib/cli/setup-infra.js +29 -2
  22. package/lib/cli/setup-secrets.js +52 -5
  23. package/lib/cli/setup-utility.js +12 -3
  24. package/lib/commands/app-install.js +172 -0
  25. package/lib/commands/app-shell.js +75 -0
  26. package/lib/commands/app-test.js +282 -0
  27. package/lib/commands/app.js +1 -1
  28. package/lib/commands/dev-cli-handlers.js +141 -0
  29. package/lib/commands/dev-down.js +114 -0
  30. package/lib/commands/dev-init.js +309 -0
  31. package/lib/commands/secrets-list.js +118 -0
  32. package/lib/commands/secrets-remove.js +97 -0
  33. package/lib/commands/secrets-set.js +30 -17
  34. package/lib/commands/secrets-validate.js +50 -0
  35. package/lib/commands/up-dataplane.js +2 -2
  36. package/lib/commands/up-miso.js +0 -25
  37. package/lib/commands/upload.js +26 -1
  38. package/lib/core/admin-secrets.js +96 -0
  39. package/lib/core/secrets-ensure.js +378 -0
  40. package/lib/core/secrets-env-write.js +157 -0
  41. package/lib/core/secrets.js +147 -81
  42. package/lib/datasource/field-reference-validator.js +91 -0
  43. package/lib/datasource/validate.js +21 -3
  44. package/lib/deployment/environment-config.js +137 -0
  45. package/lib/deployment/environment.js +21 -98
  46. package/lib/deployment/push.js +32 -2
  47. package/lib/external-system/download.js +7 -0
  48. package/lib/external-system/test-auth.js +7 -3
  49. package/lib/external-system/test.js +5 -1
  50. package/lib/generator/index.js +174 -25
  51. package/lib/generator/wizard.js +8 -0
  52. package/lib/infrastructure/helpers.js +103 -20
  53. package/lib/infrastructure/index.js +88 -10
  54. package/lib/infrastructure/services.js +70 -15
  55. package/lib/schema/application-schema.json +24 -3
  56. package/lib/schema/external-system.schema.json +435 -413
  57. package/lib/utils/api.js +3 -3
  58. package/lib/utils/app-register-auth.js +25 -3
  59. package/lib/utils/cli-utils.js +20 -0
  60. package/lib/utils/compose-generator.js +76 -75
  61. package/lib/utils/compose-handlebars-helpers.js +43 -0
  62. package/lib/utils/compose-vector-helper.js +18 -0
  63. package/lib/utils/config-paths.js +127 -2
  64. package/lib/utils/credential-secrets-env.js +267 -0
  65. package/lib/utils/dev-cert-helper.js +122 -0
  66. package/lib/utils/device-code-helpers.js +224 -0
  67. package/lib/utils/device-code.js +37 -336
  68. package/lib/utils/docker-build.js +40 -8
  69. package/lib/utils/env-copy.js +83 -13
  70. package/lib/utils/env-map.js +35 -5
  71. package/lib/utils/env-template.js +6 -5
  72. package/lib/utils/error-formatters/http-status-errors.js +20 -1
  73. package/lib/utils/help-builder.js +15 -2
  74. package/lib/utils/infra-status.js +30 -1
  75. package/lib/utils/local-secrets.js +7 -52
  76. package/lib/utils/mutagen-install.js +195 -0
  77. package/lib/utils/mutagen.js +146 -0
  78. package/lib/utils/paths.js +43 -33
  79. package/lib/utils/port-resolver.js +28 -16
  80. package/lib/utils/remote-dev-auth.js +38 -0
  81. package/lib/utils/remote-docker-env.js +43 -0
  82. package/lib/utils/remote-secrets-loader.js +60 -0
  83. package/lib/utils/secrets-generator.js +94 -6
  84. package/lib/utils/secrets-helpers.js +33 -25
  85. package/lib/utils/secrets-path.js +2 -2
  86. package/lib/utils/secrets-utils.js +52 -1
  87. package/lib/utils/secrets-validation.js +84 -0
  88. package/lib/utils/ssh-key-helper.js +116 -0
  89. package/lib/utils/token-manager-messages.js +90 -0
  90. package/lib/utils/token-manager.js +5 -4
  91. package/lib/utils/variable-transformer.js +3 -3
  92. package/lib/validation/validator.js +65 -0
  93. package/package.json +2 -2
  94. package/scripts/install-local.js +34 -15
  95. package/templates/README.md +0 -1
  96. package/templates/applications/README.md.hbs +4 -4
  97. package/templates/applications/dataplane/application.yaml +5 -4
  98. package/templates/applications/dataplane/env.template +12 -7
  99. package/templates/applications/keycloak/env.template +2 -0
  100. package/templates/applications/miso-controller/application.yaml +1 -0
  101. package/templates/applications/miso-controller/env.template +11 -9
  102. package/templates/python/docker-compose.hbs +49 -23
  103. package/templates/typescript/docker-compose.hbs +48 -22
@@ -17,8 +17,6 @@ const { exec } = require('child_process');
17
17
  const { loadConfigFile } = require('../utils/config-format');
18
18
  const { promisify } = require('util');
19
19
  const validator = require('../validation/validator');
20
- const infra = require('../infrastructure');
21
- const secrets = require('../core/secrets');
22
20
  const config = require('../core/config');
23
21
  const buildCopy = require('../utils/build-copy');
24
22
  const logger = require('../utils/logger');
@@ -27,7 +25,10 @@ const composeGenerator = require('../utils/compose-generator');
27
25
  const dockerUtils = require('../utils/docker');
28
26
  const containerHelpers = require('../utils/app-run-containers');
29
27
  const pathsUtil = require('../utils/paths');
28
+ const runEnvCompose = require('./run-env-compose');
29
+ const { resolveEnvOutputPath, writeEnvOutputForReload, writeEnvOutputForLocal } = require('../utils/env-copy');
30
30
  const { resolveVersionForApp } = require('../utils/image-version');
31
+ const { parseImageOverride } = require('../utils/parse-image-ref');
31
32
 
32
33
  const execAsync = promisify(exec);
33
34
 
@@ -160,6 +161,28 @@ async function resolveAndUpdateVersion(appName, appConfig, debug) {
160
161
  }
161
162
  }
162
163
 
164
+ /**
165
+ * Resolve image name and tag from app config and optional run override.
166
+ * @param {string} appName - Application name
167
+ * @param {Object} appConfig - Application configuration
168
+ * @param {Object} runOptions - Run options; runOptions.image overrides config
169
+ * @returns {{ imageName: string, imageTag: string }} imageName and imageTag
170
+ */
171
+ function resolveRunImage(appName, appConfig, runOptions) {
172
+ const imageOverride = runOptions && runOptions.image;
173
+ if (imageOverride) {
174
+ const parsed = parseImageOverride(imageOverride);
175
+ return {
176
+ imageName: parsed ? parsed.name : composeGenerator.getImageName(appConfig, appName),
177
+ imageTag: parsed ? parsed.tag : (appConfig.image && appConfig.image.tag) || 'latest'
178
+ };
179
+ }
180
+ return {
181
+ imageName: composeGenerator.getImageName(appConfig, appName),
182
+ imageTag: (appConfig.image && appConfig.image.tag) || 'latest'
183
+ };
184
+ }
185
+
163
186
  /**
164
187
  * Checks prerequisites: Docker image and (optionally) infrastructure
165
188
  * @async
@@ -167,11 +190,11 @@ async function resolveAndUpdateVersion(appName, appConfig, debug) {
167
190
  * @param {Object} appConfig - Application configuration
168
191
  * @param {boolean} [debug=false] - Enable debug logging
169
192
  * @param {boolean} [skipInfraCheck=false] - When true, skip infra health check (e.g. when caller already verified, e.g. up-miso)
193
+ * @param {Object} [runOptions] - Run options; when runOptions.image is set, that image is checked instead of config-derived
170
194
  * @throws {Error} If prerequisites are not met
171
195
  */
172
- async function checkPrerequisites(appName, appConfig, debug = false, skipInfraCheck = false) {
173
- const imageName = composeGenerator.getImageName(appConfig, appName);
174
- const imageTag = appConfig.image?.tag || 'latest';
196
+ async function checkPrerequisites(appName, appConfig, debug = false, skipInfraCheck = false, runOptions = {}) {
197
+ const { imageName, imageTag } = resolveRunImage(appName, appConfig, runOptions);
175
198
  const fullImageName = `${imageName}:${imageTag}`;
176
199
 
177
200
  if (debug) {
@@ -181,7 +204,11 @@ async function checkPrerequisites(appName, appConfig, debug = false, skipInfraCh
181
204
  logger.log(chalk.blue(`Checking if image ${fullImageName} exists...`));
182
205
  const imageExists = await checkImageExists(imageName, imageTag, debug);
183
206
  if (!imageExists) {
184
- throw new Error(`Docker image ${fullImageName} not found\nRun 'aifabrix build ${appName}' first`);
207
+ const isTemplateApp = TEMPLATE_APP_KEYS.includes(appName);
208
+ const hint = isTemplateApp
209
+ ? `Pull the image (e.g. docker pull ${fullImageName}) or use --image ${appName}=<image> for up-miso/up-dataplane.`
210
+ : `Run 'aifabrix build ${appName}' first`;
211
+ throw new Error(`Docker image ${fullImageName} not found\n${hint}`);
185
212
  }
186
213
  logger.log(chalk.green(`✓ Image ${fullImageName} found`));
187
214
 
@@ -200,6 +227,7 @@ async function checkPrerequisites(appName, appConfig, debug = false, skipInfraCh
200
227
  */
201
228
  async function checkInfraHealthOrThrow(debug) {
202
229
  logger.log(chalk.blue('Checking infrastructure health...'));
230
+ const infra = require('../infrastructure');
203
231
  const infraHealth = await infra.checkInfraHealth();
204
232
  if (debug) {
205
233
  logger.log(chalk.gray(`[DEBUG] Infrastructure health: ${JSON.stringify(infraHealth, null, 2)}`));
@@ -230,73 +258,20 @@ async function ensureDevDirectory(appName, developerId) {
230
258
  }
231
259
 
232
260
  /**
233
- * Generate or update .env file for Docker
234
- * @async
235
- * @param {string} appName - Application name
236
- * @param {string} builderEnvPath - Path to builder .env file
237
- * @param {boolean} [skipOutputPath=false] - When true, skip copying to envOutputPath (e.g. up-miso/up-dataplane, no local code)
238
- */
239
- async function ensureEnvFile(appName, builderEnvPath, skipOutputPath = false) {
240
- if (!fsSync.existsSync(builderEnvPath)) {
241
- logger.log(chalk.yellow('Generating .env file from template...'));
242
- await secrets.generateEnvFile(appName, null, 'docker', false, skipOutputPath);
243
- } else {
244
- logger.log(chalk.blue('Updating .env file for Docker environment...'));
245
- await secrets.generateEnvFile(appName, null, 'docker', false, skipOutputPath);
246
- }
247
- }
248
-
249
- /**
250
- * Copy .env file to dev directory
251
- * @async
252
- * @param {string} builderEnvPath - Path to builder .env file
253
- * @param {string} devEnvPath - Path to dev .env file
254
- */
255
- async function copyEnvToDev(builderEnvPath, devEnvPath) {
256
- if (fsSync.existsSync(builderEnvPath)) {
257
- await fs.copyFile(builderEnvPath, devEnvPath);
258
- }
259
- }
260
-
261
- /**
262
- * Handle envOutputPath configuration
263
- * @async
264
- * @param {string} appName - Application name
265
- * @param {string} configPath - Path to application config file
266
- * @param {string} builderEnvPath - Path to builder .env file
267
- * @param {string} devEnvPath - Path to dev .env file
268
- * @param {boolean} [skipOutputPath=false] - When true, skip (e.g. up-miso/up-dataplane, no local code)
269
- */
270
- async function handleEnvOutputPath(appName, configPath, builderEnvPath, devEnvPath, skipOutputPath = false) {
271
- if (skipOutputPath) {
272
- return;
273
- }
274
- let variables;
275
- try {
276
- variables = loadConfigFile(configPath);
277
- } catch {
278
- return;
279
- }
280
-
281
- if (variables?.build?.envOutputPath && variables.build.envOutputPath !== null) {
282
- logger.log(chalk.blue('Ensuring .env file in apps/ directory is updated for Docker...'));
283
- await secrets.generateEnvFile(appName, null, 'docker');
284
- await copyEnvToDev(builderEnvPath, devEnvPath);
285
- }
286
- }
287
-
288
- /**
289
- * Calculate compose port from options or app config
290
- * @param {Object} options - Run options
261
+ * Calculate host port for docker-compose mapping (first port in "host:container").
262
+ * Uses application.yaml top-level port (not localPort). Second port is always containerPort from config.
263
+ * Example: keycloak port 8082, containerPort 8080 → "8082:8080"; miso-controller port 3000 → "3000:3000".
264
+ *
265
+ * @param {Object} options - Run options (may include port override)
291
266
  * @param {Object} appConfig - Application configuration
292
267
  * @param {string} developerId - Developer ID
293
- * @returns {number} Port number
268
+ * @returns {number} Host port number
294
269
  */
295
270
  function calculateComposePort(options, appConfig, developerId) {
296
271
  if (options.port) {
297
272
  return options.port;
298
273
  }
299
- const basePort = appConfig.port || 3000;
274
+ const basePort = appConfig.port ?? 3000;
300
275
  const idNum = typeof developerId === 'string' ? parseInt(developerId, 10) : developerId;
301
276
  return idNum === 0 ? basePort : basePort + (idNum * 100);
302
277
  }
@@ -313,84 +288,104 @@ function calculateComposePort(options, appConfig, developerId) {
313
288
  async function generateComposeFile(appName, appConfig, composeOptions, devDir) {
314
289
  logger.log(chalk.blue('Generating Docker Compose configuration...'));
315
290
  const composeContent = await composeGenerator.generateDockerCompose(appName, appConfig, composeOptions);
291
+ runEnvCompose.assertNoPasswordLiteralsInCompose(composeContent);
316
292
  const tempComposePath = path.join(devDir, 'docker-compose.yaml');
317
293
  await fs.writeFile(tempComposePath, composeContent);
318
294
  return tempComposePath;
319
295
  }
320
296
 
321
297
  /**
322
- * Prepares environment: ensures .env file and generates Docker Compose
298
+ * Writes .env to envOutputPath when application.yaml build.envOutputPath is set.
323
299
  * @async
324
300
  * @param {string} appName - Application name
325
301
  * @param {Object} appConfig - Application configuration
326
- * @param {Object} options - Run options
327
- * @returns {Promise<string>} Path to generated compose file
302
+ * @param {string} runEnvPath - Path to .env.run
303
+ * @param {Object} options - Run options (reload flag)
304
+ */
305
+ async function writeEnvOutputIfConfigured(appName, appConfig, runEnvPath, options) {
306
+ if (options && options.skipEnvOutputPath === true) return;
307
+ const envOutputPathRaw = appConfig.build?.envOutputPath;
308
+ if (!envOutputPathRaw || typeof envOutputPathRaw !== 'string' || envOutputPathRaw.trim() === '') {
309
+ return;
310
+ }
311
+ const configPath = path.join(pathsUtil.getBuilderPath(appName), 'application.yaml');
312
+ const outputPath = resolveEnvOutputPath(envOutputPathRaw.trim(), configPath);
313
+ const outputDir = path.dirname(outputPath);
314
+ if (!fsSync.existsSync(outputDir)) {
315
+ await fs.mkdir(outputDir, { recursive: true });
316
+ }
317
+ if (options.reload) {
318
+ await writeEnvOutputForReload(outputPath, runEnvPath);
319
+ } else {
320
+ await writeEnvOutputForLocal(appName, outputPath);
321
+ }
322
+ }
323
+
324
+ /**
325
+ * Prepares environment: clean applications dir, build two .env files (app-only + start-only), generate Docker Compose.
326
+ * .env.run = app container only (no admin secrets). .env.run.admin = db-init/start only (POSTGRES_PASSWORD etc.), deleted after run.
327
+ *
328
+ * @async
329
+ * @param {string} appName - Application name
330
+ * @param {Object} appConfig - Application configuration
331
+ * @param {Object} options - Run options (may include envFilePath, devMountPath from caller)
332
+ * @returns {Promise<{ composePath: string, runEnvPath: string, runEnvAdminPath: string }>} Paths to compose and both run .env files (delete after success)
328
333
  */
329
334
  async function prepareEnvironment(appName, appConfig, options) {
330
335
  const developerId = await config.getDeveloperId();
331
336
  const devDir = await ensureDevDirectory(appName, developerId);
332
- const skipEnvOutputPath = options.skipEnvOutputPath === true;
333
337
 
334
- // Generate/update .env file (respect AIFABRIX_BUILDER_DIR when set by up-miso/up-dataplane)
335
- const builderEnvPath = path.join(pathsUtil.getBuilderPath(appName), '.env');
336
- await ensureEnvFile(appName, builderEnvPath, skipEnvOutputPath);
338
+ runEnvCompose.cleanApplicationsDir(developerId);
339
+ logger.log(chalk.blue('Building merged .env (admin + app secrets)...'));
340
+ const { runEnvPath, runEnvAdminPath } = await runEnvCompose.buildMergedRunEnvAndWrite(appName, appConfig, devDir);
337
341
 
338
- // Copy .env to dev directory
339
- const devEnvPath = path.join(devDir, '.env');
340
- await copyEnvToDev(builderEnvPath, devEnvPath);
342
+ const composeOptions = {
343
+ ...options,
344
+ envFilePath: runEnvPath,
345
+ dbInitEnvFilePath: runEnvAdminPath
346
+ };
347
+ composeOptions.port = calculateComposePort(composeOptions, appConfig, developerId);
348
+ const composePath = await generateComposeFile(appName, appConfig, composeOptions, devDir);
341
349
 
342
- // Handle envOutputPath if configured (skipped when skipEnvOutputPath e.g. up-miso/up-dataplane)
343
- let configPath;
344
- try {
345
- configPath = pathsUtil.resolveApplicationConfigPath(devDir);
346
- } catch {
347
- configPath = null;
348
- }
349
- if (configPath) {
350
- await handleEnvOutputPath(appName, configPath, builderEnvPath, devEnvPath, skipEnvOutputPath);
351
- }
350
+ await writeEnvOutputIfConfigured(appName, appConfig, runEnvPath, options);
352
351
 
353
- // Generate Docker Compose
354
- const composeOptions = { ...options };
355
- composeOptions.port = calculateComposePort(composeOptions, appConfig, developerId);
356
- return await generateComposeFile(appName, appConfig, composeOptions, devDir);
352
+ return { composePath, runEnvPath, runEnvAdminPath };
357
353
  }
358
354
 
359
355
  /**
360
- * Prepare environment variables from admin secrets
356
+ * Prepare environment variables for docker compose (no secrets in host env; compose uses env_file).
361
357
  * @async
362
358
  * @param {boolean} debug - Enable debug logging
363
- * @returns {Promise<Object>} Environment variables object
359
+ * @returns {Promise<Object>} Environment variables object for child process
364
360
  */
365
361
  async function prepareContainerEnv(debug) {
366
- const adminSecretsPath = await infra.ensureAdminSecrets();
367
- if (debug) {
368
- logger.log(chalk.gray(`[DEBUG] Admin secrets path: ${adminSecretsPath}`));
369
- }
362
+ const env = { ...process.env };
370
363
 
371
- const adminSecretsContent = fsSync.readFileSync(adminSecretsPath, 'utf8');
372
- const postgresPasswordMatch = adminSecretsContent.match(/^POSTGRES_PASSWORD=(.+)$/m);
373
- const postgresPassword = postgresPasswordMatch ? postgresPasswordMatch[1] : '';
364
+ if (typeof process.getuid === 'function' && typeof process.getgid === 'function') {
365
+ env.AIFABRIX_UID = String(process.getuid());
366
+ env.AIFABRIX_GID = String(process.getgid());
367
+ } else {
368
+ env.AIFABRIX_UID = '1000';
369
+ env.AIFABRIX_GID = '1000';
370
+ }
374
371
 
375
- const env = {
376
- ...process.env,
377
- ADMIN_SECRETS_PATH: adminSecretsPath,
378
- POSTGRES_PASSWORD: postgresPassword
379
- };
372
+ const { getRemoteDockerEnv } = require('../utils/remote-docker-env');
373
+ const remoteDocker = await getRemoteDockerEnv();
374
+ Object.assign(env, remoteDocker);
380
375
 
381
376
  if (debug) {
382
- logger.log(chalk.gray(`[DEBUG] Environment variables: ADMIN_SECRETS_PATH=${adminSecretsPath}, POSTGRES_PASSWORD=${postgresPassword ? '***' : '(not set)'}`));
377
+ logger.log(chalk.gray('[DEBUG] Container env prepared (secrets via env_file)'));
383
378
  }
384
379
 
385
380
  return env;
386
381
  }
387
382
 
388
383
  /**
389
- * Execute docker-compose up command
384
+ * Execute docker-compose up command. Services get env from env_file in the compose (e.g. .env.run).
390
385
  * @async
391
386
  * @param {string} composeCmdBase - Base compose command
392
387
  * @param {string} composePath - Path to compose file
393
- * @param {Object} env - Environment variables
388
+ * @param {Object} env - Environment variables for the child process
394
389
  * @param {boolean} debug - Enable debug logging
395
390
  */
396
391
  async function executeComposeUp(composeCmdBase, composePath, env, debug) {
@@ -403,37 +398,44 @@ async function executeComposeUp(composeCmdBase, composePath, env, debug) {
403
398
  }
404
399
 
405
400
  /**
406
- * Starts the container and waits for health check
401
+ * Starts the container and waits for health check. Deletes run .env files after success (ISO 27K).
407
402
  * @async
408
403
  * @param {string} appName - Application name
409
404
  * @param {string} composePath - Path to Docker Compose file
410
405
  * @param {number} port - Application port
411
406
  * @param {Object} appConfig - Application configuration
412
- * @param {boolean} [debug=false] - Enable debug logging
407
+ * @param {Object} [opts] - Options
408
+ * @param {boolean} [opts.debug=false] - Enable debug logging
409
+ * @param {string|null} [opts.runEnvPath=null] - Path to .env.run (app-only) to delete after successful start
410
+ * @param {string|null} [opts.runEnvAdminPath=null] - Path to .env.run.admin (start-only) to delete after successful start
413
411
  * @throws {Error} If container fails to start or become healthy
414
412
  */
415
- async function startContainer(appName, composePath, port, appConfig = null, debug = false) {
413
+ async function startContainer(appName, composePath, port, appConfig = null, opts = {}) {
414
+ const { debug = false, runEnvPath = null, runEnvAdminPath = null } = opts;
416
415
  logger.log(chalk.blue(`Starting ${appName}...`));
417
416
 
418
- // Ensure Docker + Compose available and determine correct compose command
419
417
  const composeCmdBase = await dockerUtils.ensureDockerAndCompose().then(() => dockerUtils.getComposeCommand());
420
-
421
- // Prepare environment variables
422
418
  const env = await prepareContainerEnv(debug);
423
-
424
- // Execute compose up
425
419
  await executeComposeUp(composeCmdBase, composePath, env, debug);
426
420
 
427
- // Get container name and log status
428
421
  const idNum = typeof appConfig.developerId === 'string' ? parseInt(appConfig.developerId, 10) : appConfig.developerId;
429
422
  const containerName = idNum === 0 ? `aifabrix-${appName}` : `aifabrix-dev${appConfig.developerId}-${appName}`;
430
423
  logger.log(chalk.green(`✓ Container ${containerName} started`));
431
424
  await containerHelpers.logContainerStatus(containerName, debug);
432
425
 
433
- // Wait for health check
434
426
  const healthCheckPath = appConfig?.healthCheck?.path || '/health';
435
427
  logger.log(chalk.blue(`Waiting for application to be healthy at http://localhost:${port}${healthCheckPath}...`));
436
428
  await waitForHealthCheck(appName, 90, port, appConfig, debug);
429
+
430
+ for (const p of [runEnvPath, runEnvAdminPath]) {
431
+ if (p && typeof p === 'string') {
432
+ try {
433
+ await fs.unlink(p);
434
+ } catch (err) {
435
+ if (err.code !== 'ENOENT') logger.log(chalk.yellow(`Warning: could not remove run .env: ${err.message}`));
436
+ }
437
+ }
438
+ }
437
439
  }
438
440
 
439
441
  /**
@@ -461,6 +463,7 @@ module.exports = {
461
463
  checkPrerequisites,
462
464
  prepareEnvironment,
463
465
  startContainer,
464
- displayRunStatus
466
+ displayRunStatus,
467
+ cleanApplicationsDir: runEnvCompose.cleanApplicationsDir
465
468
  };
466
469
 
package/lib/app/run.js CHANGED
@@ -14,14 +14,50 @@ const { exec } = require('child_process');
14
14
  const { promisify } = require('util');
15
15
  const config = require('../core/config');
16
16
  const logger = require('../utils/logger');
17
+ const pathsUtil = require('../utils/paths');
17
18
  const { checkPortAvailable, waitForHealthCheck } = require('../utils/health-check');
18
19
  const composeGenerator = require('../utils/compose-generator');
19
20
  const containerHelpers = require('../utils/app-run-containers');
21
+ const mutagen = require('../utils/mutagen');
20
22
  // Helper functions extracted to reduce file size and complexity
21
23
  const helpers = require('./run-helpers');
22
24
 
23
25
  const execAsync = promisify(exec);
24
26
 
27
+ /**
28
+ * True if host is localhost or 127.0.0.1 (case-insensitive).
29
+ * @param {string} host - Hostname or empty
30
+ * @returns {boolean}
31
+ */
32
+ function isLocalhostHost(host) {
33
+ if (!host || typeof host !== 'string') return false;
34
+ const h = host.trim().toLowerCase();
35
+ return h === 'localhost' || h === '127.0.0.1';
36
+ }
37
+
38
+ /**
39
+ * True if the given URL or endpoint string refers to localhost (host is localhost or 127.0.0.1).
40
+ * Handles tcp://localhost:2376, https://127.0.0.1:8443, etc.
41
+ * @param {string} urlOrEndpoint - URL (e.g. https://localhost:8443) or Docker endpoint (e.g. tcp://localhost:2376)
42
+ * @returns {boolean}
43
+ */
44
+ function isLocalhostEndpoint(urlOrEndpoint) {
45
+ if (!urlOrEndpoint || typeof urlOrEndpoint !== 'string') return false;
46
+ const s = urlOrEndpoint.trim();
47
+ if (!s) return false;
48
+ try {
49
+ if (s.startsWith('tcp://') || s.startsWith('unix://')) {
50
+ const rest = s.replace(/^tcp:\/\//i, '').replace(/^unix:\/\//i, '');
51
+ const host = rest.split('/')[0].split(':')[0];
52
+ return isLocalhostHost(host);
53
+ }
54
+ const u = new URL(s);
55
+ return isLocalhostHost(u.hostname);
56
+ } catch {
57
+ return false;
58
+ }
59
+ }
60
+
25
61
  /**
26
62
  * Validate app for run and check if it's an external system
27
63
  * @async
@@ -106,6 +142,48 @@ async function calculateHostPort(appConfig, options, debug) {
106
142
  return hostPort;
107
143
  }
108
144
 
145
+ /**
146
+ * When run --reload in dev with remote: ensure Mutagen sync session; return remote path for compose mount.
147
+ * Uses codePath (resolved build.context) as Mutagen local path so one config field drives both local and remote.
148
+ * When Docker endpoint, remote-server, or sync-ssh-host is localhost/127.0.0.1, returns null (no sync; use local path).
149
+ *
150
+ * @param {string} appName - Application name
151
+ * @param {string} developerId - Developer ID
152
+ * @param {boolean} debug - Debug flag
153
+ * @param {string} codePath - Resolved build.context (absolute path to app code)
154
+ * @param {string} [remoteSyncPath] - Optional relative path under user-mutagen-folder (from build.remoteSyncPath); when unset, defaults to dev/<appKey>
155
+ * @returns {Promise<string|null>} Remote path for -v mount or null if not remote/reload or already on server (localhost)
156
+ * @throws {Error} If --reload but remote not configured, or Mutagen install fails
157
+ */
158
+ async function ensureReloadSync(appName, developerId, debug, codePath, remoteSyncPath) {
159
+ const endpoint = await config.getDockerEndpoint();
160
+ const serverUrl = await config.getRemoteServer();
161
+ if (!endpoint && !serverUrl) return null;
162
+ const syncSshHost = await config.getSyncSshHost();
163
+ if (isLocalhostEndpoint(endpoint) || isLocalhostEndpoint(serverUrl) || isLocalhostHost(syncSshHost || '')) {
164
+ if (debug) logger.log(chalk.gray('[DEBUG] Docker/remote/sync host is localhost; skipping Mutagen, using local path'));
165
+ return null;
166
+ }
167
+ const [userMutagenFolder, syncSshUser] = await Promise.all([
168
+ config.getUserMutagenFolder(),
169
+ config.getSyncSshUser()
170
+ ]);
171
+ if (!userMutagenFolder || !syncSshUser || !syncSshHost) {
172
+ throw new Error(
173
+ 'run --reload requires remote server sync settings. Run "aifabrix dev init" or set user-mutagen-folder, sync-ssh-user, sync-ssh-host in config.'
174
+ );
175
+ }
176
+ const mutagenPath = await mutagen.ensureMutagenPath(logger.log);
177
+ const remotePath = mutagen.getRemotePath(userMutagenFolder, appName, remoteSyncPath);
178
+ const sshUrl = mutagen.getSyncSshUrl(syncSshUser, syncSshHost, remotePath);
179
+ const sessionName = mutagen.getSessionName(developerId, appName);
180
+ const localPath = (codePath && typeof codePath === 'string') ? codePath : pathsUtil.getBuilderPath(appName);
181
+ if (debug) logger.log(chalk.gray(`[DEBUG] Mutagen sync: ${sessionName} ${localPath} <-> ${sshUrl}`));
182
+ logger.log(chalk.blue('Ensuring Mutagen sync session...'));
183
+ await mutagen.ensureSyncSession(mutagenPath, sessionName, localPath, sshUrl);
184
+ return remotePath;
185
+ }
186
+
109
187
  /**
110
188
  * Load and configure application
111
189
  * @async
@@ -132,16 +210,20 @@ async function loadAndConfigureApp(appName, debug) {
132
210
  * @param {string} tempComposePath - Path to compose file
133
211
  * @param {number} hostPort - Host port
134
212
  * @param {Object} appConfig - Application configuration
135
- * @param {boolean} debug - Debug flag
213
+ * @param {Object} opts - Options (debug, runEnvPath, runEnvAdminPath)
136
214
  * @throws {Error} If container start fails
137
215
  */
138
- async function startAppContainer(appName, tempComposePath, hostPort, appConfig, debug) {
216
+ async function startAppContainer(appName, tempComposePath, hostPort, appConfig, opts) {
217
+ const { debug, runEnvPath = null, runEnvAdminPath = null } = opts;
139
218
  try {
140
- await helpers.startContainer(appName, tempComposePath, hostPort, appConfig, debug);
219
+ await helpers.startContainer(appName, tempComposePath, hostPort, appConfig, { debug, runEnvPath, runEnvAdminPath });
141
220
  await helpers.displayRunStatus(appName, hostPort, appConfig);
142
221
  } catch (error) {
143
222
  logger.log(chalk.yellow(`\n⚠️ Compose file preserved at: ${tempComposePath}`));
144
223
  logger.log(chalk.yellow(' Review the file to debug issues'));
224
+ if (runEnvPath || runEnvAdminPath) {
225
+ logger.log(chalk.yellow(' Run .env file(s) (contain secrets) were not deleted; remove them manually if desired.'));
226
+ }
145
227
  if (debug) {
146
228
  logger.log(chalk.gray(`[DEBUG] Error during container start: ${error.message}`));
147
229
  }
@@ -149,6 +231,54 @@ async function startAppContainer(appName, tempComposePath, hostPort, appConfig,
149
231
  }
150
232
  }
151
233
 
234
+ /**
235
+ * Resolve run options (paths and optional reload sync) for prepareAppRun.
236
+ * @param {string} appName - Application name
237
+ * @param {Object} appConfig - Application configuration
238
+ * @param {Object} options - Run options
239
+ * @param {string} envKey - Environment key (dev/tst/pro)
240
+ * @param {boolean} debug - Debug flag
241
+ * @returns {Promise<Object>} Run options with optional devMountPath
242
+ */
243
+ async function resolveRunOptions(appName, appConfig, options, envKey, debug) {
244
+ const runOptions = { ...options, env: envKey };
245
+ const builderPath = pathsUtil.getBuilderPath(appName);
246
+ const codePath = pathsUtil.resolveBuildContext(builderPath, appConfig.build?.context || '.');
247
+ if (options.reload && envKey === 'dev') {
248
+ const remoteSyncPath = appConfig.build?.remoteSyncPath;
249
+ const remotePath = await ensureReloadSync(appName, appConfig.developerId, debug, codePath, remoteSyncPath);
250
+ runOptions.devMountPath = remotePath || codePath;
251
+ }
252
+ return runOptions;
253
+ }
254
+
255
+ /**
256
+ * Prepare run: validate app, load config, check prereqs, stop existing container, resolve port and reload, prepare env.
257
+ * @param {string} appName - Application name
258
+ * @param {Object} options - Run options
259
+ * @param {boolean} debug - Debug flag
260
+ * @returns {Promise<{ appConfig: Object, tempComposePath: string, hostPort: number }|null>} Prepared run context or null if should not continue
261
+ */
262
+ async function prepareAppRun(appName, options, debug) {
263
+ const envKey = (options.env || 'dev').toLowerCase();
264
+ if (envKey !== 'dev' && envKey !== 'tst' && envKey !== 'pro') {
265
+ throw new Error('--env must be dev, tst, or pro');
266
+ }
267
+ const shouldContinue = await validateAppForRun(appName, debug);
268
+ if (!shouldContinue) {
269
+ return null;
270
+ }
271
+ const appConfig = await loadAndConfigureApp(appName, debug);
272
+ await helpers.checkPrerequisites(appName, appConfig, debug, options.skipInfraCheck === true, options);
273
+ await checkAndStopContainer(appName, appConfig.developerId, debug);
274
+ const hostPort = await calculateHostPort(appConfig, options, debug);
275
+ const runOptions = await resolveRunOptions(appName, appConfig, options, envKey, debug);
276
+ const { composePath: tempComposePath, runEnvPath, runEnvAdminPath } = await helpers.prepareEnvironment(appName, appConfig, runOptions);
277
+ const result = { appConfig, tempComposePath, hostPort, runEnvPath, runEnvAdminPath };
278
+ if (runOptions.devMountPath) result.devMountPath = runOptions.devMountPath;
279
+ return result;
280
+ }
281
+
152
282
  /**
153
283
  * Runs the application locally using Docker
154
284
  * Starts container with proper port mapping and environment
@@ -168,40 +298,27 @@ async function startAppContainer(appName, tempComposePath, hostPort, appConfig,
168
298
  */
169
299
  async function runApp(appName, options = {}) {
170
300
  const debug = options.debug || false;
171
-
172
301
  if (debug) {
173
302
  logger.log(chalk.gray(`[DEBUG] Starting run process for: ${appName}`));
174
303
  logger.log(chalk.gray(`[DEBUG] Options: ${JSON.stringify(options, null, 2)}`));
175
304
  }
176
-
177
305
  try {
178
- // Validate app for run and check if external
179
- const shouldContinue = await validateAppForRun(appName, debug);
180
- if (!shouldContinue) {
306
+ const prepared = await prepareAppRun(appName, options, debug);
307
+ if (!prepared) {
181
308
  return;
182
309
  }
183
-
184
- // Load and configure application
185
- const appConfig = await loadAndConfigureApp(appName, debug);
186
-
187
- // Check prerequisites: image and (unless skipped) infrastructure
188
- await helpers.checkPrerequisites(appName, appConfig, debug, options.skipInfraCheck === true);
189
-
190
- // Check if container is already running and stop it if needed
191
- await checkAndStopContainer(appName, appConfig.developerId, debug);
192
-
193
- // Calculate host port and validate it's available
194
- const hostPort = await calculateHostPort(appConfig, options, debug);
195
-
196
- // Prepare environment: ensure .env file and generate Docker Compose
197
- const tempComposePath = await helpers.prepareEnvironment(appName, appConfig, options);
198
310
  if (debug) {
199
- logger.log(chalk.gray(`[DEBUG] Compose file generated: ${tempComposePath}`));
311
+ logger.log(chalk.gray(`[DEBUG] Compose file generated: ${prepared.tempComposePath}`));
200
312
  }
201
-
202
- // Start container and display status
203
- await startAppContainer(appName, tempComposePath, hostPort, appConfig, debug);
204
-
313
+ if (options.reload && prepared.devMountPath) {
314
+ logger.log(chalk.gray('With --reload: workspace mounted from host at /app (container runs as your user for write access).'));
315
+ logger.log(chalk.gray(` Host path: ${prepared.devMountPath}`));
316
+ }
317
+ await startAppContainer(appName, prepared.tempComposePath, prepared.hostPort, prepared.appConfig, {
318
+ debug,
319
+ runEnvPath: prepared.runEnvPath,
320
+ runEnvAdminPath: prepared.runEnvAdminPath
321
+ });
205
322
  } catch (error) {
206
323
  if (debug) {
207
324
  logger.log(chalk.gray(`[DEBUG] Run failed: ${error.message}`));
@@ -243,6 +360,9 @@ async function restartApp(appName) {
243
360
  module.exports = {
244
361
  runApp,
245
362
  restartApp,
363
+ ensureReloadSync,
364
+ isLocalhostHost,
365
+ isLocalhostEndpoint,
246
366
  checkImageExists: helpers.checkImageExists,
247
367
  checkContainerRunning: helpers.checkContainerRunning,
248
368
  stopAndRemoveContainer: helpers.stopAndRemoveContainer,
package/lib/app/show.js CHANGED
@@ -171,7 +171,9 @@ function healthStrFromVariables(variables) {
171
171
  function buildStrFromVariables(variables) {
172
172
  const build = variables.build;
173
173
  if (!build) return '—';
174
- return `${build.language || '—'}, localPort ${build.localPort || '—'}`;
174
+ const port = variables.port ?? build.containerPort;
175
+ const portStr = (port !== undefined && port !== null) ? `port ${port}` : '';
176
+ return [build.language || '—', portStr].filter(Boolean).join(', ') || '—';
175
177
  }
176
178
 
177
179
  function buildApplicationFromVariables(variables) {
@@ -438,7 +440,8 @@ function formatBuildForDisplay(build) {
438
440
  if (!build) return '—';
439
441
  const parts = [];
440
442
  if (build.language) parts.push(build.language);
441
- if (build.localPort !== undefined && build.localPort !== null) parts.push(`localPort ${build.localPort}`);
443
+ const port = build.port ?? build.localPort;
444
+ if (port !== undefined && port !== null) parts.push(`port ${port}`);
442
445
  if (build.dockerfile) parts.push('dockerfile');
443
446
  if (build.envOutputPath) parts.push(`envOutputPath: ${build.envOutputPath}`);
444
447
  return parts.length ? parts.join(', ') : '—';
@@ -26,6 +26,7 @@ const dockerBuild = require('../utils/docker-build');
26
26
  const buildCopy = require('../utils/build-copy');
27
27
  const { buildDevImageName } = require('../utils/image-name');
28
28
  const buildHelpers = require('../utils/build-helpers');
29
+ const secretsEnvWrite = require('../core/secrets-env-write');
29
30
 
30
31
  /**
31
32
  * Loads application config for an application
@@ -410,11 +411,18 @@ async function buildApp(appName, options = {}) {
410
411
  appConfig
411
412
  }, options, buildHelpers);
412
413
 
413
- // 5. Execute Docker build
414
+ // 5. Resolve NPM_TOKEN/PYPI_TOKEN for private registries and pass as build-args
415
+ const envMap = await secretsEnvWrite.resolveAndGetEnvMap(appName, { environment: 'docker' });
416
+ const buildArgs = {};
417
+ if (envMap.NPM_TOKEN) buildArgs.NPM_TOKEN = envMap.NPM_TOKEN;
418
+ if (envMap.PYPI_TOKEN) buildArgs.PYPI_TOKEN = envMap.PYPI_TOKEN;
419
+ const buildOptions = { ...options, buildArgs };
420
+
421
+ // 6. Execute Docker build
414
422
  const tag = options.tag || 'latest';
415
- await dockerBuild.executeDockerBuildWithTag(effectiveImageName, imageName, dockerfilePath, contextPath, tag, options);
423
+ await dockerBuild.executeDockerBuildWithTag(effectiveImageName, imageName, dockerfilePath, contextPath, tag, buildOptions);
416
424
 
417
- // 6. Post-build tasks
425
+ // 7. Post-build tasks
418
426
  await postBuildTasks(appName, buildConfig);
419
427
 
420
428
  logger.log(chalk.green('\n✅ Build completed successfully!'));