@@ -600,7 +600,7 @@ export function getResource(sourceFile: string): Resource {
600600
601601
602602function importBundleJson ( file : File , json : BundledFormat , stream : ThroughStream ) : void {
603- let transifexEditorXlfs : Map < XLF > = Object . create ( null ) ;
603+ let bundleXlfs : Map < XLF > = Object . create ( null ) ;
604604
605605 for ( let source in json . keys ) {
606606 const projectResource = getResource ( source ) ;
@@ -613,13 +613,13 @@ function importBundleJson(file: File, json: BundledFormat, stream: ThroughStream
613613 log ( 'Error:' , `There is a mismatch between keys and messages in ${ file . relative } ` ) ;
614614 }
615615
616- let xlf = transifexEditorXlfs [ resource ] ? transifexEditorXlfs [ resource ] : transifexEditorXlfs [ resource ] = new XLF ( project ) ;
616+ let xlf = bundleXlfs [ resource ] ? bundleXlfs [ resource ] : bundleXlfs [ resource ] = new XLF ( project ) ;
617617 xlf . addFile ( source , keys , messages ) ;
618618 }
619619
620- for ( let resource in transifexEditorXlfs ) {
621- const newFilePath = `${ transifexEditorXlfs [ resource ] . project } /${ resource . replace ( / \/ / g, '_' ) } .xlf` ;
622- const xlfFile = new File ( { path : newFilePath , contents : new Buffer ( transifexEditorXlfs [ resource ] . toString ( ) , 'utf-8' ) } ) ;
620+ for ( let resource in bundleXlfs ) {
621+ const newFilePath = `${ bundleXlfs [ resource ] . project } /${ resource . replace ( / \/ / g, '_' ) } .xlf` ;
622+ const xlfFile = new File ( { path : newFilePath , contents : new Buffer ( bundleXlfs [ resource ] . toString ( ) , 'utf-8' ) } ) ;
623623 stream . emit ( 'data' , xlfFile ) ;
624624 }
625625}
@@ -720,6 +720,9 @@ function importIsl(file: File, stream: ThroughStream) {
720720}
721721
722722export function pushXlfFiles ( apiUrl : string , username : string , password : string ) : ThroughStream {
723+ let tryGetPromises = [ ] ;
724+ let updateCreatePromises = [ ] ;
725+
723726 return through ( function ( file : File ) {
724727 const project = path . dirname ( file . relative ) ;
725728 const fileName = path . basename ( file . path ) ;
@@ -730,12 +733,23 @@ export function pushXlfFiles(apiUrl: string, username: string, password: string)
730733 } ;
731734
732735 // Check if resource already exists, if not, then create it.
733- tryGetResource ( project , slug , apiUrl , credentials ) . then ( exists => {
736+ let promise = tryGetResource ( project , slug , apiUrl , credentials ) ;
737+ tryGetPromises . push ( promise ) ;
738+ promise . then ( exists => {
734739 if ( exists ) {
735- updateResource ( project , slug , file , apiUrl , credentials ) ;
740+ promise = updateResource ( project , slug , file , apiUrl , credentials ) ;
736741 } else {
737- createResource ( project , slug , file , apiUrl , credentials ) ;
742+ promise = createResource ( project , slug , file , apiUrl , credentials ) ;
738743 }
744+ updateCreatePromises . push ( promise ) ;
745+ } ) ;
746+
747+ } , function ( ) {
748+ // End the pipe only after all the communication with Transifex API happened
749+ Promise . all ( tryGetPromises ) . then ( ( ) => {
750+ Promise . all ( updateCreatePromises ) . then ( ( ) => {
751+ this . emit ( 'end' ) ;
752+ } ) ;
739753 } ) ;
740754 } ) ;
741755}
@@ -755,54 +769,60 @@ function tryGetResource(project: string, slug: string, apiUrl: string, credentia
755769 } ) ;
756770}
757771
758- function createResource ( project : string , slug : string , xlfFile : File , apiUrl :string , credentials : any ) : void {
759- const url = `${ apiUrl } /project/${ project } /resources` ;
760- const options = {
761- 'body' : {
762- 'content' : xlfFile . contents . toString ( ) ,
763- 'name' : slug ,
764- 'slug' : slug ,
765- 'i18n_type' : 'XLIFF'
766- } ,
767- 'json' : true ,
768- 'auth' : credentials
769- } ;
772+ function createResource ( project : string , slug : string , xlfFile : File , apiUrl :string , credentials : any ) : Promise < any > {
773+ return new Promise ( ( resolve ) => {
774+ const url = `${ apiUrl } /project/${ project } /resources` ;
775+ const options = {
776+ 'body' : {
777+ 'content' : xlfFile . contents . toString ( ) ,
778+ 'name' : slug ,
779+ 'slug' : slug ,
780+ 'i18n_type' : 'XLIFF'
781+ } ,
782+ 'json' : true ,
783+ 'auth' : credentials
784+ } ;
770785
771- request . post ( url , options , function ( err , res ) {
772- if ( err ) {
773- log ( 'Error:' , `Failed to create Transifex ${ project } /${ slug } : ${ err } ` ) ;
774- }
786+ request . post ( url , options , function ( err , res ) {
787+ if ( err ) {
788+ log ( 'Error:' , `Failed to create Transifex ${ project } /${ slug } : ${ err } ` ) ;
789+ }
775790
776- if ( res . statusCode === 201 ) {
777- log ( `Resource ${ project } /${ slug } successfully created on Transifex.` ) ;
778- } else {
779- log ( 'Error:' , `Something went wrong creating ${ slug } in ${ project } . ${ res . statusCode } ` ) ;
780- }
791+ if ( res . statusCode === 201 ) {
792+ log ( `Resource ${ project } /${ slug } successfully created on Transifex.` ) ;
793+ resolve ( ) ;
794+ } else {
795+ log ( 'Error:' , `Something went wrong creating ${ slug } in ${ project } . ${ res . statusCode } ` ) ;
796+ }
797+ } ) ;
781798 } ) ;
782799}
783800
784801/**
785802 * The following link provides information about how Transifex handles updates of a resource file:
786803 * https://dev.befoolish.co/tx-docs/public/projects/updating-content#what-happens-when-you-update-files
787804 */
788- function updateResource ( project : string , slug : string , xlfFile : File , apiUrl : string , credentials : any ) : void {
789- const url = `${ apiUrl } /project/${ project } /resource/${ slug } /content` ;
790- const options = {
791- 'body' : { 'content' : xlfFile . contents . toString ( ) } ,
792- 'json' : true ,
793- 'auth' : credentials
794- } ;
805+ function updateResource ( project : string , slug : string , xlfFile : File , apiUrl : string , credentials : any ) : Promise < any > {
806+ return new Promise ( ( resolve ) => {
807+ const url = `${ apiUrl } /project/${ project } /resource/${ slug } /content` ;
808+ const options = {
809+ 'body' : { 'content' : xlfFile . contents . toString ( ) } ,
810+ 'json' : true ,
811+ 'auth' : credentials
812+ } ;
795813
796- request . put ( url , options , function ( err , res , body ) {
797- if ( err ) {
798- log ( 'Error:' , `Failed to update Transifex ${ project } /${ slug } : ${ err } ` ) ;
799- }
814+ request . put ( url , options , function ( err , res , body ) {
815+ if ( err ) {
816+ log ( 'Error:' , `Failed to update Transifex ${ project } /${ slug } : ${ err } ` ) ;
817+ }
800818
801- if ( res . statusCode === 200 ) {
802- log ( `Resource ${ project } /${ slug } successfully updated on Transifex. Strings added: ${ body [ 'strings_added' ] } , updated: ${ body [ 'strings_updated' ] } , deleted: ${ body [ 'strings_delete' ] } ` ) ;
803- } else {
804- log ( 'Error:' , `Something went wrong updating ${ slug } in ${ project } . ${ res . statusCode } ` ) ;
805- }
819+ if ( res . statusCode === 200 ) {
820+ log ( `Resource ${ project } /${ slug } successfully updated on Transifex. Strings added: ${ body [ 'strings_added' ] } , updated: ${ body [ 'strings_updated' ] } , deleted: ${ body [ 'strings_delete' ] } ` ) ;
821+ resolve ( ) ;
822+ } else {
823+ log ( 'Error:' , `Something went wrong updating ${ slug } in ${ project } . ${ res . statusCode } ` ) ;
824+ }
825+ } ) ;
806826 } ) ;
807827}
808828
0 commit comments