@@ -46,9 +46,9 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
46
46
let promise = Promise . resolve < FilesPayload > ( null ) ;
47
47
const startSyncFilesTimeout = async ( platform : string ) => {
48
48
await promise
49
- . then ( async ( ) => {
50
- const projectData = this . $projectDataService . getProjectData ( data . projectDir ) ;
51
- promise = this . applyChanges ( this . $platformsData . getPlatformData ( platform , projectData ) , projectData , filesToSyncMap [ platform ] ) ;
49
+ . then ( async ( ) => {
50
+ // We don't need to prepare when webpack emits changed files. We just need to send a message to pubnub.
51
+ promise = this . syncFilesForPlatformSafe ( data , platform , { filesToSync : filesToSyncMap [ platform ] , skipPrepare : true } ) ;
52
52
await promise ;
53
53
} ) ;
54
54
filesToSyncMap [ platform ] = [ ] ;
@@ -67,13 +67,12 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
67
67
}
68
68
} ) ;
69
69
await this . $previewAppPluginsService . comparePluginsOnDevice ( data , device ) ;
70
- const payloads = await this . syncFilesForPlatformSafe ( data , device . platform ) ;
71
- payloads . deviceId = device . id ;
70
+ const payloads = await this . syncFilesForPlatformSafe ( data , device . platform , { isInitialSync : true } ) ;
72
71
return payloads ;
73
72
}
74
73
75
- public async syncFiles ( data : IPreviewAppLiveSyncData , files ? : string [ ] ) : Promise < void > {
76
- this . showWarningsForNativeFiles ( files ) ;
74
+ public async syncFiles ( data : IPreviewAppLiveSyncData , filesToSync : string [ ] , filesToRemove : string [ ] ) : Promise < void > {
75
+ this . showWarningsForNativeFiles ( filesToSync ) ;
77
76
78
77
for ( const device of this . $previewSdkService . connectedDevices ) {
79
78
await this . $previewAppPluginsService . comparePluginsOnDevice ( data , device ) ;
@@ -85,83 +84,60 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
85
84
. value ( ) ;
86
85
87
86
for ( const platform of platforms ) {
88
- await this . syncFilesForPlatformSafe ( data , platform , files ) ;
87
+ await this . syncFilesForPlatformSafe ( data , platform , { filesToSync , filesToRemove } ) ;
89
88
}
90
89
}
91
90
92
91
public async stopLiveSync ( ) : Promise < void > {
93
92
this . $previewSdkService . stop ( ) ;
94
93
}
95
94
96
- private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , files ?: string [ ] ) : Promise < FilesPayload > {
95
+ private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , opts ?: { filesToSync ?: string [ ] , filesToRemove ?: string [ ] , isInitialSync ?: boolean , skipPrepare ?: boolean } ) : Promise < FilesPayload > {
97
96
this . $logger . info ( `Start syncing changes for platform ${ platform } .` ) ;
98
97
98
+ opts = opts || { } ;
99
+ const { filesToSync, filesToRemove } = opts ;
100
+ let payloads = null ;
101
+
99
102
try {
100
103
const { appFilesUpdaterOptions, env, projectDir } = data ;
101
104
const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
102
105
const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
103
- await this . preparePlatform ( platform , appFilesUpdaterOptions , env , projectData ) ;
104
106
105
- let result : FilesPayload = null ;
106
- if ( files && files . length ) {
107
- result = await this . applyChanges ( platformData , projectData , files ) ;
108
- this . $logger . info ( `Successfully synced ${ result . files . map ( filePayload => filePayload . file . yellow ) } for platform ${ platform } .` ) ;
109
- } else {
110
- result = await this . getFilesPayload ( platformData , projectData ) ;
107
+ if ( ! opts . skipPrepare ) {
108
+ await this . preparePlatform ( platform , appFilesUpdaterOptions , env , projectData ) ;
109
+ }
110
+
111
+ if ( opts . isInitialSync ) {
112
+ const platformsAppFolderPath = path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) ;
113
+ const files = this . $projectFilesManager . getProjectFiles ( platformsAppFolderPath ) ;
114
+ payloads = this . getFilesPayload ( platformData , projectData , files ) ;
111
115
this . $logger . info ( `Successfully synced changes for platform ${ platform } .` ) ;
116
+ } else {
117
+ const files = _ . map ( filesToSync , file => this . $projectFilesProvider . mapFilePath ( file , platformData . normalizedPlatformName , projectData ) ) ;
118
+ payloads = this . getFilesPayload ( platformData , projectData , files , filesToRemove ) ;
119
+ await this . $previewSdkService . applyChanges ( payloads ) ;
120
+ this . $logger . info ( `Successfully synced ${ payloads . files . map ( filePayload => filePayload . file . yellow ) } for platform ${ platform } .` ) ;
112
121
}
113
122
114
- return result ;
123
+ return payloads ;
115
124
} catch ( err ) {
116
125
this . $logger . warn ( `Unable to apply changes for platform ${ platform } . Error is: ${ err } , ${ JSON . stringify ( err , null , 2 ) } .` ) ;
117
126
}
118
127
}
119
128
120
- private async applyChanges ( platformData : IPlatformData , projectData : IProjectData , files : string [ ] ) : Promise < FilesPayload > {
121
- const payloads = this . getFilesPayload ( platformData , projectData , _ ( files ) . uniq ( ) . value ( ) ) ;
122
- await this . $previewSdkService . applyChanges ( payloads ) ;
123
-
124
- return payloads ;
125
- }
126
-
127
- private getFilesPayload ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilesPayload {
128
- const platformsAppFolderPath = path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) ;
129
-
130
- if ( files && files . length ) {
131
- files = files . map ( file => this . $projectFilesProvider . mapFilePath ( file , platformData . normalizedPlatformName , projectData ) ) ;
132
- } else {
133
- files = this . $projectFilesManager . getProjectFiles ( platformsAppFolderPath ) ;
134
- }
135
-
136
- const filesToTransfer = files
129
+ private getFilesPayload ( platformData : IPlatformData , projectData : IProjectData , filesToSync ?: string [ ] , filesToRemove ?: string [ ] ) : FilesPayload {
130
+ const filesToTransfer = filesToSync
137
131
. filter ( file => file . indexOf ( TNS_MODULES_FOLDER_NAME ) === - 1 )
138
132
. filter ( file => file . indexOf ( APP_RESOURCES_FOLDER_NAME ) === - 1 )
139
133
. filter ( file => ! _ . includes ( this . excludedFiles , path . basename ( file ) ) )
140
134
. filter ( file => ! _ . includes ( this . excludedFileExtensions , path . extname ( file ) ) ) ;
141
135
142
136
this . $logger . trace ( `Transferring ${ filesToTransfer . join ( "\n" ) } .` ) ;
143
137
144
- const payloads = filesToTransfer
145
- . map ( file => {
146
- const projectFileInfo = this . $projectFilesProvider . getProjectFileInfo ( file , platformData . normalizedPlatformName , null ) ;
147
- const relativePath = path . relative ( platformsAppFolderPath , file ) ;
148
- const filePayload : FilePayload = {
149
- event : PreviewSdkEventNames . CHANGE_EVENT_NAME ,
150
- file : path . join ( path . dirname ( relativePath ) , projectFileInfo . onDeviceFileName ) ,
151
- binary : isTextOrBinary . isBinarySync ( file ) ,
152
- fileContents : ""
153
- } ;
154
-
155
- if ( filePayload . binary ) {
156
- const bitmap = < string > this . $fs . readFile ( file ) ;
157
- const base64 = Buffer . from ( bitmap ) . toString ( 'base64' ) ;
158
- filePayload . fileContents = base64 ;
159
- } else {
160
- filePayload . fileContents = this . $fs . readText ( path . join ( path . dirname ( projectFileInfo . filePath ) , projectFileInfo . onDeviceFileName ) ) ;
161
- }
162
-
163
- return filePayload ;
164
- } ) ;
138
+ const payloadsToSync = filesToTransfer . map ( file => this . createFilePayload ( file , platformData , projectData , PreviewSdkEventNames . CHANGE_EVENT_NAME ) ) ;
139
+ const payloadsToRemove = _ . map ( filesToRemove , file => this . createFilePayload ( file , platformData , projectData , PreviewSdkEventNames . UNLINK_EVENT_NAME ) ) ;
140
+ const payloads = payloadsToSync . concat ( payloadsToRemove ) ;
165
141
166
142
return { files : payloads , platform : platformData . normalizedPlatformName . toLowerCase ( ) } ;
167
143
}
@@ -188,5 +164,36 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
188
164
_ . filter ( files , file => file . indexOf ( APP_RESOURCES_FOLDER_NAME ) > - 1 )
189
165
. forEach ( file => this . $logger . warn ( `Unable to apply changes from ${ APP_RESOURCES_FOLDER_NAME } folder. You need to build your application in order to make changes in ${ APP_RESOURCES_FOLDER_NAME } folder.` ) ) ;
190
166
}
167
+
168
+ private createFilePayload ( file : string , platformData : IPlatformData , projectData : IProjectData , event : string ) : FilePayload {
169
+ const projectFileInfo = this . $projectFilesProvider . getProjectFileInfo ( file , platformData . normalizedPlatformName , null ) ;
170
+ const binary = isTextOrBinary . isBinarySync ( file ) ;
171
+ let fileContents = "" ;
172
+ let filePath = "" ;
173
+
174
+ if ( event === PreviewSdkEventNames . CHANGE_EVENT_NAME ) {
175
+ const relativePath = path . relative ( path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) , file ) ;
176
+ filePath = path . join ( path . dirname ( relativePath ) , projectFileInfo . onDeviceFileName ) ;
177
+
178
+ if ( binary ) {
179
+ const bitmap = < string > this . $fs . readFile ( file ) ;
180
+ const base64 = Buffer . from ( bitmap ) . toString ( 'base64' ) ;
181
+ fileContents = base64 ;
182
+ } else {
183
+ fileContents = this . $fs . readText ( path . join ( path . dirname ( projectFileInfo . filePath ) , projectFileInfo . onDeviceFileName ) ) ;
184
+ }
185
+ } else if ( event === PreviewSdkEventNames . UNLINK_EVENT_NAME ) {
186
+ filePath = path . relative ( path . join ( projectData . projectDir , APP_FOLDER_NAME ) , file ) ;
187
+ }
188
+
189
+ const filePayload = {
190
+ event,
191
+ file : filePath ,
192
+ binary,
193
+ fileContents
194
+ } ;
195
+
196
+ return filePayload ;
197
+ }
191
198
}
192
199
$injector . register ( "previewAppLiveSyncService" , PreviewAppLiveSyncService ) ;
0 commit comments