11import * as path from "path" ;
2- import { FilePayload , Device } from "nativescript-preview-sdk" ;
2+ import { FilePayload , Device , FilesPayload } from "nativescript-preview-sdk" ;
33import { PreviewSdkEventNames } from "./preview-app-constants" ;
44import { APP_FOLDER_NAME , APP_RESOURCES_FOLDER_NAME , TNS_MODULES_FOLDER_NAME } from "../../../constants" ;
55const isTextOrBinary = require ( 'istextorbinary' ) ;
@@ -9,6 +9,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
99 private excludedFiles = [ ".DS_Store" ] ;
1010
1111 constructor ( private $fs : IFileSystem ,
12+ private $errors : IErrors ,
1213 private $hooksService : IHooksService ,
1314 private $logger : ILogger ,
1415 private $platformService : IPlatformService ,
@@ -19,15 +20,14 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
1920 private $projectFilesManager : IProjectFilesManager ,
2021 private $projectFilesProvider : IProjectFilesProvider ) { }
2122
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+ }
2528
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 ) ;
2929 const filesToSyncMap : IDictionary < string [ ] > = { } ;
30- let promise = Promise . resolve ( ) ;
30+ let promise = Promise . resolve < FilesPayload > ( null ) ;
3131 const startSyncFilesTimeout = async ( platform : string ) => {
3232 await promise
3333 . then ( async ( ) => {
@@ -45,11 +45,13 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
4545 appFilesUpdaterOptions : data . appFilesUpdaterOptions ,
4646 } ,
4747 filesToSyncMap,
48- startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
48+ startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
4949 }
50- } ) ;
50+ } ) ;
5151 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 ;
5355 } ) ;
5456 }
5557
@@ -71,33 +73,41 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
7173 }
7274
7375 public async stopLiveSync ( ) : Promise < void > {
74- this . $previewSdkService . removeAllListeners ( ) ;
7576 this . $previewSdkService . stop ( ) ;
7677 }
7778
78- private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , files ?: string [ ] ) : Promise < void > {
79+ private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , files ?: string [ ] ) : Promise < FilesPayload > {
7980 this . $logger . info ( `Start syncing changes for platform ${ platform } .` ) ;
8081
8182 try {
8283 const { appFilesUpdaterOptions, env, projectDir } = data ;
8384 const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
85+ const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
8486 await this . preparePlatform ( platform , appFilesUpdaterOptions , env , projectData ) ;
8587
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+ }
8794
8895 this . $logger . info ( `Successfully synced changes for platform ${ platform } .` ) ;
96+
97+ return result ;
8998 } catch ( err ) {
9099 this . $logger . warn ( `Unable to apply changes for platform ${ platform } . Error is: ${ err } , ${ JSON . stringify ( err , null , 2 ) } .` ) ;
91100 }
92101 }
93102
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 ;
98108 }
99109
100- private getFilePayloads ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilePayload [ ] {
110+ private getFilesPayload ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilesPayload {
101111 const appFolderPath = path . join ( projectData . projectDir , APP_FOLDER_NAME ) ;
102112 const platformsAppFolderPath = path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) ;
103113
@@ -127,7 +137,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
127137 } ;
128138
129139 if ( filePayload . binary ) {
130- const bitmap = < string > this . $fs . readFile ( file ) ;
140+ const bitmap = < string > this . $fs . readFile ( file ) ;
131141 const base64 = Buffer . from ( bitmap ) . toString ( 'base64' ) ;
132142 filePayload . fileContents = base64 ;
133143 } else {
@@ -137,7 +147,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
137147 return filePayload ;
138148 } ) ;
139149
140- return payloads ;
150+ return { files : payloads , platform : platformData . normalizedPlatformName . toLowerCase ( ) } ;
141151 }
142152
143153 private async preparePlatform ( platform : string , appFilesUpdaterOptions : IAppFilesUpdaterOptions , env : Object , projectData : IProjectData ) : Promise < void > {
0 commit comments