@@ -41,8 +41,56 @@ export async function exportFile(name: string, fileName: string): Promise<any> {
41
41
throw new Error ( 'Something wrong happened' ) ;
42
42
}
43
43
const content = data . result . content ;
44
- fs . writeFileSync ( fileName , ( content || [ ] ) . join ( '\n' ) ) ;
45
- log ( 'Success' ) ;
44
+ const { noStorage, dontExportIfNoChanges } = config ( ) . get ( 'export' ) ;
45
+
46
+ const promise = new Promise ( ( resolve , reject ) => {
47
+ if ( noStorage ) {
48
+ // get only the storage xml for the doc.
49
+ api . getDoc ( name + '?storageOnly=1' ) . then ( storageData => {
50
+ if ( ! storageData || ! storageData . result ) {
51
+ reject ( new Error ( 'Something wrong happened fetching the storage data' ) ) ;
52
+ }
53
+ const storageContent = storageData . result . content ;
54
+
55
+ if ( storageContent . length > 1 && storageContent [ 0 ] ) {
56
+ const storageContentString = storageContent . join ( "\n" ) ;
57
+ const contentString = content . join ( "\n" ) ;
58
+
59
+ // find and replace the docs storage section with ''
60
+ resolve ( { 'found' : contentString . indexOf ( storageContentString ) >= 0 , 'content' : contentString . replace ( storageContentString , '' ) } ) ;
61
+ } else {
62
+ resolve ( { 'found' : false } ) ;
63
+ }
64
+ } ) ;
65
+ } else {
66
+ resolve ( { 'found' : false } ) ;
67
+ }
68
+ } ) ;
69
+
70
+ promise . then ( ( res :any ) => {
71
+ let joinedContent = ( content || [ ] ) . join ( "\n" ) . toString ( 'utf8' ) ;
72
+ let isSkipped = '' ;
73
+
74
+ if ( res . found ) {
75
+ joinedContent = res . content . toString ( 'utf8' ) ;
76
+ }
77
+
78
+ if ( dontExportIfNoChanges && fs . existsSync ( fileName ) ) {
79
+ const existingContent = fs . readFileSync ( fileName , "utf8" ) ;
80
+ // stringify to harmonise the text encoding.
81
+ if ( JSON . stringify ( joinedContent ) != JSON . stringify ( existingContent ) ) {
82
+ fs . writeFileSync ( fileName , joinedContent ) ;
83
+ } else {
84
+ isSkipped = ' => skipped - no changes.' ;
85
+ }
86
+ } else {
87
+ fs . writeFileSync ( fileName , joinedContent ) ;
88
+ }
89
+
90
+ log ( `Success ${ isSkipped } ` ) ;
91
+ } ) . catch ( error => {
92
+ throw error ;
93
+ } ) ;
46
94
} ) ;
47
95
} )
48
96
. catch ( error => {
0 commit comments