@@ -9,13 +9,15 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
99 private excludedFiles = [ ".DS_Store" ] ;
1010
1111 constructor ( private $fs : IFileSystem ,
12+ private $hooksService : IHooksService ,
1213 private $logger : ILogger ,
1314 private $platformService : IPlatformService ,
1415 private $platformsData : IPlatformsData ,
1516 private $projectDataService : IProjectDataService ,
1617 private $previewSdkService : IPreviewSdkService ,
1718 private $previewAppPluginsService : IPreviewAppPluginsService ,
18- private $projectFilesManager : IProjectFilesManager ) { }
19+ private $projectFilesManager : IProjectFilesManager ,
20+ private $projectFilesProvider : IProjectFilesProvider ) { }
1921
2022 public initialize ( ) {
2123 this . $previewSdkService . initialize ( ) ;
@@ -24,13 +26,47 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
2426 public async initialSync ( data : IPreviewAppLiveSyncData ) : Promise < void > {
2527 this . $previewSdkService . on ( PreviewSdkEventNames . DEVICE_CONNECTED , async ( device : Device ) => {
2628 this . $logger . trace ( "Found connected device" , device ) ;
27- await this . syncFilesOnDeviceSafe ( data , device ) ;
29+ const filesToSyncMap : IDictionary < string [ ] > = { } ;
30+ let promise = Promise . resolve ( ) ;
31+ const startSyncFilesTimeout = async ( platform : string ) => {
32+ promise
33+ . then ( async ( ) => {
34+ promise = this . syncFilesForPlatformSafe ( data , platform , filesToSyncMap [ platform ] ) ;
35+ await promise ;
36+ } ) ;
37+ filesToSyncMap [ platform ] = [ ] ;
38+ } ;
39+ await this . $hooksService . executeBeforeHooks ( "preview-sync" , {
40+ hookArgs : {
41+ projectData : this . $projectDataService . getProjectData ( data . projectDir ) ,
42+ config : {
43+ env : data . env ,
44+ platform : device . platform ,
45+ appFilesUpdaterOptions : data . appFilesUpdaterOptions ,
46+ } ,
47+ filesToSyncMap,
48+ startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
49+ }
50+ } ) ;
51+ await this . $previewAppPluginsService . comparePluginsOnDevice ( device ) ;
52+ await this . syncFilesForPlatformSafe ( data , device . platform ) ;
2853 } ) ;
2954 }
3055
31- public async syncFiles ( data : IPreviewAppLiveSyncData , files : string [ ] ) : Promise < void > {
56+ public async syncFiles ( data : IPreviewAppLiveSyncData , files ?: string [ ] ) : Promise < void > {
57+ this . showWarningsForNativeFiles ( files ) ;
58+
3259 for ( const device of this . $previewSdkService . connectedDevices ) {
33- await this . syncFilesOnDeviceSafe ( data , device , files ) ;
60+ await this . $previewAppPluginsService . comparePluginsOnDevice ( device ) ;
61+ }
62+
63+ const platforms = _ ( this . $previewSdkService . connectedDevices )
64+ . map ( device => device . platform )
65+ . uniq ( )
66+ . value ( ) ;
67+
68+ for ( const platform of platforms ) {
69+ await this . syncFilesForPlatformSafe ( data , platform , files ) ;
3470 }
3571 }
3672
@@ -39,37 +75,37 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
3975 this . $previewSdkService . stop ( ) ;
4076 }
4177
42- private async syncFilesOnDeviceSafe ( data : IPreviewAppLiveSyncData , device : Device , files ?: string [ ] ) : Promise < void > {
43- await this . $previewAppPluginsService . comparePluginsOnDevice ( device ) ;
44- this . showWarningsForNativeFiles ( files ) ;
45-
46- this . $logger . info ( `Start syncing changes on device ${ device . id } .` ) ;
78+ private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , files ?: string [ ] ) : Promise < void > {
79+ this . $logger . info ( `Start syncing changes for platform ${ platform } .` ) ;
4780
4881 try {
49- await this . syncFilesOnDevice ( data , device , files ) ;
50- this . $logger . info ( `Successfully synced changes on device ${ device . id } .` ) ;
82+ const { appFilesUpdaterOptions, env, projectDir } = data ;
83+ const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
84+ await this . preparePlatform ( platform , appFilesUpdaterOptions , env , projectData ) ;
85+
86+ // TODO: This should be refactored after implementing platform param in pubnub's meta data.
87+ const devices = this . $previewSdkService . connectedDevices . filter ( device => device . platform === platform ) ;
88+ for ( const device of devices ) {
89+ await this . applyChanges ( projectData , device , files ) ;
90+ }
91+
92+ this . $logger . info ( `Successfully synced changes for platform ${ platform } .` ) ;
5193 } catch ( err ) {
52- this . $logger . warn ( `Unable to apply changes on device ${ device . id } . Error is: ${ JSON . stringify ( err , null , 2 ) } .` ) ;
94+ this . $logger . warn ( `Unable to apply changes for platform ${ platform } . Error is: ${ err } , ${ JSON . stringify ( err , null , 2 ) } .` ) ;
5395 }
5496 }
5597
56- private async syncFilesOnDevice ( data : IPreviewAppLiveSyncData , device : Device , files ?: string [ ] ) : Promise < void > {
57- const { appFilesUpdaterOptions, env, projectDir } = data ;
58- const platform = device . platform ;
59- const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
60- const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
61-
62- await this . preparePlatform ( platform , appFilesUpdaterOptions , env , projectData ) ;
63-
64- const payloads = this . getFilePayloads ( platformData , projectData , files ) ;
65- await this . $previewSdkService . applyChanges ( payloads ) ;
98+ private async applyChanges ( projectData : IProjectData , device : Device , files : string [ ] ) {
99+ const platformData = this . $platformsData . getPlatformData ( device . platform , projectData ) ;
100+ const payloads = this . getFilePayloads ( platformData , projectData , _ ( files ) . uniq ( ) . value ( ) ) ;
101+ await this . $previewSdkService . applyChanges ( payloads , device . id ) ;
66102 }
67103
68104 private getFilePayloads ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilePayload [ ] {
69105 const appFolderPath = path . join ( projectData . projectDir , APP_FOLDER_NAME ) ;
70106 const platformsAppFolderPath = path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) ;
71107
72- if ( files ) {
108+ if ( files && files . length ) {
73109 files = files . map ( file => path . join ( platformsAppFolderPath , path . relative ( appFolderPath , file ) ) ) ;
74110 } else {
75111 files = this . $projectFilesManager . getProjectFiles ( platformsAppFolderPath ) ;
@@ -85,9 +121,15 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
85121
86122 const payloads = filesToTransfer
87123 . map ( file => {
124+ const projectFileInfo = this . $projectFilesProvider . getProjectFileInfo ( file , platformData . normalizedPlatformName , null ) ;
125+ const relativePath = path . relative ( platformsAppFolderPath , file ) ;
126+ const extName = path . extname ( relativePath ) ;
127+ const baseName = projectFileInfo . onDeviceFileName . split ( extName ) [ 0 ] ;
128+ const newFileName = `${ baseName } .${ platformData . normalizedPlatformName . toLowerCase ( ) } ${ extName } ` ;
129+
88130 const filePayload : FilePayload = {
89131 event : PreviewSdkEventNames . CHANGE_EVENT_NAME ,
90- file : path . relative ( platformsAppFolderPath , file ) ,
132+ file : path . join ( path . dirname ( relativePath ) , newFileName ) ,
91133 binary : isTextOrBinary . isBinarySync ( file ) ,
92134 fileContents : ""
93135 } ;
@@ -97,7 +139,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
97139 const base64 = new Buffer ( bitmap ) . toString ( 'base64' ) ;
98140 filePayload . fileContents = base64 ;
99141 } else {
100- filePayload . fileContents = this . $fs . readText ( file ) ;
142+ filePayload . fileContents = this . $fs . readText ( path . join ( path . dirname ( projectFileInfo . filePath ) , projectFileInfo . onDeviceFileName ) ) ;
101143 }
102144
103145 return filePayload ;
0 commit comments