@@ -13,7 +13,7 @@ const filesFilter = (file: any) => {
1313 return true ;
1414} ;
1515
16- const getCategory = ( fileName : string , addCategory : any | boolean ) : string => {
16+ export const getCategory = ( fileName : string , addCategory : any | boolean ) : string => {
1717 const fileExt = fileName . split ( "." ) . pop ( ) . toLowerCase ( ) ;
1818 if ( typeof addCategory === "object" ) {
1919 for ( const pattern of Object . keys ( addCategory ) ) {
@@ -45,22 +45,30 @@ export const getFileName = (
4545 [ key : string ] : string ;
4646 }
4747) : string => {
48- if ( map ) {
49- for ( const pattern of Object . keys ( map ) ) {
50- if ( new RegExp ( `^${ pattern } $` ) . test ( name ) ) {
51- name = name . replace ( new RegExp ( `^${ pattern } $` ) , map [ pattern ] ) ;
52- break ;
48+ if ( name . includes ( "/" ) ) {
49+ // This is a file from a web application
50+ const nameArr : string [ ] = name . split ( "/" ) ;
51+ const cat = addCategory ? getCategory ( name , addCategory ) : null ;
52+ return [ folder , cat , ...nameArr ] . filter ( notNull ) . join ( path . sep ) ;
53+ } else {
54+ // This is a class, routine or include file
55+ if ( map ) {
56+ for ( const pattern of Object . keys ( map ) ) {
57+ if ( new RegExp ( `^${ pattern } $` ) . test ( name ) ) {
58+ name = name . replace ( new RegExp ( `^${ pattern } $` ) , map [ pattern ] ) ;
59+ break ;
60+ }
5361 }
5462 }
63+ const fileNameArray : string [ ] = name . split ( "." ) ;
64+ const fileExt = fileNameArray . pop ( ) . toLowerCase ( ) ;
65+ const cat = addCategory ? getCategory ( name , addCategory ) : null ;
66+ if ( split ) {
67+ const fileName = [ folder , cat , ...fileNameArray ] . filter ( notNull ) . join ( path . sep ) ;
68+ return [ fileName , fileExt ] . join ( "." ) ;
69+ }
70+ return [ folder , cat , name ] . filter ( notNull ) . join ( path . sep ) ;
5571 }
56- const fileNameArray : string [ ] = name . split ( "." ) ;
57- const fileExt = fileNameArray . pop ( ) . toLowerCase ( ) ;
58- const cat = addCategory ? getCategory ( name , addCategory ) : null ;
59- if ( split ) {
60- const fileName = [ folder , cat , ...fileNameArray ] . filter ( notNull ) . join ( path . sep ) ;
61- return [ fileName , fileExt ] . join ( "." ) ;
62- }
63- return [ folder , cat , name ] . filter ( notNull ) . join ( path . sep ) ;
6472} ;
6573
6674export const getFolderName = ( folder : string , name : string , split : boolean , cat : string = null ) : string => {
@@ -122,26 +130,43 @@ export async function exportFile(
122130
123131 return promise
124132 . then ( ( res : any ) => {
125- let joinedContent = content instanceof Buffer ? content . toString ( "utf-8" ) : ( content || [ ] ) . join ( "\n" ) ;
126- let isSkipped = "" ;
133+ if ( Buffer . isBuffer ( content ) ) {
134+ // This is a binary file
135+ let isSkipped = "" ;
136+ if ( dontExportIfNoChanges && fs . existsSync ( fileName ) ) {
137+ const existingContent = fs . readFileSync ( fileName ) ;
138+ if ( content . equals ( existingContent ) ) {
139+ fs . writeFileSync ( fileName , content ) ;
140+ } else {
141+ isSkipped = " => skipped - no changes." ;
142+ }
143+ } else {
144+ fs . writeFileSync ( fileName , content ) ;
145+ }
146+ log ( `Success ${ isSkipped } ` ) ;
147+ } else {
148+ // This is a text file
149+ let joinedContent = content . join ( "\n" ) ;
150+ let isSkipped = "" ;
127151
128- if ( res . found ) {
129- joinedContent = res . content . toString ( "utf8" ) ;
130- }
152+ if ( res . found ) {
153+ joinedContent = res . content . toString ( "utf8" ) ;
154+ }
131155
132- if ( dontExportIfNoChanges && fs . existsSync ( fileName ) ) {
133- const existingContent = fs . readFileSync ( fileName , "utf8" ) ;
134- // stringify to harmonise the text encoding.
135- if ( JSON . stringify ( joinedContent ) !== JSON . stringify ( existingContent ) ) {
136- fs . writeFileSync ( fileName , joinedContent ) ;
156+ if ( dontExportIfNoChanges && fs . existsSync ( fileName ) ) {
157+ const existingContent = fs . readFileSync ( fileName , "utf8" ) ;
158+ // stringify to harmonise the text encoding.
159+ if ( JSON . stringify ( joinedContent ) !== JSON . stringify ( existingContent ) ) {
160+ fs . writeFileSync ( fileName , joinedContent ) ;
161+ } else {
162+ isSkipped = " => skipped - no changes." ;
163+ }
137164 } else {
138- isSkipped = " => skipped - no changes." ;
165+ fs . writeFileSync ( fileName , joinedContent ) ;
139166 }
140- } else {
141- fs . writeFileSync ( fileName , joinedContent ) ;
142- }
143167
144- log ( `Success ${ isSkipped } ` ) ;
168+ log ( `Success ${ isSkipped } ` ) ;
169+ }
145170 } )
146171 . catch ( ( error ) => {
147172 throw error ;
0 commit comments