Skip to content

Commit eba907b

Browse files
feat:Add 'Skip' option to bypass adding environment variables
1 parent 3ac04e3 commit eba907b

File tree

4 files changed

+151
-5
lines changed

4 files changed

+151
-5
lines changed

src/adapters/base-class.test.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import BaseClass from './base-class';
2+
import { cliux as ux } from '@contentstack/cli-utilities';
3+
4+
jest.mock('@contentstack/cli-utilities', () => ({
5+
cliux: {
6+
inquire: jest.fn(),
7+
table: jest.fn(),
8+
},
9+
}));
10+
11+
describe('BaseClass - handleEnvImportFlow', () => {
12+
let baseClass: BaseClass;
13+
let logMock: jest.Mock;
14+
let exitMock: jest.Mock;
15+
16+
beforeEach(() => {
17+
logMock = jest.fn();
18+
exitMock = jest.fn();
19+
baseClass = new BaseClass({
20+
log: logMock,
21+
exit: exitMock,
22+
config: {
23+
variablePreparationTypeOptions: [
24+
'Import variables from a stack',
25+
'Manually add custom variables to the list',
26+
'Import variables from the .env.local file',
27+
'Skip adding environment variables',
28+
],
29+
},
30+
} as any);
31+
});
32+
33+
afterEach(() => {
34+
jest.clearAllMocks();
35+
});
36+
37+
it('should exit if no options are selected', async () => {
38+
(ux.inquire as jest.Mock).mockResolvedValueOnce([]);
39+
40+
await baseClass.handleEnvImportFlow();
41+
42+
expect(logMock).toHaveBeenCalledWith(
43+
'Please select at least one option by pressing <space>, then press <enter> to proceed.',
44+
'error',
45+
);
46+
expect(exitMock).toHaveBeenCalledWith(1);
47+
});
48+
49+
it('should exit if "Skip adding environment variables" is selected with other options', async () => {
50+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
51+
(ux.inquire as jest.Mock).mockResolvedValueOnce([
52+
'Skip adding environment variables',
53+
'Import variables from a stack',
54+
]);
55+
56+
await baseClass.handleEnvImportFlow();
57+
58+
expect(logMock).toHaveBeenCalledWith(
59+
"The 'Skip' 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.",
60+
'error',
61+
);
62+
63+
expect(exitMock).toHaveBeenCalledWith(1);
64+
expect(importEnvFromStackMock).toHaveBeenCalled();
65+
});
66+
67+
it('should call importEnvFromStack if "Import variables from a stack" is selected', async () => {
68+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
69+
70+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from a stack']);
71+
72+
await baseClass.handleEnvImportFlow();
73+
74+
expect(importEnvFromStackMock).toHaveBeenCalled();
75+
});
76+
77+
it('should call promptForEnvValues if "Manually add custom variables to the list" is selected', async () => {
78+
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
79+
80+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Manually add custom variables to the list']);
81+
82+
await baseClass.handleEnvImportFlow();
83+
84+
expect(promptForEnvValuesMock).toHaveBeenCalled();
85+
});
86+
87+
it('should call importVariableFromLocalConfig if "Import variables from the .env.local file" is selected', async () => {
88+
const importVariableFromLocalConfigMock = jest
89+
.spyOn(baseClass, 'importVariableFromLocalConfig')
90+
.mockResolvedValueOnce();
91+
92+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from the .env.local file']);
93+
94+
await baseClass.handleEnvImportFlow();
95+
96+
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
97+
});
98+
99+
it('should set envVariables to an empty array if "Skip adding environment variables" is selected', async () => {
100+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Skip adding environment variables']);
101+
102+
await baseClass.handleEnvImportFlow();
103+
104+
expect(baseClass.envVariables).toEqual([]);
105+
expect(logMock).toHaveBeenCalledWith('Skipped adding environment variables.', 'info');
106+
});
107+
108+
it('should print all variables if envVariables are populated', async () => {
109+
const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
110+
const printAllVariablesMock = jest.spyOn(baseClass, 'printAllVariables').mockImplementation();
111+
(ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from a stack']);
112+
113+
baseClass.envVariables = [{ key: 'APP_ENV', value: 'test' }];
114+
115+
await baseClass.handleEnvImportFlow();
116+
117+
expect(importEnvFromStackMock).toHaveBeenCalled();
118+
expect(printAllVariablesMock).toHaveBeenCalled();
119+
expect(baseClass.envVariables).toEqual([{ key: 'APP_ENV', value: 'test' }]);
120+
});
121+
});

src/adapters/base-class.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ export default class BaseClass {
264264
this.log(error, 'error');
265265
this.exit(1);
266266
})) || [];
267+
268+
if(listOfStacks.length === 0) {
269+
this.log('No stacks found in the organization. Please create a stack first in the organization', 'error');
270+
this.exit(1);
271+
}
267272

