1- import * as path from "path" ;
2- import * as constants from "../constants" ;
1+ // import * as path from "path";
2+ // import * as constants from "../constants";
33
4+ const deviceDescriptorPrimaryKey = "identifier" ;
5+
6+ // TODO: Rename this class to RunWorkflowService
47export class BundleWorkflowService implements IBundleWorkflowService {
8+ private liveSyncProcessesInfo : IDictionary < any > = { } ;
9+
510 constructor (
611 private $devicesService : Mobile . IDevicesService ,
12+ private $deviceWorkflowService : IDeviceWorkflowService ,
713 private $errors : IErrors ,
8- private $fs : IFileSystem ,
14+ private $liveSyncService : ILiveSyncService2 ,
15+ // private $fs: IFileSystem,
916 private $logger : ILogger ,
10- private $platformAddService : IPlatformAddService ,
17+ // private $platformAddService: IPlatformAddService,
1118 private $platformsData : IPlatformsData ,
1219 private $platformWatcherService : IPlatformWatcherService ,
20+ private $platformWorkflowService : IPlatformWorkflowService ,
1321 private $pluginsService : IPluginsService ,
14- private $projectChangesService : IProjectChangesService
22+ private $projectDataService : IProjectDataService ,
23+ // private $projectChangesService: IProjectChangesService
1524 ) { }
1625
1726 // processInfo[projectDir] = {
1827 // deviceDescriptors, nativeFilesWatcher, jsFilesWatcher
1928 // }
2029
21- public async start ( projectData : IProjectData , deviceDescriptors : ILiveSyncDeviceInfo [ ] , liveSyncInfo : ILiveSyncInfo ) : Promise < void > {
30+ public async start ( projectDir : string , deviceDescriptors : ILiveSyncDeviceInfo [ ] , liveSyncInfo : ILiveSyncInfo ) : Promise < void > {
31+ const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
2232 await this . initializeSetup ( projectData ) ;
2333
2434 const platforms = _ ( deviceDescriptors )
2535 . map ( device => this . $devicesService . getDeviceByIdentifier ( device . identifier ) )
2636 . map ( device => device . deviceInfo . platform )
2737 . uniq ( )
2838 . value ( ) ;
29- for ( const platform in platforms ) {
30- const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
3139
32- const shouldAddPlatform = this . shouldAddPlatform ( platformData , projectData , liveSyncInfo . nativePrepare ) ;
33- if ( shouldAddPlatform ) {
34- await this . $platformAddService . addPlatform ( {
35- platformParam : ( < any > liveSyncInfo ) . platformParam ,
36- frameworkPath : ( < any > liveSyncInfo ) . frameworkPath ,
37- nativePrepare : liveSyncInfo . nativePrepare
38- } , projectData ) ;
40+ const workflowData : IPlatformWorkflowData = {
41+ platformParam : null ,
42+ nativePrepare : liveSyncInfo . nativePrepare ,
43+ release : liveSyncInfo . release ,
44+ useHotModuleReload : liveSyncInfo . useHotModuleReload ,
45+ signingOptions : {
46+ teamId : ( < any > liveSyncInfo ) . teamId ,
47+ provision : ( < any > liveSyncInfo ) . provision
3948 }
49+ } ;
4050
41- this . $platformWatcherService . on ( "onInitialSync" , async ( ) => {
42- console . log ( "================= RECEIVED INITIAL SYNC ============= " ) ;
43- // check if we should build, install, transfer files
44- // AddActionToChain
45- } ) ;
46- this . $platformWatcherService . on ( "onFilesChange" , ( ) => {
47- console . log ( "=================== RECEIVED FILES CHANGE ================ " ) ;
48- // Emitted when webpack compilatation is done and native prepare is done
49- // console.log("--------- ========= ---------- ", data);
50- // AddActionToChain
51- } ) ;
51+ // Ensure platform is added before starting JS(webpack) and native prepare
52+ for ( const platform of platforms ) {
53+ const platformNameLowerCase = platform . toLowerCase ( ) ;
54+ const platformData = this . $platformsData . getPlatformData ( platformNameLowerCase , projectData ) ;
55+ workflowData . platformParam = platformNameLowerCase ;
56+ await this . $platformWorkflowService . addPlatformIfNeeded ( platformData , projectData , workflowData ) ;
57+ }
58+
59+ this . setLiveSyncProcessInfo ( projectDir , liveSyncInfo , deviceDescriptors ) ;
60+
61+ const initalSyncDeviceAction = async ( device : Mobile . IDevice ) : Promise < void > => {
62+ console . log ( "================== INITIAL SYNC DEVICE ACTION ================" ) ;
63+ const deviceBuildInfoDescriptor = _ . find ( deviceDescriptors , dd => dd . identifier === device . deviceInfo . identifier ) ;
64+ const platform = device . deviceInfo . platform ;
65+ const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
66+ const buildConfig = {
67+ buildForDevice : ! device . isEmulator ,
68+ device : device . deviceInfo . identifier ,
69+ release : liveSyncInfo . release ,
70+ clean : liveSyncInfo . clean ,
71+ iCloudContainerEnvironment : "" ,
72+ projectDir : projectData . projectDir ,
73+ teamId : < any > null ,
74+ provision : < any > null ,
75+ } ;
76+ const outputPath = deviceBuildInfoDescriptor . outputPath || platformData . getBuildOutputPath ( buildConfig ) ;
77+ const packageFilePath = await this . $platformWorkflowService . buildPlatformIfNeeded ( platformData , projectData , workflowData , buildConfig , outputPath ) ;
78+
79+ await this . $deviceWorkflowService . installOnDeviceIfNeeded ( device , platformData , projectData , buildConfig , packageFilePath , outputPath ) ;
80+
81+ await this . $liveSyncService . fullSync ( device , deviceBuildInfoDescriptor , projectData , liveSyncInfo ) ;
82+ } ;
83+
84+ // const filesChangeDeviceAction = async (device: Mobile.IDevice): Promise<void> {
85+ // // test
86+ // };
87+
88+ this . $platformWatcherService . on ( "onInitialSync" , async ( ) => { // TODO: emit correct initialSyncData -> platform + hasNativeChange
89+ await this . addActionToChain ( projectData . projectDir , ( ) => this . $devicesService . execute ( initalSyncDeviceAction , ( device : Mobile . IDevice ) => _ . some ( deviceDescriptors , deviceDescriptor => deviceDescriptor . identifier === device . deviceInfo . identifier ) ) ) ;
90+ } ) ;
91+ this . $platformWatcherService . on ( "fileChangeData" , ( ) => {
92+ console . log ( "=================== RECEIVED FILES CHANGE ================ " ) ;
93+ // Emitted when webpack compilatation is done and native prepare is done
94+ // console.log("--------- ========= ---------- ", data);
95+ // AddActionToChain
96+ } ) ;
5297
53- await this . $platformWatcherService . startWatcher ( platformData , projectData , {
54- webpackCompilerConfig : liveSyncInfo . webpackCompilerConfig ,
55- preparePlatformData : {
56- release : liveSyncInfo . release ,
57- useHotModuleReload : liveSyncInfo . useHotModuleReload ,
58- nativePrepare : liveSyncInfo . nativePrepare ,
59- signingOptions : {
60- teamId : ( < any > liveSyncInfo ) . teamId ,
61- provision : ( < any > liveSyncInfo ) . provision
98+ const shouldStartWatcher = ! liveSyncInfo . skipWatcher && ( liveSyncInfo . syncToPreviewApp || this . liveSyncProcessesInfo [ projectData . projectDir ] . deviceDescriptors . length ) ;
99+
100+ if ( shouldStartWatcher ) {
101+ // TODO: Extract the preparePlatformData to separate variable
102+ for ( const platform of platforms ) {
103+ const platformData = this . $platformsData . getPlatformData ( platform . toLocaleLowerCase ( ) , projectData ) ;
104+ await this . $platformWatcherService . startWatcher ( platformData , projectData , {
105+ webpackCompilerConfig : liveSyncInfo . webpackCompilerConfig ,
106+ preparePlatformData : {
107+ release : liveSyncInfo . release ,
108+ useHotModuleReload : liveSyncInfo . useHotModuleReload ,
109+ nativePrepare : liveSyncInfo . nativePrepare ,
110+ signingOptions : {
111+ teamId : ( < any > liveSyncInfo ) . teamId ,
112+ provision : ( < any > liveSyncInfo ) . provision
113+ }
62114 }
63- }
64- } ) ;
115+ } ) ;
116+ }
65117 }
118+ }
66119
67- for ( const deviceDescriptor in deviceDescriptors ) {
68- console . log ( "============ DEVICE DESCRIPTOR ============== " , deviceDescriptor ) ;
69- }
120+ public getLiveSyncDeviceDescriptors ( projectDir : string ) : ILiveSyncDeviceInfo [ ] {
121+ const liveSyncProcessesInfo = this . liveSyncProcessesInfo [ projectDir ] || < ILiveSyncProcessInfo > { } ;
122+ const currentDescriptors = liveSyncProcessesInfo . deviceDescriptors ;
123+ return currentDescriptors || [ ] ;
124+ }
125+
126+ private setLiveSyncProcessInfo ( projectDir : string , liveSyncInfo : ILiveSyncInfo , deviceDescriptors : ILiveSyncDeviceInfo [ ] ) : void {
127+ this . liveSyncProcessesInfo [ projectDir ] = this . liveSyncProcessesInfo [ projectDir ] || Object . create ( null ) ;
128+ this . liveSyncProcessesInfo [ projectDir ] . actionsChain = this . liveSyncProcessesInfo [ projectDir ] . actionsChain || Promise . resolve ( ) ;
129+ this . liveSyncProcessesInfo [ projectDir ] . currentSyncAction = this . liveSyncProcessesInfo [ projectDir ] . actionsChain ;
130+ this . liveSyncProcessesInfo [ projectDir ] . isStopped = false ;
131+ this . liveSyncProcessesInfo [ projectDir ] . syncToPreviewApp = liveSyncInfo . syncToPreviewApp ;
132+
133+ const currentDeviceDescriptors = this . getLiveSyncDeviceDescriptors ( projectDir ) ;
134+ this . liveSyncProcessesInfo [ projectDir ] . deviceDescriptors = _ . uniqBy ( currentDeviceDescriptors . concat ( deviceDescriptors ) , deviceDescriptorPrimaryKey ) ;
70135 }
71136
72137 private async initializeSetup ( projectData : IProjectData ) : Promise < void > {
@@ -78,15 +143,20 @@ export class BundleWorkflowService implements IBundleWorkflowService {
78143 }
79144 }
80145
81- private shouldAddPlatform ( platformData : IPlatformData , projectData : IProjectData , nativePrepare : INativePrepare ) : boolean {
82- const platformName = platformData . normalizedPlatformName . toLowerCase ( ) ;
83- const prepareInfo = this . $projectChangesService . getPrepareInfo ( platformName , projectData ) ;
84- const hasPlatformDirectory = this . $fs . exists ( path . join ( projectData . platformsDir , platformName ) ) ;
85- const shouldAddNativePlatform = ! nativePrepare || ! nativePrepare . skipNativePrepare ;
86- const requiresNativePlatformAdd = prepareInfo && prepareInfo . nativePlatformStatus === constants . NativePlatformStatus . requiresPlatformAdd ;
87- const result = ! hasPlatformDirectory || ( shouldAddNativePlatform && requiresNativePlatformAdd ) ;
146+ private async addActionToChain < T > ( projectDir : string , action : ( ) => Promise < T > ) : Promise < T > {
147+ const liveSyncInfo = this . liveSyncProcessesInfo [ projectDir ] ;
148+ if ( liveSyncInfo ) {
149+ liveSyncInfo . actionsChain = liveSyncInfo . actionsChain . then ( async ( ) => {
150+ if ( ! liveSyncInfo . isStopped ) {
151+ liveSyncInfo . currentSyncAction = action ( ) ;
152+ const res = await liveSyncInfo . currentSyncAction ;
153+ return res ;
154+ }
155+ } ) ;
88156
89- return result ;
157+ const result = await liveSyncInfo . actionsChain ;
158+ return result ;
159+ }
90160 }
91161}
92162$injector . register ( "bundleWorkflowService" , BundleWorkflowService ) ;
0 commit comments