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 ( ) => {
@@ -46,11 +46,13 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
4646 } ,
4747 externals : this . $previewAppPluginsService . getExternalPlugins ( device ) ,
4848 filesToSyncMap,
49- startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
49+ startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
5050 }
51- } ) ;
51+ } ) ;
5252 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 ;
5456 } ) ;
5557 }
5658
@@ -72,33 +74,41 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
7274 }
7375
7476 public async stopLiveSync ( ) : Promise < void > {
75- this . $previewSdkService . removeAllListeners ( ) ;
7677 this . $previewSdkService . stop ( ) ;
7778 }
7879
79- private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , files ?: string [ ] ) : Promise < void > {
80+ private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , files ?: string [ ] ) : Promise < FilesPayload > {
8081 this . $logger . info ( `Start syncing changes for platform ${ platform } .` ) ;
8182
8283 try {
8384 const { appFilesUpdaterOptions, env, projectDir } = data ;
8485 const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
86+ const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
8587 await this . preparePlatform ( platform , appFilesUpdaterOptions , env , projectData ) ;
8688
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+ }
8895
8996 this . $logger . info ( `Successfully synced changes for platform ${ platform } .` ) ;
97+
98+ return result ;
9099 } catch ( err ) {
91100 this . $logger . warn ( `Unable to apply changes for platform ${ platform } . Error is: ${ err } , ${ JSON . stringify ( err , null , 2 ) } .` ) ;
92101 }
93102 }
94103
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 ;
99109 }
100110
101- private getFilePayloads ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilePayload [ ] {
111+ private getFilesPayload ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilesPayload {
102112 const appFolderPath = path . join ( projectData . projectDir , APP_FOLDER_NAME ) ;
103113 const platformsAppFolderPath = path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) ;
104114
@@ -128,7 +138,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
128138 } ;
129139
130140 if ( filePayload . binary ) {
131- const bitmap = < string > this . $fs . readFile ( file ) ;
141+ const bitmap = < string > this . $fs . readFile ( file ) ;
132142 const base64 = Buffer . from ( bitmap ) . toString ( 'base64' ) ;
133143 filePayload . fileContents = base64 ;
134144 } else {
@@ -138,7 +148,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
138148 return filePayload ;
139149 } ) ;
140150
141- return payloads ;
151+ return { files : payloads , platform : platformData . normalizedPlatformName . toLowerCase ( ) } ;
142152 }
143153
144154 private async preparePlatform ( platform : string , appFilesUpdaterOptions : IAppFilesUpdaterOptions , env : Object , projectData : IProjectData ) : Promise < void > {
0 commit comments