@@ -2,11 +2,12 @@ import pick from "lodash/pick";
22import { resolve } from "path" ;
33import merge from "lodash/merge" ;
44import { flags } from "@contentstack/cli-utilities" ;
5+ import { App } from "@contentstack/management/types/app" ;
56import { existsSync , readFileSync , writeFileSync } from "fs" ;
67
7- import { getOrg } from "../../util" ;
88import { AppManifest } from "../../types" ;
99import { BaseCommand } from "./base-command" ;
10+ import { fetchApp , getApp , getOrg } from "../../util" ;
1011import { $t , appUpdate , commonMsg } from "../../messages" ;
1112
1213export default class Update extends BaseCommand < typeof Update > {
@@ -51,7 +52,10 @@ export default class Update extends BaseCommand<typeof Update> {
5152 }
5253
5354 try {
54- await this . flagsPromptQueue ( ) ;
55+ this . sharedConfig . org = await getOrg ( this . flags , {
56+ log : this . log ,
57+ managementSdk : this . managementSdk ,
58+ } ) ;
5559 await this . validateManifest ( ) ;
5660 await this . validateAppUid ( ) ;
5761 await this . appVersionValidation ( ) ;
@@ -63,54 +67,29 @@ export default class Update extends BaseCommand<typeof Update> {
6367 }
6468
6569 /**
66- * @method flagsPromptQueue
70+ * @method validateManifest
6771 *
6872 * @return {* } {Promise<void>}
6973 * @memberof Create
7074 */
71- async flagsPromptQueue ( ) : Promise < void > {
72- const validate = ( value : string ) => {
73- return ( val : string ) => {
74- if ( ! val ) return this . $t ( this . messages . NOT_EMPTY , { value } ) ;
75- return true ;
76- } ;
77- } ;
78-
79- if ( ! this . sharedConfig . orgValidated ) {
80- this . sharedConfig . org = await getOrg ( this . flags , {
81- log : this . log ,
82- managementSdk : this . managementSdk ,
83- } ) ;
84- this . sharedConfig . orgValidated = true ;
85- }
86-
87- if ( ! this . flags [ "app-uid" ] ) {
88- this . flags [ "app-uid" ] = ( await this . getValPrompt ( {
89- name : "appUid" ,
90- validate : validate ( "App UID" ) ,
91- message : this . messages . APP_UID ,
92- } ) ) as string ;
93- }
94-
75+ async validateManifest ( ) : Promise < void > {
9576 if ( ! this . flags [ "app-manifest" ] ) {
9677 this . flags [ "app-manifest" ] = ( await this . getValPrompt ( {
9778 name : "appManifest" ,
98- validate : validate ( "App manifest path" ) ,
79+ validate : ( val : string ) => {
80+ if ( ! val )
81+ return this . $t ( this . messages . NOT_EMPTY , {
82+ value : "App manifest path" ,
83+ } ) ;
84+ return true ;
85+ } ,
9986 message : this . $t ( this . messages . FILE_PATH , {
10087 fileName : "app manifest.json" ,
10188 } ) ,
10289 } ) ) as string ;
10390 }
104- }
10591
106- /**
107- * @method validateManifest
108- *
109- * @return {* } {Promise<void>}
110- * @memberof Create
111- */
112- async validateManifest ( ) : Promise < void > {
113- const manifestPath = this . flags [ "app-manifest" ] as string ;
92+ const manifestPath = this . flags [ "app-manifest" ] ;
11493 let hasError = false ;
11594 if ( existsSync ( manifestPath ) ) {
11695 try {
@@ -135,7 +114,6 @@ export default class Update extends BaseCommand<typeof Update> {
135114
136115 if ( this . manifestPathRetry < 3 ) {
137116 this . flags [ "app-manifest" ] = "" ;
138- await this . flagsPromptQueue ( ) ;
139117 await this . validateManifest ( ) ;
140118 } else {
141119 this . log ( this . messages . MAX_RETRY_LIMIT , "warn" ) ;
@@ -151,13 +129,28 @@ export default class Update extends BaseCommand<typeof Update> {
151129 * @memberof Create
152130 */
153131 async validateAppUid ( ) : Promise < void > {
132+ let appData ;
133+
134+ if ( ! this . flags [ "app-uid" ] ) {
135+ appData = ( await getApp ( this . flags , this . sharedConfig . org , {
136+ managementSdk : this . managementAppSdk ,
137+ log : this . log ,
138+ } ) ) as App ;
139+ } else {
140+ appData = await fetchApp ( this . flags , this . sharedConfig . org , {
141+ managementSdk : this . managementAppSdk ,
142+ log : this . log ,
143+ } ) ;
144+ }
145+
146+ this . flags [ "app-uid" ] = appData . uid ;
147+
154148 if ( this . flags [ "app-uid" ] !== this . manifestData ?. uid ) {
155149 this . log ( this . messages . APP_UID_NOT_MATCH , "error" ) ;
156150 this . appUidRetry ++ ;
157151
158152 if ( this . appUidRetry < 3 ) {
159153 this . flags [ "app-uid" ] = "" ;
160- await this . flagsPromptQueue ( ) ;
161154 await this . validateAppUid ( ) ;
162155 } else {
163156 this . log ( this . messages . MAX_RETRY_LIMIT , "warn" ) ;
0 commit comments