Skip to content

Commit a5c2b95

Browse files
committed
fix: allow --variable-type flag to support multiple selections
1 parent 41e3935 commit a5c2b95

File tree

2 files changed

+169
-14
lines changed

2 files changed

+169
-14
lines changed

src/adapters/base-class.test.ts

Lines changed: 165 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ describe('BaseClass', () => {
5151
expect(exitMock).not.toHaveBeenCalled();
5252
});
5353

54-
it('should handle string variableType by converting to array - Manually add custom variables to the list', async () => {
54+
it(
55+
'should handle string variableType by converting to array - Manually add custom variables to the list',
56+
async () => {
5557
baseClass = new BaseClass({
5658
log: logMock,
5759
exit: exitMock,
@@ -69,7 +71,9 @@ describe('BaseClass', () => {
6971
expect(exitMock).not.toHaveBeenCalled();
7072
});
7173

72-
it('should handle string variableType by converting to array - Import variables from the .env.local file', async () => {
74+
it(
75+
'should handle string variableType by converting to array - Import variables from the .env.local file',
76+
async () => {
7377
baseClass = new BaseClass({
7478
log: logMock,
7579
exit: exitMock,
@@ -120,11 +124,139 @@ describe('BaseClass', () => {
120124

121125
expect(exitMock).not.toHaveBeenCalled();
122126
expect(logMock).not.toHaveBeenCalledWith(
123-
"The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options.",
127+
"The 'Skip adding environment variables' option cannot be combined with other " +
128+
"environment variable options. Please choose either 'Skip adding environment variables' " +
129+
"or one or more of the other available options.",
124130
'error',
125131
);
126132
});
127133

134+
it(
135+
'should handle two options Import variables from a stack and Manually add custom variables to the list',
136+
async () => {
137+
baseClass = new BaseClass({
138+
log: logMock,
139+
exit: exitMock,
140+
config: {
141+
variableType: ['Import variables from a stack', 'Manually add custom variables to the list'],
142+
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
143+
},
144+
} as any);
145+
146+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
147+
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
148+
149+
await baseClass.handleEnvImportFlow();
150+
151+
expect(importEnvFromStackMock).toHaveBeenCalled();
152+
expect(promptForEnvValuesMock).toHaveBeenCalled();
153+
expect(exitMock).not.toHaveBeenCalled();
154+
});
155+
156+
it(
157+
'should handle two options Import variables from a stack and Import variables from the .env.local file',
158+
async () => {
159+
baseClass = new BaseClass({
160+
log: logMock,
161+
exit: exitMock,
162+
config: {
163+
variableType: ['Import variables from a stack', 'Import variables from the .env.local file'],
164+
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
165+
},
166+
} as any);
167+
168+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
169+
const importVariableFromLocalConfigMock = jest
170+
.spyOn(baseClass, 'importVariableFromLocalConfig')
171+
.mockResolvedValueOnce();
172+
173+
await baseClass.handleEnvImportFlow();
174+
175+
expect(importEnvFromStackMock).toHaveBeenCalled();
176+
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
177+
expect(exitMock).not.toHaveBeenCalled();
178+
});
179+
180+
it(
181+
'should handle two options Manually add custom variables to the list and ' +
182+
'Import variables from the .env.local file',
183+
async () => {
184+
baseClass = new BaseClass({
185+
log: logMock,
186+
exit: exitMock,
187+
config: {
188+
variableType: ['Manually add custom variables to the list', 'Import variables from the .env.local file'],
189+
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
190+
},
191+
} as any);
192+
193+
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
194+
const importVariableFromLocalConfigMock = jest
195+
.spyOn(baseClass, 'importVariableFromLocalConfig')
196+
.mockResolvedValueOnce();
197+
198+
await baseClass.handleEnvImportFlow();
199+
200+
expect(promptForEnvValuesMock).toHaveBeenCalled();
201+
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
202+
expect(exitMock).not.toHaveBeenCalled();
203+
});
204+
205+
it(
206+
'should handle three options Import variables from a stack, Manually add custom variables to the list ' +
207+
'and Import variables from the .env.local file',
208+
async () => {
209+
baseClass = new BaseClass({
210+
log: logMock,
211+
exit: exitMock,
212+
config: {
213+
variableType: [
214+
'Import variables from a stack',
215+
'Manually add custom variables to the list',
216+
'Import variables from the .env.local file',
217+
],
218+
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
219+
},
220+
} as any);
221+
222+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
223+
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
224+
const importVariableFromLocalConfigMock = jest
225+
.spyOn(baseClass, 'importVariableFromLocalConfig')
226+
.mockResolvedValueOnce();
227+
228+
await baseClass.handleEnvImportFlow();
229+
230+
expect(importEnvFromStackMock).toHaveBeenCalled();
231+
expect(promptForEnvValuesMock).toHaveBeenCalled();
232+
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
233+
expect(exitMock).not.toHaveBeenCalled();
234+
});
235+
236+
it('should fail when Skip adding environment variables is combined with any other option', async () => {
237+
baseClass = new BaseClass({
238+
log: logMock,
239+
exit: exitMock,
240+
config: {
241+
variableType: ['Skip adding environment variables', 'Import variables from a stack'],
242+
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
243+
},
244+
} as any);
245+
246+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce(undefined);
247+
248+
await baseClass.handleEnvImportFlow();
249+
250+
expect(logMock).toHaveBeenCalledWith(
251+
"The 'Skip adding environment variables' option cannot be combined with other " +
252+
"environment variable options. Please choose either 'Skip adding environment variables' " +
253+
"or one or more of the other available options.",
254+
'error',
255+
);
256+
expect(exitMock).toHaveBeenCalledWith(1);
257+
expect(importEnvFromStackMock).toHaveBeenCalled();
258+
});
259+
128260
it('should exit if no options are selected', async () => {
129261
(ux.inquire as jest.Mock).mockResolvedValueOnce([]);
130262

@@ -147,7 +279,9 @@ describe('BaseClass', () => {
147279
await baseClass.handleEnvImportFlow();
148280

149281
expect(logMock).toHaveBeenCalledWith(
150-
"The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options.",
282+
"The 'Skip adding environment variables' option cannot be combined with other " +
283+
"environment variable options. Please choose either 'Skip adding environment variables' " +
284+
"or one or more of the other available options.",
151285
'error',
152286
);
153287

@@ -175,7 +309,9 @@ describe('BaseClass', () => {
175309
expect(promptForEnvValuesMock).toHaveBeenCalled();
176310
});
177311

178-
it('should call importVariableFromLocalConfig if "Import variables from the .env.local file" is selected', async () => {
312+
it(
313+
'should call importVariableFromLocalConfig if "Import variables from the .env.local file" is selected',
314+
async () => {
179315
const importVariableFromLocalConfigMock = jest
180316
.spyOn(baseClass, 'importVariableFromLocalConfig')
181317
.mockResolvedValueOnce();
@@ -196,11 +332,17 @@ describe('BaseClass', () => {
196332
expect(logMock).toHaveBeenCalledWith('Skipped adding environment variables.', 'info');
197333
});
198334

199-
it('should call importEnvFromStack and promptForEnvValues if "Import variables from a stack" and "Manually add custom variables to the list" both are selected.', async () => {
200-
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
201-
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
335+
it(
336+
'should call importEnvFromStack and promptForEnvValues if "Import variables from a stack" and ' +
337+
'"Manually add custom variables to the list" both are selected.',
338+
async () => {
339+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
340+
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
202341

203-
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from a stack', 'Manually add custom variables to the list']);
342+
(ux.inquire as jest.Mock).mockResolvedValueOnce([
343+
'Import variables from a stack',
344+
'Manually add custom variables to the list',
345+
]);
204346

205347
await baseClass.handleEnvImportFlow();
206348

@@ -209,7 +351,10 @@ describe('BaseClass', () => {
209351
expect(exitMock).not.toHaveBeenCalledWith(1);
210352
});
211353

212-
it('should call importVariableFromLocalConfig and importEnvFromStack if "Import variables from a stack" and "Import variables from the .env.local file" is selected', async () => {
354+
it(
355+
'should call importVariableFromLocalConfig and importEnvFromStack if "Import variables from a stack" ' +
356+
'and "Import variables from the .env.local file" is selected',
357+
async () => {
213358
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
214359
const importVariableFromLocalConfigMock = jest
215360
.spyOn(baseClass, 'importVariableFromLocalConfig')
@@ -226,7 +371,10 @@ describe('BaseClass', () => {
226371
expect(exitMock).not.toHaveBeenCalledWith(1);
227372
});
228373

229-
it('should call promptForEnvValues and importVariableFromLocalConfig if "Manually add custom variables to the list" and "Import variables from the .env.local file" is selected', async () => {
374+
it(
375+
'should call promptForEnvValues and importVariableFromLocalConfig if "Manually add custom variables ' +
376+
'to the list" and "Import variables from the .env.local file" is selected',
377+
async () => {
230378
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
231379
const importVariableFromLocalConfigMock = jest
232380
.spyOn(baseClass, 'importVariableFromLocalConfig')
@@ -243,7 +391,10 @@ describe('BaseClass', () => {
243391
expect(exitMock).not.toHaveBeenCalledWith(1);
244392
});
245393

246-
it('should call importEnvFromStack, promptForEnvValues and importVariableFromLocalConfig if all three options selected', async () => {
394+
it(
395+
'should call importEnvFromStack, promptForEnvValues and importVariableFromLocalConfig if all three ' +
396+
'options selected',
397+
async () => {
247398
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
248399
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
249400
const importVariableFromLocalConfigMock = jest
@@ -346,7 +497,8 @@ describe('BaseClass', () => {
346497
await expect(baseClass.selectStack()).rejects.toThrow('exit');
347498

348499
expect(logMock).toHaveBeenCalledWith(
349-
'No stacks were found in your organization, or you do not have access to any. Please create a stack in the organization to proceed in the organization.',
500+
'No stacks were found in your organization, or you do not have access to any. ' +
501+
'Please create a stack in the organization to proceed in the organization.',
350502
'error',
351503
);
352504
expect(exitMock).toHaveBeenCalledWith(1);

src/commands/launch/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export default class Launch extends BaseCommand<typeof Launch> {
2929
'<%= config.bin %> <%= command.id %> --config <path/to/launch/config/file> --type <options: GitHub|FileUpload> --name=<value> --environment=<value> --branch=<value> --build-command=<value> --framework=<option> --org=<value> --out-dir=<value> --variable-type="Import variables from a stack" --alias=<value>',
3030
// eslint-disable-next-line max-len
3131
'<%= config.bin %> <%= command.id %> --config <path/to/launch/config/file> --type <options: GitHub|FileUpload> --name=<value> --environment=<value> --branch=<value> --build-command=<value> --framework=<option> --org=<value> --out-dir=<value> --variable-type="Manually add custom variables to the list" --env-variables="APP_ENV:prod, TEST_ENV:testVal"',
32+
// eslint-disable-next-line max-len
33+
'<%= config.bin %> <%= command.id %> --config <path/to/launch/config/file> --type <options: GitHub|FileUpload> --name=<value> --environment=<value> --branch=<value> --build-command=<value> --framework=<option> --org=<value> --out-dir=<value> --variable-type="Import variables from a stack" --variable-type="Manually add custom variables to the list" --alias=<value>',
3234
];
3335

3436
static flags: FlagInput = {
@@ -64,10 +66,11 @@ export default class Launch extends BaseCommand<typeof Launch> {
6466
description: '[optional] Server Command.',
6567
}),
6668
'variable-type': Flags.string({
69+
multiple: true,
6770
options: [...config.variablePreparationTypeOptions],
6871
description:
6972
// eslint-disable-next-line max-len
70-
'[optional] Provide a variable type. <options: Import variables from a stack|Manually add custom variables to the list|Import variables from the .env.local file|Skip adding environment variables>',
73+
'[optional] Provide a variable type (can specify multiple times). <options: Import variables from a stack|Manually add custom variables to the list|Import variables from the .env.local file|Skip adding environment variables>',
7174
}),
7275
'show-variables': Flags.boolean({
7376
hidden: true,

0 commit comments

Comments
 (0)