Skip to content

Commit 116b506

Browse files
committed
fix: types in BaseClass -> getEnvironment method
1 parent fce1db2 commit 116b506

File tree

7 files changed

+71
-49
lines changed

7 files changed

+71
-49
lines changed

src/adapters/base-class.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
EmitMessage,
3838
DeploymentLogResp,
3939
ServerLogResp,
40+
Environment,
4041
} from '../types';
4142

4243
export default class BaseClass {
@@ -86,9 +87,9 @@ export default class BaseClass {
8687
* @return {*} {Promise<void>}
8788
* @memberof GitHub
8889
*/
89-
async createNewDeployment(skipGitData = false, environment:string, uploadUid?: string): Promise<void> {
90+
async createNewDeployment(skipGitData = false, environmentUid:string, uploadUid?: string): Promise<void> {
9091
const deployment: Record<string, any> = {
91-
environment: environment
92+
environment: environmentUid
9293
};
9394

9495
if (uploadUid) {
@@ -676,7 +677,7 @@ export default class BaseClass {
676677
},
677678
baseUrl: this.config.manageApiBaseUrl,
678679
}).apolloClient;
679-
this.config.environment = await this.getEnvironment();
680+
this.config.environment = (await this.getEnvironment()).uid;
680681
this.config.deployment = (last(this.config.currentConfig.deployments) as Record<string, any>)?.uid;
681682
const logs = new LogPolling({
682683
config: this.config,
@@ -745,32 +746,38 @@ export default class BaseClass {
745746
this.exit(1);
746747
}
747748

748-
async getEnvironment(): Promise<any> {
749+
async getEnvironment(): Promise<Environment> | never {
749750
const environmentFlagInput = this.config['environment'];
750751

751752
if (!environmentFlagInput) {
752-
const defaultEnvironment = (first(this.config.currentConfig.environments) as Record<string, any>)?.uid;
753-
this.config.environment = defaultEnvironment;
753+
const defaultEnvironment = (first(this.config.currentConfig.environments) as Environment);
754+
this.setEnvironmentOnConfig(defaultEnvironment as Environment);
754755
return defaultEnvironment;
755756
}
756757
const environmentList = await this.fetchEnvironments();
757-
const isValidEnvironment = environmentList.find((env: any) => env.name === environmentFlagInput || env.uid === environmentFlagInput);
758+
let environment = environmentList.find((env: Environment) => env.name === environmentFlagInput || env.uid === environmentFlagInput);
758759

759-
if (!isValidEnvironment) {
760-
this.log('Invalid environment name!', 'error');
760+
if (!environment) {
761+
this.log(`Environment "${environmentFlagInput}" not found in this project. Please provide a valid environment name or UID.`, 'error');
761762
this.exit(1);
762763
}
763-
this.config.environment = isValidEnvironment.uid;
764-
return isValidEnvironment.uid;
764+
765+
environment = environment as Environment;
766+
this.setEnvironmentOnConfig(environment);
767+
return environment;
768+
}
769+
770+
async setEnvironmentOnConfig(environment: Environment): Promise<void> {
771+
this.config.environment = environment.uid;
765772
}
766773

767-
async fetchEnvironments(): Promise<any> {
774+
async fetchEnvironments(): Promise<Environment[]> | never {
768775
try {
769776
const { data } = await this.apolloClient.query({ query: environmentsQuery });
770777
const environments = map(data.Environments.edges, 'node');
771778
return environments;
772-
} catch (error: any) {
773-
this.log(error?.message, 'error');
779+
} catch (error: unknown) {
780+
this.log(error instanceof Error ? error.message : String(error), 'error');
774781
process.exit(1);
775782
}
776783
}
@@ -822,4 +829,4 @@ export default class BaseClass {
822829
});
823830
}
824831
}
825-
}
832+
}

src/adapters/file-upload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class FileUpload extends BaseClass {
2727
async run(): Promise<void> {
2828
if (this.config.isExistingProject) {
2929
const environment = await this.getEnvironment();
30-
await this.handleExistingProject(environment);
30+
await this.handleExistingProject(environment.uid);
3131
} else {
3232
await this.handleNewProject();
3333
}

src/adapters/github.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default class GitHub extends BaseClass {
2323
async run(): Promise<void> {
2424
if (this.config.isExistingProject) {
2525
const environment = await this.getEnvironment();
26-
await this.handleExistingProject(environment);
26+
await this.handleExistingProject(environment.uid);
2727
} else {
2828
await this.handleNewProject();
2929
}
@@ -34,7 +34,7 @@ export default class GitHub extends BaseClass {
3434
this.showSuggestion();
3535
}
3636

37-
private async handleExistingProject(environment:string): Promise<void> {
37+
private async handleExistingProject(environmentUid:string): Promise<void> {
3838
await this.initApolloClient();
3939

4040
const redeployLastUpload = this.config['redeploy-last-upload'];
@@ -49,7 +49,7 @@ export default class GitHub extends BaseClass {
4949
await this.confirmLatestRedeployment();
5050
}
5151

52-
await this.createNewDeployment(false, environment);
52+
await this.createNewDeployment(false, environmentUid);
5353
}
5454

5555
private async confirmLatestRedeployment(): Promise<void> {

src/adapters/pre-check.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default class PreCheck extends BaseClass {
6262
this.log('Current Project details:', { bold: true, color: 'green' });
6363
this.log(''); // Empty line
6464
const { name, projectType, repository, environments } = this.config.currentConfig;
65-
const [environment] = environments;
65+
const environment = await this.getEnvironment();
6666

6767
const detail: Record<string, any> = {
6868
'Project Name': name,

src/types/launch.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ type SignedUploadUrlData = {
8282
uploadUid: string;
8383
};
8484

85+
export type Environment = {
86+
uid: string;
87+
name: string;
88+
frameworkPreset: string;
89+
};
90+
8591
export {
8692
LogFn,
8793
ExitFn,

test/unit/adapters/file-upload.test.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ describe('File Upload', () => {
5151
const signedUploadUrlData = { uploadUrl: 'http://example.com/upload', uploadUid: '123456789' };
5252
const zipName = 'test.zip';
5353
const zipPath = '/path/to/zip';
54-
const defaultEnvironment = 'Default';
55-
const environmentFlagInput = 'environmentFlagInput';
54+
const defaultEnvironment = { uid: 'testEnvUid', name: 'Default', frameworkPreset: 'OTHER' };
55+
const environmentMatchedByEnvironmentFlagInput = {
56+
uid: 'environmentFlagInput',
57+
name: 'environmentFlagInput',
58+
frameworkPreset: 'OTHER',
59+
};
5660

5761
beforeEach(() => {
5862
getEnvironmentStub = stub(BaseClass.prototype, 'getEnvironment');
@@ -121,7 +125,7 @@ describe('File Upload', () => {
121125
expect(createNewDeploymentStub.calledOnce).to.be.true;
122126
expect(createNewDeploymentStub.args[0]).to.deep.equal([
123127
true,
124-
defaultEnvironment,
128+
defaultEnvironment.uid,
125129
signedUploadUrlData.uploadUid,
126130
]);
127131
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
@@ -131,7 +135,7 @@ describe('File Upload', () => {
131135
});
132136

133137
it('should run file upload flow successfully for existing project where environment and redeploy-latest flags are passed.', async () => {
134-
getEnvironmentStub.resolves(environmentFlagInput);
138+
getEnvironmentStub.resolves(environmentMatchedByEnvironmentFlagInput);
135139

136140
let adapterConstructorOptions = {
137141
config: {
@@ -152,7 +156,7 @@ describe('File Upload', () => {
152156
expect(createNewDeploymentStub.calledOnce).to.be.true;
153157
expect(createNewDeploymentStub.args[0]).to.deep.equal([
154158
true,
155-
environmentFlagInput,
159+
environmentMatchedByEnvironmentFlagInput.uid,
156160
signedUploadUrlData.uploadUid,
157161
]);
158162
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
@@ -179,15 +183,15 @@ describe('File Upload', () => {
179183
expect(archiveStub.calledOnce).to.be.false;
180184
expect(uploadFileStub.calledOnce).to.be.false;
181185
expect(createNewDeploymentStub.calledOnce).to.be.true;
182-
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, defaultEnvironment, undefined]);
186+
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, defaultEnvironment.uid, undefined]);
183187
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
184188
expect(showLogsStub.calledOnce).to.be.true;
185189
expect(showDeploymentUrlStub.calledOnce).to.be.true;
186190
expect(showSuggestionStub.calledOnce).to.be.true;
187191
});
188192

189193
it('should run file upload flow successfully for existing project where environment and redeploy-last-upload flags are passed', async () => {
190-
getEnvironmentStub.resolves(environmentFlagInput);
194+
getEnvironmentStub.resolves(environmentMatchedByEnvironmentFlagInput);
191195

192196
let adapterConstructorOptions = {
193197
config: {
@@ -205,7 +209,7 @@ describe('File Upload', () => {
205209
expect(archiveStub.calledOnce).to.be.false;
206210
expect(uploadFileStub.calledOnce).to.be.false;
207211
expect(createNewDeploymentStub.calledOnce).to.be.true;
208-
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, environmentFlagInput, undefined]);
212+
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, environmentMatchedByEnvironmentFlagInput.uid, undefined]);
209213
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
210214
expect(showLogsStub.calledOnce).to.be.true;
211215
expect(showDeploymentUrlStub.calledOnce).to.be.true;
@@ -246,7 +250,7 @@ describe('File Upload', () => {
246250
});
247251

248252
it('should exit with an error message when both --redeploy-last-upload and --redeploy-latest flags are passed alongwith environment flag', async () => {
249-
getEnvironmentStub.resolves(environmentFlagInput);
253+
getEnvironmentStub.resolves(environmentMatchedByEnvironmentFlagInput);
250254

251255
let adapterConstructorOptions = {
252256
config: {
@@ -305,15 +309,15 @@ describe('File Upload', () => {
305309
expect(uploadFileStub.calledOnce).to.be.true;
306310
expect(uploadFileStub.args[0]).to.deep.equal([zipName, zipPath, signedUploadUrlData]);
307311
expect(createNewDeploymentStub.calledOnce).to.be.true;
308-
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, defaultEnvironment, signedUploadUrlData.uploadUid]);
312+
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, defaultEnvironment.uid, signedUploadUrlData.uploadUid]);
309313
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
310314
expect(showLogsStub.calledOnce).to.be.true;
311315
expect(showDeploymentUrlStub.calledOnce).to.be.true;
312316
expect(showSuggestionStub.calledOnce).to.be.true;
313317
});
314318

315319
it('should show prompt and successfully redeploy with "new file" if the option to redeploy with new file is selected, when --redeploy-latest and --redeploy-last-upload flags are not passed and environment flag passed', async () => {
316-
getEnvironmentStub.resolves(environmentFlagInput);
320+
getEnvironmentStub.resolves(environmentMatchedByEnvironmentFlagInput);
317321
let adapterConstructorOptions = {
318322
config: {
319323
isExistingProject: true,
@@ -339,7 +343,7 @@ describe('File Upload', () => {
339343
expect(uploadFileStub.calledOnce).to.be.true;
340344
expect(uploadFileStub.args[0]).to.deep.equal([zipName, zipPath, signedUploadUrlData]);
341345
expect(createNewDeploymentStub.calledOnce).to.be.true;
342-
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, environmentFlagInput, signedUploadUrlData.uploadUid]);
346+
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, environmentMatchedByEnvironmentFlagInput.uid, signedUploadUrlData.uploadUid]);
343347
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
344348
expect(showLogsStub.calledOnce).to.be.true;
345349
expect(showDeploymentUrlStub.calledOnce).to.be.true;
@@ -372,15 +376,15 @@ describe('File Upload', () => {
372376
expect(archiveStub.calledOnce).to.be.false;
373377
expect(uploadFileStub.calledOnce).to.be.false;
374378
expect(createNewDeploymentStub.calledOnce).to.be.true;
375-
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, defaultEnvironment, undefined]);
379+
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, defaultEnvironment.uid, undefined]);
376380
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
377381
expect(showLogsStub.calledOnce).to.be.true;
378382
expect(showDeploymentUrlStub.calledOnce).to.be.true;
379383
expect(showSuggestionStub.calledOnce).to.be.true;
380384
});
381385

382386
it('should show prompt and successfully redeploy with "last file upload" if the option to redeploy with last file upload is selected, when --redeploy-latest and --redeploy-last-upload flags are not passed and environment flag passed', async () => {
383-
getEnvironmentStub.resolves(environmentFlagInput);
387+
getEnvironmentStub.resolves(environmentMatchedByEnvironmentFlagInput);
384388

385389
let adapterConstructorOptions = {
386390
config: {
@@ -405,7 +409,7 @@ describe('File Upload', () => {
405409
expect(archiveStub.calledOnce).to.be.false;
406410
expect(uploadFileStub.calledOnce).to.be.false;
407411
expect(createNewDeploymentStub.calledOnce).to.be.true;
408-
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, environmentFlagInput, undefined]);
412+
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, environmentMatchedByEnvironmentFlagInput.uid, undefined]);
409413
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
410414
expect(showLogsStub.calledOnce).to.be.true;
411415
expect(showDeploymentUrlStub.calledOnce).to.be.true;
@@ -451,6 +455,7 @@ describe('File Upload', () => {
451455
});
452456

453457
it('should exit if "No" is selected for prompt to redeploy, when --redeploy-latest and --redeploy-last-upload flags are not passed and environment flag is passed', async () => {
458+
getEnvironmentStub.resolves(defaultEnvironment);
454459
let adapterConstructorOptions = {
455460
config: {
456461
isExistingProject: true,

test/unit/adapters/github.test.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ describe('GitHub', () => {
4646
exitStub,
4747
showSuggestionStub;
4848

49-
const defaultEnvironment = 'Default';
50-
const environmentFlagInput = 'environmentFlagInput';
49+
const defaultEnvironment = { uid: 'testEnvUid', name: 'Default', frameworkPreset: 'OTHER' };
50+
const environmentMatchedByEnvironmentFlagInput = {
51+
uid: 'environmentFlagInput',
52+
name: 'environmentFlagInput',
53+
frameworkPreset: 'OTHER',
54+
};
5155

5256
beforeEach(() => {
5357
getEnvironmentStub = stub(BaseClass.prototype, 'getEnvironment').resolves();
@@ -117,7 +121,7 @@ describe('GitHub', () => {
117121
expect(createNewDeploymentStub.calledOnce).to.be.true;
118122
expect(createNewDeploymentStub.args[0]).to.deep.equal([
119123
false,
120-
defaultEnvironment
124+
defaultEnvironment.uid
121125
]);
122126
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
123127
expect(showLogsStub.calledOnce).to.be.true;
@@ -126,13 +130,13 @@ describe('GitHub', () => {
126130
});
127131

128132
it('should successfully run github flow for existing project when flag redeploy-latest and environment is passed ', async () => {
129-
getEnvironmentStub.resolves(environmentFlagInput);
133+
getEnvironmentStub.resolves(environmentMatchedByEnvironmentFlagInput);
130134

131135
let adapterConstructorOptions = {
132136
config: {
133137
isExistingProject: true,
134138
'redeploy-latest': true,
135-
environment: environmentFlagInput,
139+
environment: 'environmentFlagInput',
136140
},
137141
};
138142

@@ -143,7 +147,7 @@ describe('GitHub', () => {
143147
expect(createNewDeploymentStub.calledOnce).to.be.true;
144148
expect(createNewDeploymentStub.args[0]).to.deep.equal([
145149
false,
146-
environmentFlagInput
150+
environmentMatchedByEnvironmentFlagInput.uid
147151
]);
148152
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
149153
expect(showLogsStub.calledOnce).to.be.true;
@@ -180,12 +184,12 @@ describe('GitHub', () => {
180184
});
181185

182186
it('should abort github flow for existing project when flag redeploy-last-upload is passed and environment flag passed', async () => {
183-
getEnvironmentStub.resolves(environmentFlagInput)
187+
getEnvironmentStub.resolves(environmentMatchedByEnvironmentFlagInput)
184188
const adapterConstructorOptions = {
185189
config: {
186190
isExistingProject: true,
187191
'redeploy-last-upload': true,
188-
environment: environmentFlagInput,
192+
environment: 'environmentFlagInput',
189193
},
190194
};
191195
let exitStatusCode;
@@ -228,7 +232,7 @@ describe('GitHub', () => {
228232
expect(createNewDeploymentStub.calledOnce).to.be.true;
229233
expect(createNewDeploymentStub.args[0]).to.deep.equal([
230234
false,
231-
defaultEnvironment
235+
defaultEnvironment.uid
232236
]);
233237
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
234238
expect(showLogsStub.calledOnce).to.be.true;
@@ -237,12 +241,12 @@ describe('GitHub', () => {
237241
});
238242

239243
it('should show prompt and successfully redeploy with "latest commit" if the option to redeploy is selected, when --redeploy-latest flag is not passed and environment flag is passed', async() => {
240-
getEnvironmentStub.resolves(environmentFlagInput);
244+
getEnvironmentStub.resolves(environmentMatchedByEnvironmentFlagInput);
241245

242246
const adapterConstructorOptions = {
243247
config: {
244248
isExistingProject: true,
245-
environment: environmentFlagInput
249+
environment: 'environmentFlagInput'
246250
},
247251
};
248252
inquireStub.withArgs({
@@ -258,7 +262,7 @@ describe('GitHub', () => {
258262
expect(createNewDeploymentStub.calledOnce).to.be.true;
259263
expect(createNewDeploymentStub.args[0]).to.deep.equal([
260264
false,
261-
environmentFlagInput
265+
environmentMatchedByEnvironmentFlagInput.uid
262266
]);
263267
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
264268
expect(showLogsStub.calledOnce).to.be.true;
@@ -299,12 +303,12 @@ describe('GitHub', () => {
299303
});
300304

301305
it('should exit if "No" is selected for prompt to redeploy, when --redeploy-latest flag is not passed and environment flag passed', async() => {
302-
getEnvironmentStub.resolves(environmentFlagInput);
306+
getEnvironmentStub.resolves(environmentMatchedByEnvironmentFlagInput);
303307

304308
const adapterConstructorOptions = {
305309
config: {
306310
isExistingProject: true,
307-
environment: environmentFlagInput
311+
environment: 'environmentFlagInput'
308312
},
309313
};
310314
inquireStub.withArgs({

0 commit comments

Comments
 (0)