1
1
import * as path from "path" ;
2
- import { FilePayload , Device } from "nativescript-preview-sdk" ;
2
+ import { FilePayload , Device , FilesPayload } from "nativescript-preview-sdk" ;
3
3
import { PreviewSdkEventNames } from "./preview-app-constants" ;
4
4
import { APP_FOLDER_NAME , APP_RESOURCES_FOLDER_NAME , TNS_MODULES_FOLDER_NAME } from "../../../constants" ;
5
5
const isTextOrBinary = require ( 'istextorbinary' ) ;
@@ -9,6 +9,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
9
9
private excludedFiles = [ ".DS_Store" ] ;
10
10
11
11
constructor ( private $fs : IFileSystem ,
12
+ private $errors : IErrors ,
12
13
private $hooksService : IHooksService ,
13
14
private $logger : ILogger ,
14
15
private $platformService : IPlatformService ,
@@ -19,15 +20,14 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
19
20
private $projectFilesManager : IProjectFilesManager ,
20
21
private $projectFilesProvider : IProjectFilesProvider ) { }
21
22
22
- public initialize ( ) {
23
- this . $previewSdkService . initialize ( ) ;
24
- }
23
+ public initialize ( data : IPreviewAppLiveSyncData ) {
24
+ this . $previewSdkService . initialize ( async ( device : Device ) => {
25
+ if ( ! device ) {
26
+ this . $errors . failWithoutHelp ( "Sending initial preview files without a specified device is not supported." ) ;
27
+ }
25
28
26
- public async initialSync ( data : IPreviewAppLiveSyncData ) : Promise < void > {
27
- this . $previewSdkService . on ( PreviewSdkEventNames . DEVICE_CONNECTED , async ( device : Device ) => {
28
- this . $logger . trace ( "Found connected device" , device ) ;
29
29
const filesToSyncMap : IDictionary < string [ ] > = { } ;
30
- let promise = Promise . resolve ( ) ;
30
+ let promise = Promise . resolve < FilesPayload > ( null ) ;
31
31
const startSyncFilesTimeout = async ( platform : string ) => {
32
32
await promise
33
33
. then ( async ( ) => {
@@ -46,11 +46,13 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
46
46
} ,
47
47
externals : this . $previewAppPluginsService . getExternalPlugins ( device ) ,
48
48
filesToSyncMap,
49
- startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
49
+ startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
50
50
}
51
- } ) ;
51
+ } ) ;
52
52
await this . $previewAppPluginsService . comparePluginsOnDevice ( device ) ;
53
- await this . syncFilesForPlatformSafe ( data , device . platform ) ;
53
+ const payloads = await this . syncFilesForPlatformSafe ( data , device . platform ) ;
54
+
55
+ return payloads ;
54
56
} ) ;
55
57
}
56
58
@@ -72,33 +74,41 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
72
74
}
73
75
74
76
public async stopLiveSync ( ) : Promise < void > {
75
- this . $previewSdkService . removeAllListeners ( ) ;
76
77
this . $previewSdkService . stop ( ) ;
77
78
}
78
79
79
- private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , files ?: string [ ] ) : Promise < void > {
80
+ private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , files ?: string [ ] ) : Promise < FilesPayload > {
80
81
this . $logger . info ( `Start syncing changes for platform ${ platform } .` ) ;
81
82
82
83
try {
83
84
const { appFilesUpdaterOptions, env, projectDir } = data ;
84
85
const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
86
+ const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
85
87
await this . preparePlatform ( platform , appFilesUpdaterOptions , env , projectData ) ;
86
88
87
- await this . applyChanges ( projectData , platform , files ) ;
89
+ let result : FilesPayload = null ;
90
+ if ( files && files . length ) {
91
+ result = await this . applyChanges ( platformData , projectData , files ) ;
92
+ } else {
93
+ result = await this . getFilesPayload ( platformData , projectData ) ;
94
+ }
88
95
89
96
this . $logger . info ( `Successfully synced changes for platform ${ platform } .` ) ;
97
+
98
+ return result ;
90
99
} catch ( err ) {
91
100
this . $logger . warn ( `Unable to apply changes for platform ${ platform } . Error is: ${ err } , ${ JSON . stringify ( err , null , 2 ) } .` ) ;
92
101
}
93
102
}
94
103
95
- private async applyChanges ( projectData : IProjectData , platform : string , files : string [ ] ) {
96
- const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
97
- const payloads = this . getFilePayloads ( platformData , projectData , _ ( files ) . uniq ( ) . value ( ) ) ;
98
- await this . $previewSdkService . applyChanges ( payloads , platform ) ;
104
+ private async applyChanges ( platformData : IPlatformData , projectData : IProjectData , files : string [ ] ) : Promise < FilesPayload > {
105
+ const payloads = this . getFilesPayload ( platformData , projectData , _ ( files ) . uniq ( ) . value ( ) ) ;
106
+ await this . $previewSdkService . applyChanges ( payloads ) ;
107
+
108
+ return payloads ;
99
109
}
100
110
101
- private getFilePayloads ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilePayload [ ] {
111
+ private getFilesPayload ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilesPayload {
102
112
const appFolderPath = path . join ( projectData . projectDir , APP_FOLDER_NAME ) ;
103
113
const platformsAppFolderPath = path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) ;
104
114
@@ -128,7 +138,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
128
138
} ;
129
139
130
140
if ( filePayload . binary ) {
131
- const bitmap = < string > this . $fs . readFile ( file ) ;
141
+ const bitmap = < string > this . $fs . readFile ( file ) ;
132
142
const base64 = Buffer . from ( bitmap ) . toString ( 'base64' ) ;
133
143
filePayload . fileContents = base64 ;
134
144
} else {
@@ -138,7 +148,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
138
148
return filePayload ;
139
149
} ) ;
140
150
141
- return payloads ;
151
+ return { files : payloads , platform : platformData . normalizedPlatformName . toLowerCase ( ) } ;
142
152
}
143
153
144
154
private async preparePlatform ( platform : string , appFilesUpdaterOptions : IAppFilesUpdaterOptions , env : Object , projectData : IProjectData ) : Promise < void > {
0 commit comments