Skip to content

Commit b474704

Browse files
Merge pull request #71 from contentstack/cl-2121
fix: environment variable parsing for URL formatted values
2 parents 41e3935 + 6c924f0 commit b474704

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

src/adapters/base-class.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,4 +391,36 @@ describe('BaseClass', () => {
391391
expect(exitMock).toHaveBeenCalledWith(1);
392392
});
393393
});
394+
395+
describe('handleEnvVariables', () => {
396+
beforeEach(() => {
397+
baseClass = new BaseClass({
398+
log: logMock,
399+
exit: exitMock,
400+
config: {},
401+
} as any);
402+
});
403+
404+
afterEach(() => {
405+
jest.clearAllMocks();
406+
});
407+
408+
it('should parse environment variables from config string with various value formats including URLs', async () => {
409+
baseClass = new BaseClass({
410+
log: logMock,
411+
exit: exitMock,
412+
config: {
413+
envVariables: 'APP_ENV:prod, API_URL:https://api.example.com/v1, DB_URL:postgresql://localhost:5432/dbname',
414+
},
415+
} as any);
416+
417+
await baseClass.promptForEnvValues();
418+
419+
expect(baseClass.envVariables).toEqual([
420+
{ key: 'APP_ENV', value: 'prod' },
421+
{ key: 'API_URL', value: 'https://api.example.com/v1' },
422+
{ key: 'DB_URL', value: 'postgresql://localhost:5432/dbname' },
423+
]);
424+
});
425+
});
394426
});

src/adapters/base-class.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,24 @@ export default class BaseClass {
311311
this.config.stackEnvironment = this.config.deliveryToken?.scope[0]?.environments[0]?.name;
312312
}
313313

314+
/**
315+
* @method parseEnvVariablesString - Parse environment variables string into key-value pairs
316+
* Splits on first colon only to support values containing colons (e.g., URLs)
317+
*
318+
* @param {string} envString - Comma-separated string of key:value pairs
319+
* @return {*} {Array<{key: string, value: string}>}
320+
* @memberof BaseClass
321+
*/
322+
private parseEnvVariablesString(envString: string): Array<{ key: string; value: string }> {
323+
return map(split(envString, ','), (pair) => {
324+
const trimmedPair = (pair as string).trim();
325+
const colonIndex = trimmedPair.indexOf(':');
326+
const key = colonIndex !== -1 ? trimmedPair.substring(0, colonIndex).trim() : trimmedPair.trim();
327+
const value = colonIndex !== -1 ? trimmedPair.substring(colonIndex + 1).trim() : '';
328+
return { key, value };
329+
}).filter(({ key }) => key);
330+
}
331+
314332
/**
315333
* @method promptForEnvValues - Prompt and get manual entry of environment variables
316334
*
@@ -330,15 +348,7 @@ export default class BaseClass {
330348
message:
331349
'Enter key and value with a colon between them, and use a comma(,) for the key-value pair. Format: <key1>:<value1>, <key2>:<value2> Ex: APP_ENV:prod, TEST_ENV:testVal',
332350
})
333-
.then((variable) => {
334-
return map(split(variable as string, ','), (variable) => {
335-
let [key, value] = split(variable as string, ':');
336-
value = (value || '').trim();
337-
key = (key || '').trim();
338-
339-
return { key, value };
340-
}).filter(({ key }) => key);
341-
});
351+
.then((variable) => this.parseEnvVariablesString(variable as string));
342352

343353
envVariables.push(...variable);
344354

@@ -356,13 +366,7 @@ export default class BaseClass {
356366
this.envVariables.push(...envVariables);
357367
} else {
358368
if (typeof this.config.envVariables === 'string') {
359-
const variable = map(split(this.config.envVariables as string, ','), (variable) => {
360-
let [key, value] = split(variable as string, ':');
361-
value = (value || '').trim();
362-
key = (key || '').trim();
363-
364-
return { key, value };
365-
});
369+
const variable = this.parseEnvVariablesString(this.config.envVariables);
366370
this.envVariables.push(...variable);
367371
}
368372
}

0 commit comments

Comments
 (0)