Skip to content

Commit 61e6008

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

File tree

4 files changed

+191
-5
lines changed

4 files changed

+191
-5
lines changed

src/adapters/base-class.test.ts

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

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 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.', '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 were found in the selected stack. Please create a delivery token in the stack to continue', '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 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.", '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)