Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/adapters/base-class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,36 @@ describe('BaseClass', () => {
expect(exitMock).toHaveBeenCalledWith(1);
});
});

describe('handleEnvVariables', () => {
beforeEach(() => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {},
} as any);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should parse environment variables from config string with various value formats including URLs', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
envVariables: 'APP_ENV:prod, API_URL:https://api.example.com/v1, DB_URL:postgresql://localhost:5432/dbname',
},
} as any);

await baseClass.promptForEnvValues();

expect(baseClass.envVariables).toEqual([
{ key: 'APP_ENV', value: 'prod' },
{ key: 'API_URL', value: 'https://api.example.com/v1' },
{ key: 'DB_URL', value: 'postgresql://localhost:5432/dbname' },
]);
});
});
});
36 changes: 20 additions & 16 deletions src/adapters/base-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,24 @@ export default class BaseClass {
this.config.stackEnvironment = this.config.deliveryToken?.scope[0]?.environments[0]?.name;
}

/**
* @method parseEnvVariablesString - Parse environment variables string into key-value pairs
* Splits on first colon only to support values containing colons (e.g., URLs)
*
* @param {string} envString - Comma-separated string of key:value pairs
* @return {*} {Array<{key: string, value: string}>}
* @memberof BaseClass
*/
private parseEnvVariablesString(envString: string): Array<{ key: string; value: string }> {
return map(split(envString, ','), (pair) => {
const trimmedPair = (pair as string).trim();
const colonIndex = trimmedPair.indexOf(':');
const key = colonIndex !== -1 ? trimmedPair.substring(0, colonIndex).trim() : trimmedPair.trim();
const value = colonIndex !== -1 ? trimmedPair.substring(colonIndex + 1).trim() : '';
return { key, value };
}).filter(({ key }) => key);
}

/**
* @method promptForEnvValues - Prompt and get manual entry of environment variables
*
Expand All @@ -330,15 +348,7 @@ export default class BaseClass {
message:
'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',
})
.then((variable) => {
return map(split(variable as string, ','), (variable) => {
let [key, value] = split(variable as string, ':');
value = (value || '').trim();
key = (key || '').trim();

return { key, value };
}).filter(({ key }) => key);
});
.then((variable) => this.parseEnvVariablesString(variable as string));

envVariables.push(...variable);

Expand All @@ -356,13 +366,7 @@ export default class BaseClass {
this.envVariables.push(...envVariables);
} else {
if (typeof this.config.envVariables === 'string') {
const variable = map(split(this.config.envVariables as string, ','), (variable) => {
let [key, value] = split(variable as string, ':');
value = (value || '').trim();
key = (key || '').trim();

return { key, value };
});
const variable = this.parseEnvVariablesString(this.config.envVariables);
this.envVariables.push(...variable);
}
}
Expand Down
Loading