genbox 1.0.28 → 1.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/init.js +71 -75
- package/package.json +1 -1
package/dist/commands/init.js
CHANGED
|
@@ -153,44 +153,65 @@ function getServiceNameFromUrl(baseUrl) {
|
|
|
153
153
|
return { name: 'unknown', varPrefix: 'UNKNOWN', description: 'Unknown Service' };
|
|
154
154
|
}
|
|
155
155
|
/**
|
|
156
|
-
*
|
|
156
|
+
* Create service URL mappings based on configured environments
|
|
157
|
+
* Uses URLs from configured environments instead of prompting again
|
|
157
158
|
*/
|
|
158
|
-
async function
|
|
159
|
+
async function createServiceUrlMappings(serviceUrls, configuredEnvs) {
|
|
159
160
|
const mappings = [];
|
|
160
161
|
if (serviceUrls.size === 0) {
|
|
161
162
|
return mappings;
|
|
162
163
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
164
|
+
// Determine which environment to use (prefer staging, then production)
|
|
165
|
+
const envNames = Object.keys(configuredEnvs || {});
|
|
166
|
+
const primaryEnv = envNames.includes('staging') ? 'staging' :
|
|
167
|
+
envNames.includes('production') ? 'production' :
|
|
168
|
+
envNames[0];
|
|
169
|
+
if (!primaryEnv || !configuredEnvs?.[primaryEnv]) {
|
|
170
|
+
// No environments configured - skip service URL mapping
|
|
171
|
+
return mappings;
|
|
172
|
+
}
|
|
173
|
+
const envConfig = configuredEnvs[primaryEnv];
|
|
174
|
+
const apiUrl = envConfig.urls?.api;
|
|
175
|
+
if (!apiUrl) {
|
|
176
|
+
// No API URL configured - skip
|
|
177
|
+
return mappings;
|
|
178
|
+
}
|
|
168
179
|
// Sort by port number for consistent ordering
|
|
169
180
|
const sortedServices = Array.from(serviceUrls.entries()).sort((a, b) => {
|
|
170
181
|
const portA = parseInt(a[0].match(/:(\d+)/)?.[1] || '0');
|
|
171
182
|
const portB = parseInt(b[0].match(/:(\d+)/)?.[1] || '0');
|
|
172
183
|
return portA - portB;
|
|
173
184
|
});
|
|
174
|
-
|
|
185
|
+
console.log('');
|
|
186
|
+
console.log(chalk_1.default.blue('=== Service URL Mapping ==='));
|
|
187
|
+
console.log(chalk_1.default.dim(`Mapping localhost URLs to ${primaryEnv} environment`));
|
|
188
|
+
console.log('');
|
|
189
|
+
for (const [baseUrl, { vars }] of sortedServices) {
|
|
175
190
|
const serviceInfo = getServiceNameFromUrl(baseUrl);
|
|
176
|
-
//
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
191
|
+
// Auto-map API/gateway URLs to the configured API URL
|
|
192
|
+
const isApiService = serviceInfo.name === 'gateway' ||
|
|
193
|
+
baseUrl.includes(':3050') ||
|
|
194
|
+
baseUrl.includes(':3105') ||
|
|
195
|
+
vars.some(v => v.toLowerCase().includes('api'));
|
|
196
|
+
if (isApiService && apiUrl) {
|
|
197
|
+
console.log(chalk_1.default.green(`✓ ${serviceInfo.description}: ${baseUrl} → ${apiUrl}`));
|
|
198
|
+
mappings.push({
|
|
199
|
+
varName: `${serviceInfo.varPrefix}_URL`,
|
|
200
|
+
localUrl: baseUrl,
|
|
201
|
+
remoteUrl: apiUrl,
|
|
202
|
+
remoteEnv: primaryEnv,
|
|
203
|
+
description: serviceInfo.description,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
// For non-API services, just record the local URL without remote
|
|
208
|
+
console.log(chalk_1.default.dim(` ${serviceInfo.description}: ${baseUrl} (local only)`));
|
|
209
|
+
mappings.push({
|
|
210
|
+
varName: `${serviceInfo.varPrefix}_URL`,
|
|
211
|
+
localUrl: baseUrl,
|
|
212
|
+
description: serviceInfo.description,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
194
215
|
}
|
|
195
216
|
return mappings;
|
|
196
217
|
}
|
|
@@ -202,19 +223,19 @@ function transformEnvWithVariables(envContent, mappings, frontendApps) {
|
|
|
202
223
|
return envContent;
|
|
203
224
|
}
|
|
204
225
|
let result = envContent;
|
|
226
|
+
// Determine the environment name for comments
|
|
227
|
+
const envName = mappings.find(m => m.remoteEnv)?.remoteEnv?.toUpperCase() || 'REMOTE';
|
|
205
228
|
// Build GLOBAL section additions
|
|
206
229
|
const globalAdditions = [
|
|
207
230
|
'',
|
|
208
231
|
'# Service URL Configuration',
|
|
209
|
-
|
|
232
|
+
`# These expand based on profile: \${GATEWAY_URL} → LOCAL or ${envName} value`,
|
|
210
233
|
];
|
|
211
234
|
for (const mapping of mappings) {
|
|
212
235
|
globalAdditions.push(`LOCAL_${mapping.varName}=${mapping.localUrl}`);
|
|
213
|
-
if (mapping.
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
else {
|
|
217
|
-
globalAdditions.push(`# STAGING_${mapping.varName}=https://your-staging-url.com`);
|
|
236
|
+
if (mapping.remoteUrl && mapping.remoteEnv) {
|
|
237
|
+
const prefix = mapping.remoteEnv.toUpperCase();
|
|
238
|
+
globalAdditions.push(`${prefix}_${mapping.varName}=${mapping.remoteUrl}`);
|
|
218
239
|
}
|
|
219
240
|
}
|
|
220
241
|
globalAdditions.push('');
|
|
@@ -597,6 +618,14 @@ exports.initCommand = new commander_1.Command('init')
|
|
|
597
618
|
const v4Config = convertV2ToV4(generated.config, scan);
|
|
598
619
|
// Update project name
|
|
599
620
|
v4Config.project.name = projectName;
|
|
621
|
+
// Environment configuration - do this BEFORE profiles so profiles can reference environments
|
|
622
|
+
// (skip only in non-interactive mode, always show for --from-scan since environments are required)
|
|
623
|
+
if (!nonInteractive) {
|
|
624
|
+
const envConfig = await setupEnvironments(scan, v4Config, isMultiRepoStructure, existingEnvValues);
|
|
625
|
+
if (envConfig) {
|
|
626
|
+
v4Config.environments = envConfig;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
600
629
|
// Ask about profiles (skip prompt when using --from-scan)
|
|
601
630
|
let createProfiles = true;
|
|
602
631
|
if (!nonInteractive && !options.fromScan) {
|
|
@@ -819,14 +848,6 @@ exports.initCommand = new commander_1.Command('init')
|
|
|
819
848
|
};
|
|
820
849
|
}
|
|
821
850
|
}
|
|
822
|
-
// Environment configuration (skip only in non-interactive mode)
|
|
823
|
-
// For --from-scan, we still want to prompt for environments since they're required for genbox to work
|
|
824
|
-
if (!nonInteractive) {
|
|
825
|
-
const envConfig = await setupEnvironments(scan, v4Config, isMultiRepo, existingEnvValues);
|
|
826
|
-
if (envConfig) {
|
|
827
|
-
v4Config.environments = envConfig;
|
|
828
|
-
}
|
|
829
|
-
}
|
|
830
851
|
// Script selection - always show multi-select UI (skip in non-interactive mode and --from-scan)
|
|
831
852
|
if (!nonInteractive && !options.fromScan) {
|
|
832
853
|
// Scan for scripts
|
|
@@ -914,32 +935,6 @@ exports.initCommand = new commander_1.Command('init')
|
|
|
914
935
|
console.log(chalk_1.default.dim(` - ${warning}`));
|
|
915
936
|
}
|
|
916
937
|
}
|
|
917
|
-
// Show API URL guidance if environments are configured
|
|
918
|
-
if (v4Config.environments && Object.keys(v4Config.environments).length > 0) {
|
|
919
|
-
console.log('');
|
|
920
|
-
console.log(chalk_1.default.blue('=== API URL Configuration ==='));
|
|
921
|
-
console.log(chalk_1.default.dim('The following API URLs were added to .env.genbox:'));
|
|
922
|
-
console.log('');
|
|
923
|
-
console.log(chalk_1.default.dim(' LOCAL_API_URL=http://localhost:3050'));
|
|
924
|
-
for (const [envName, envConfig] of Object.entries(v4Config.environments)) {
|
|
925
|
-
// v4 format: urls.api contains the API URL
|
|
926
|
-
const apiUrl = envConfig.urls?.api || envConfig.urls?.gateway;
|
|
927
|
-
if (apiUrl) {
|
|
928
|
-
const varName = `${envName.toUpperCase()}_API_URL`;
|
|
929
|
-
console.log(chalk_1.default.dim(` ${varName}=${apiUrl}`));
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
console.log('');
|
|
933
|
-
console.log(chalk_1.default.yellow('To use dynamic API URLs:'));
|
|
934
|
-
console.log(chalk_1.default.dim(' Use ${API_URL} in your app env vars, e.g.:'));
|
|
935
|
-
console.log(chalk_1.default.cyan(' VITE_API_BASE_URL=${API_URL}'));
|
|
936
|
-
console.log(chalk_1.default.cyan(' NEXT_PUBLIC_API_URL=${API_URL}'));
|
|
937
|
-
console.log('');
|
|
938
|
-
console.log(chalk_1.default.dim(' At create time, ${API_URL} expands based on profile:'));
|
|
939
|
-
console.log(chalk_1.default.dim(' • default_connection: staging → uses STAGING_API_URL'));
|
|
940
|
-
console.log(chalk_1.default.dim(' • default_connection: production → uses PRODUCTION_API_URL'));
|
|
941
|
-
console.log(chalk_1.default.dim(' • local/no default_connection → uses LOCAL_API_URL'));
|
|
942
|
-
}
|
|
943
938
|
// CORS Configuration Warning
|
|
944
939
|
const hasBackendApps = scan.apps.some(a => a.type === 'backend' || a.type === 'api');
|
|
945
940
|
const hasFrontendApps = scan.apps.some(a => a.type === 'frontend');
|
|
@@ -1495,17 +1490,18 @@ async function setupEnvFile(projectName, config, nonInteractive = false, scan, i
|
|
|
1495
1490
|
// Fall back to extracting from collected env content
|
|
1496
1491
|
serviceUrls = extractFrontendHttpUrls(segregatedContent, frontendApps);
|
|
1497
1492
|
}
|
|
1498
|
-
if (serviceUrls.size > 0) {
|
|
1499
|
-
//
|
|
1500
|
-
const
|
|
1501
|
-
(config.environments?.staging?.urls?.api);
|
|
1502
|
-
// Prompt for staging equivalents
|
|
1503
|
-
const urlMappings = await promptForStagingUrls(serviceUrls, existingStagingApiUrl);
|
|
1493
|
+
if (serviceUrls.size > 0 && config.environments) {
|
|
1494
|
+
// Create mappings based on configured environments (no prompting needed)
|
|
1495
|
+
const urlMappings = await createServiceUrlMappings(serviceUrls, config.environments);
|
|
1504
1496
|
// Transform content with expandable variables
|
|
1505
1497
|
if (urlMappings.length > 0) {
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1498
|
+
const mappedCount = urlMappings.filter(m => m.remoteUrl).length;
|
|
1499
|
+
if (mappedCount > 0) {
|
|
1500
|
+
segregatedContent = transformEnvWithVariables(segregatedContent, urlMappings, frontendApps);
|
|
1501
|
+
const envName = urlMappings.find(m => m.remoteEnv)?.remoteEnv || 'remote';
|
|
1502
|
+
console.log('');
|
|
1503
|
+
console.log(chalk_1.default.green(`✓ Configured ${mappedCount} service URL(s) for ${envName} environment`));
|
|
1504
|
+
}
|
|
1509
1505
|
}
|
|
1510
1506
|
}
|
|
1511
1507
|
}
|