@@ -14,10 +14,9 @@ import { print } from '../util';
1414import BaseClass from './base-class' ;
1515import { getFileList } from '../util/fs' ;
1616import { createSignedUploadUrlMutation , importProjectMutation } from '../graphql' ;
17+ import { SignedUploadUrlData } from '../types/launch' ;
1718
1819export default class FileUpload extends BaseClass {
19- private signedUploadUrlData ! : Record < string , any > ;
20-
2120 /**
2221 * @method run
2322 *
@@ -30,31 +29,31 @@ export default class FileUpload extends BaseClass {
3029 } else {
3130 await this . handleNewProject ( ) ;
3231 }
33-
32+
3433 this . prepareLaunchConfig ( ) ;
3534 await this . showLogs ( ) ;
3635 this . showDeploymentUrl ( ) ;
3736 this . showSuggestion ( ) ;
3837 }
39-
38+
4039 private async handleExistingProject ( ) : Promise < void > {
4140 await this . initApolloClient ( ) ;
42-
41+
4342 let redeployLatest = this . config [ 'redeploy-latest' ] ;
4443
4544 if ( redeployLatest ) {
46- await this . createSignedUploadUrl ( ) ;
45+ const signedUploadUrlData = await this . createSignedUploadUrl ( ) ;
4746 const { zipName, zipPath } = await this . archive ( ) ;
48- await this . uploadFile ( zipName , zipPath ) ;
47+ await this . uploadFile ( zipName , zipPath , signedUploadUrlData ) ;
4948 }
5049
51- const { uploadUid } = this . signedUploadUrlData || { uploadUid : undefined } ;
50+ const { uploadUid } = { uploadUid : undefined } ;
5251 await this . createNewDeployment ( true , uploadUid ) ;
5352 }
54-
53+
5554 private async handleNewProject ( ) : Promise < void > {
56- await this . prepareForNewProjectCreation ( ) ;
57- await this . createNewProject ( ) ;
55+ const uploadUid = await this . prepareAndUploadNewProjectFile ( ) ;
56+ await this . createNewProject ( uploadUid ) ;
5857 }
5958
6059 /**
@@ -63,7 +62,7 @@ export default class FileUpload extends BaseClass {
6362 * @return {* } {Promise<void>}
6463 * @memberof FileUpload
6564 */
66- async createNewProject ( ) : Promise < void > {
65+ async createNewProject ( uploadUid : string ) : Promise < void > {
6766 const { framework, projectName, buildCommand, outputDirectory, environmentName, serverCommand } = this . config ;
6867 await this . apolloClient
6968 . mutate ( {
@@ -72,7 +71,7 @@ export default class FileUpload extends BaseClass {
7271 project : {
7372 projectType : 'FILEUPLOAD' ,
7473 name : projectName ,
75- fileUpload : { uploadUid : this . signedUploadUrlData . uploadUid } ,
74+ fileUpload : { uploadUid } ,
7675 environment : {
7776 frameworkPreset : framework ,
7877 outputDirectory : outputDirectory ,
@@ -96,7 +95,7 @@ export default class FileUpload extends BaseClass {
9695 const canRetry = await this . handleNewProjectCreationError ( error ) ;
9796
9897 if ( canRetry ) {
99- return this . createNewProject ( ) ;
98+ return this . createNewProject ( uploadUid ) ;
10099 }
101100 } ) ;
102101 }
@@ -107,7 +106,7 @@ export default class FileUpload extends BaseClass {
107106 * @return {* } {Promise<void>}
108107 * @memberof FileUpload
109108 */
110- async prepareForNewProjectCreation ( ) : Promise < void > {
109+ async prepareAndUploadNewProjectFile ( ) : Promise < string > {
111110 const {
112111 name,
113112 framework,
@@ -124,9 +123,9 @@ export default class FileUpload extends BaseClass {
124123 this . config . deliveryToken = token ;
125124 // this.fileValidation();
126125 await this . selectOrg ( ) ;
127- await this . createSignedUploadUrl ( ) ;
126+ const signedUploadUrlData = await this . createSignedUploadUrl ( ) ;
128127 const { zipName, zipPath, projectName } = await this . archive ( ) ;
129- await this . uploadFile ( zipName , zipPath ) ;
128+ await this . uploadFile ( zipName , zipPath , signedUploadUrlData ) ;
130129 this . config . projectName =
131130 name ||
132131 ( await cliux . inquire ( {
@@ -187,6 +186,7 @@ export default class FileUpload extends BaseClass {
187186 this . config . variableType = variableType as unknown as string ;
188187 this . config . envVariables = envVariables ;
189188 await this . handleEnvImportFlow ( ) ;
189+ return signedUploadUrlData . uploadUid ;
190190 }
191191
192192 /**
@@ -252,19 +252,23 @@ export default class FileUpload extends BaseClass {
252252 /**
253253 * @method createSignedUploadUrl - create pre signed url for file upload
254254 *
255- * @return {* } {Promise<void >}
255+ * @return {* } {Promise<SignedUploadUrlData >}
256256 * @memberof FileUpload
257257 */
258- async createSignedUploadUrl ( ) : Promise < void > {
259- this . signedUploadUrlData = await this . apolloClient
260- . mutate ( { mutation : createSignedUploadUrlMutation } )
261- . then ( ( { data : { signedUploadUrl } } ) => signedUploadUrl )
262- . catch ( ( error ) => {
263- this . log ( 'Something went wrong. Please try again.' , 'warn' ) ;
264- this . log ( error , 'error' ) ;
265- this . exit ( 1 ) ;
266- } ) ;
267- this . config . uploadUid = this . signedUploadUrlData . uploadUid ;
258+ async createSignedUploadUrl ( ) : Promise < SignedUploadUrlData > {
259+ try {
260+ const result = await this . apolloClient . mutate ( { mutation : createSignedUploadUrlMutation } ) ;
261+ const signedUploadUrlData = result . data . signedUploadUrl ;
262+ this . config . uploadUid = signedUploadUrlData . uploadUid ;
263+ return signedUploadUrlData ;
264+ } catch ( error ) {
265+ this . log ( 'Something went wrong. Please try again.' , 'warn' ) ;
266+ if ( error instanceof Error ) {
267+ this . log ( error . message , 'error' ) ;
268+ }
269+ this . exit ( 1 ) ;
270+ return { } as SignedUploadUrlData ;
271+ }
268272 }
269273
270274 /**
@@ -275,8 +279,8 @@ export default class FileUpload extends BaseClass {
275279 * @return {* } {Promise<void>}
276280 * @memberof FileUpload
277281 */
278- async uploadFile ( fileName : string , filePath : PathLike ) : Promise < void > {
279- const { uploadUrl, fields, headers, method } = this . signedUploadUrlData ;
282+ async uploadFile ( fileName : string , filePath : PathLike , signedUploadUrlData : SignedUploadUrlData ) : Promise < void > {
283+ const { uploadUrl, fields, headers, method } = signedUploadUrlData ;
280284 const formData = new FormData ( ) ;
281285
282286 if ( ! isEmpty ( fields ) ) {
0 commit comments