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 ( ) => {
@@ -45,11 +45,13 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
45
45
appFilesUpdaterOptions : data . appFilesUpdaterOptions ,
46
46
} ,
47
47
filesToSyncMap,
48
- startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
48
+ startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
49
49
}
50
- } ) ;
50
+ } ) ;
51
51
await this . $previewAppPluginsService . comparePluginsOnDevice ( device ) ;
52
- await this . syncFilesForPlatformSafe ( data , device . platform ) ;
52
+ const payloads = await this . syncFilesForPlatformSafe ( data , device . platform ) ;
53
+
54
+ return payloads ;
53
55
} ) ;
54
56
}
55
57
@@ -71,33 +73,41 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
71
73
}
72
74
73
75
public async stopLiveSync ( ) : Promise < void > {
74
- this . $previewSdkService . removeAllListeners ( ) ;
75
76
this . $previewSdkService . stop ( ) ;
76
77
}
77
78
78
- private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , files ?: string [ ] ) : Promise < void > {
79
+ private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , files ?: string [ ] ) : Promise < FilesPayload > {
79
80
this . $logger . info ( `Start syncing changes for platform ${ platform } .` ) ;
80
81
81
82
try {
82
83
const { appFilesUpdaterOptions, env, projectDir } = data ;
83
84
const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
85
+ const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
84
86
await this . preparePlatform ( platform , appFilesUpdaterOptions , env , projectData ) ;
85
87
86
- await this . applyChanges ( projectData , platform , files ) ;
88
+ let result : FilesPayload = null ;
89
+ if ( files && files . length ) {
90
+ result = await this . applyChanges ( platformData , projectData , files ) ;
91
+ } else {
92
+ result = await this . getFilesPayload ( platformData , projectData ) ;
93
+ }
87
94
88
95
this . $logger . info ( `Successfully synced changes for platform ${ platform } .` ) ;
96
+
97
+ return result ;
89
98
} catch ( err ) {
90
99
this . $logger . warn ( `Unable to apply changes for platform ${ platform } . Error is: ${ err } , ${ JSON . stringify ( err , null , 2 ) } .` ) ;
91
100
}
92
101
}
93
102
94
- private async applyChanges ( projectData : IProjectData , platform : string , files : string [ ] ) {
95
- const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
96
- const payloads = this . getFilePayloads ( platformData , projectData , _ ( files ) . uniq ( ) . value ( ) ) ;
97
- await this . $previewSdkService . applyChanges ( payloads , platform ) ;
103
+ private async applyChanges ( platformData : IPlatformData , projectData : IProjectData , files : string [ ] ) : Promise < FilesPayload > {
104
+ const payloads = this . getFilesPayload ( platformData , projectData , _ ( files ) . uniq ( ) . value ( ) ) ;
105
+ await this . $previewSdkService . applyChanges ( payloads ) ;
106
+
107
+ return payloads ;
98
108
}
99
109
100
- private getFilePayloads ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilePayload [ ] {
110
+ private getFilesPayload ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilesPayload {
101
111
const appFolderPath = path . join ( projectData . projectDir , APP_FOLDER_NAME ) ;
102
112
const platformsAppFolderPath = path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) ;
103
113
@@ -127,7 +137,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
127
137
} ;
128
138
129
139
if ( filePayload . binary ) {
130
- const bitmap = < string > this . $fs . readFile ( file ) ;
140
+ const bitmap = < string > this . $fs . readFile ( file ) ;
131
141
const base64 = Buffer . from ( bitmap ) . toString ( 'base64' ) ;
132
142
filePayload . fileContents = base64 ;
133
143
} else {
@@ -137,7 +147,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
137
147
return filePayload ;
138
148
} ) ;
139
149
140
- return payloads ;
150
+ return { files : payloads , platform : platformData . normalizedPlatformName . toLowerCase ( ) } ;
141
151
}
142
152
143
153
private async preparePlatform ( platform : string , appFilesUpdaterOptions : IAppFilesUpdaterOptions , env : Object , projectData : IProjectData ) : Promise < void > {
0 commit comments