268273
if (this.config.selectedStack) {
269274
this.config.selectedStack = find(listOfStacks, { api_key: this.config.selectedStack });
@@ -306,6 +311,11 @@ export default class BaseClass {
306311
this.exit(1);
307312
})) || [];
308313

314+
if (listOfDeliveryTokens.length === 0) {
315+
this.log('No delivery tokens found in the stack. Please create a delivery token first in the stack.', 'error');
316+
this.exit(1);
317+
}
318+
309319
if (this.config.deliveryToken) {
310320
this.config.deliveryToken = find(listOfDeliveryTokens, { token: this.config.deliveryToken });
311321
} else {
@@ -526,21 +536,31 @@ export default class BaseClass {
526536
// validate: this.inquireRequireValidation,
527537
}));
528538

539+
if (variablePreparationType.length === 0) {
540+
this.log('Please select at least one option by pressing <space>, then press <enter> to proceed.', 'error');
541+
this.exit(1);
542+
}
543+
if (includes(variablePreparationType, 'Skip adding environment variables') && variablePreparationType.length > 1) {
544+
this.log("The 'Skip' 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.", 'error');
545+
this.exit(1);
546+
}
529547
if (includes(variablePreparationType, 'Import variables from a stack')) {
530548
await this.importEnvFromStack();
531549
}
532550
if (includes(variablePreparationType, 'Manually add custom variables to the list')) {
533551
await this.promptForEnvValues();
534552
}
535-
if (includes(variablePreparationType, 'Import variables from the local env file')) {
553+
if (includes(variablePreparationType, 'Import variables from the .env.local file')) {
536554
await this.importVariableFromLocalConfig();
537555
}
556+
if (includes(variablePreparationType, 'Skip adding environment variables')) {
557+
this.envVariables = [];
558+
}
538559

539560
if (this.envVariables.length) {
540561
this.printAllVariables();
541562
} else {
542-
this.log('Please provide env file!', 'error');
543-
this.exit(1);
563+
this.log('Skipped adding environment variables.', 'info');
544564
}
545565
}
546566

@@ -559,6 +579,10 @@ export default class BaseClass {
559579
path: this.config.projectBasePath,
560580
}).parsed;
561581

582+
if (isEmpty(localEnv)) {
583+
this.log('No .env.local file found.', 'error');
584+
this.exit(1);
585+
}
562586
if (!isEmpty(localEnv)) {
563587
let envKeys: Record<string, any> = keys(localEnv);
564588
const existingEnvKeys = map(this.envVariables, 'key');

src/commands/launch/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default class Launch extends BaseCommand<typeof Launch> {
6262
'variable-type': Flags.string({
6363
options: [...config.variablePreparationTypeOptions],
6464
description:
65-
'[optional] Provide a variable type. <options: Import variables from a stack|Manually add custom variables to the list|Import variables from the local env file>',
65+
'[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>',
6666
}),
6767
'show-variables': Flags.boolean({
6868
hidden: true,

src/config/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const config = {
3737
variablePreparationTypeOptions: [
3838
'Import variables from a stack',
3939
'Manually add custom variables to the list',
40-
'Import variables from the local env file',
40+
'Import variables from the .env.local file',
41+
'Skip adding environment variables',
4142
],
4243
variableType: '',
4344
supportedFrameworksForServerCommands: ['ANGULAR', 'OTHER', 'REMIX'],

0 commit comments

Comments
 (0)