@@ -105,19 +105,21 @@ export async function exportFile(
105105 await vscode . workspace . fs . createDirectory ( foldersUri ) ;
106106 }
107107
108- api . getDoc ( name ) . then ( ( data ) => {
109- if ( ! data || ! data . result ) {
110- throw new Error ( "Something wrong happened " ) ;
111- }
112- const content = data . result . content ;
113- const { noStorage, dontExportIfNoChanges } = config ( "export" ) ;
108+ const data = await api . getDoc ( name ) ;
109+ if ( ! data || ! data . result ) {
110+ throw new Error ( "Received malformed JSON object from server fetching document " ) ;
111+ }
112+ const content = data . result . content ;
113+ const { noStorage, dontExportIfNoChanges } = config ( "export" ) ;
114114
115- const promise = new Promise ( ( resolve , reject ) => {
116- if ( noStorage ) {
117- // get only the storage xml for the doc.
118- api . getDoc ( name + "?storageOnly=1" ) . then ( ( storageData ) => {
115+ const storageResult : { found : boolean ; content ?: string } = await new Promise ( ( resolve , reject ) => {
116+ if ( noStorage ) {
117+ // get only the storage xml for the doc.
118+ api
119+ . getDoc ( name + "?storageOnly=1" )
120+ . then ( ( storageData ) => {
119121 if ( ! storageData || ! storageData . result ) {
120- reject ( new Error ( "Something wrong happened fetching the storage data " ) ) ;
122+ reject ( new Error ( "Received malformed JSON object from server fetching storage only " ) ) ;
121123 }
122124 const storageContent = storageData . result . content ;
123125
@@ -133,60 +135,55 @@ export async function exportFile(
133135 } else {
134136 resolve ( { found : false } ) ;
135137 }
136- } ) ;
138+ } )
139+ . catch ( ( error ) => reject ( error ) ) ;
140+ } else {
141+ resolve ( { found : false } ) ;
142+ }
143+ } ) ;
144+
145+ const fileUri = vscode . Uri . file ( fileName ) ;
146+ if ( Buffer . isBuffer ( content ) ) {
147+ // This is a binary file
148+ let isSkipped = "" ;
149+ if ( dontExportIfNoChanges && ( await fileExists ( fileUri ) ) ) {
150+ const existingContent = await vscode . workspace . fs . readFile ( fileUri ) ;
151+ if ( content . equals ( existingContent ) ) {
152+ await vscode . workspace . fs . writeFile ( fileUri , content ) ;
137153 } else {
138- resolve ( { found : false } ) ;
154+ isSkipped = " => skipped - no changes." ;
139155 }
140- } ) ;
141-
142- return promise
143- . then ( async ( res : any ) => {
144- const fileUri = vscode . Uri . file ( fileName ) ;
145- if ( Buffer . isBuffer ( content ) ) {
146- // This is a binary file
147- let isSkipped = "" ;
148- if ( dontExportIfNoChanges && ( await fileExists ( fileUri ) ) ) {
149- const existingContent = await vscode . workspace . fs . readFile ( fileUri ) ;
150- if ( content . equals ( existingContent ) ) {
151- await vscode . workspace . fs . writeFile ( fileUri , content ) ;
152- } else {
153- isSkipped = " => skipped - no changes." ;
154- }
155- } else {
156- await vscode . workspace . fs . writeFile ( fileUri , content ) ;
157- }
158- log ( `Success ${ isSkipped } ` ) ;
159- } else {
160- // This is a text file
161- let joinedContent = content . join ( "\n" ) ;
162- let isSkipped = "" ;
156+ } else {
157+ await vscode . workspace . fs . writeFile ( fileUri , content ) ;
158+ }
159+ log ( `Success ${ isSkipped } ` ) ;
160+ } else {
161+ // This is a text file
162+ let joinedContent = content . join ( "\n" ) ;
163+ let isSkipped = "" ;
163164
164- if ( res . found ) {
165- joinedContent = res . content . toString ( "utf8" ) ;
166- }
165+ if ( storageResult . found ) {
166+ joinedContent = storageResult . content ;
167+ }
167168
168- if ( dontExportIfNoChanges && ( await fileExists ( fileUri ) ) ) {
169- const existingContent = new TextDecoder ( ) . decode ( await vscode . workspace . fs . readFile ( fileUri ) ) ;
170- // stringify to harmonise the text encoding.
171- if ( JSON . stringify ( joinedContent ) !== JSON . stringify ( existingContent ) ) {
172- await vscode . workspace . fs . writeFile ( fileUri , new TextEncoder ( ) . encode ( joinedContent ) ) ;
173- } else {
174- isSkipped = " => skipped - no changes." ;
175- }
176- } else {
177- await vscode . workspace . fs . writeFile ( fileUri , new TextEncoder ( ) . encode ( joinedContent ) ) ;
178- }
169+ if ( dontExportIfNoChanges && ( await fileExists ( fileUri ) ) ) {
170+ const existingContent = new TextDecoder ( ) . decode ( await vscode . workspace . fs . readFile ( fileUri ) ) ;
171+ // stringify to harmonise the text encoding.
172+ if ( JSON . stringify ( joinedContent ) !== JSON . stringify ( existingContent ) ) {
173+ await vscode . workspace . fs . writeFile ( fileUri , new TextEncoder ( ) . encode ( joinedContent ) ) ;
174+ } else {
175+ isSkipped = " => skipped - no changes." ;
176+ }
177+ } else {
178+ await vscode . workspace . fs . writeFile ( fileUri , new TextEncoder ( ) . encode ( joinedContent ) ) ;
179+ }
179180
180- log ( `Success ${ isSkipped } ` ) ;
181- }
182- } )
183- . catch ( ( error ) => {
184- throw error ;
185- } ) ;
186- } ) ;
181+ log ( `Success ${ isSkipped } ` ) ;
182+ }
187183 } catch ( error ) {
188- log ( "ERROR: " + error ) ;
189- throw error ;
184+ const errorStr = typeof error == "string" ? error : error instanceof Error ? error . message : JSON . stringify ( error ) ;
185+ log ( `ERROR${ errorStr . length ? `: ${ errorStr } ` : "" } ` ) ;
186+ throw errorStr ;
190187 }
191188}
192189
0 commit comments