-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathlivesync-command-helper.ts
181 lines (149 loc) · 7.32 KB
/
livesync-command-helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { RunOnDeviceEvents } from "../constants";
import { DeployController } from "../controllers/deploy-controller";
export class LiveSyncCommandHelper implements ILiveSyncCommandHelper {
constructor(
private $buildDataService: IBuildDataService,
private $projectData: IProjectData,
private $options: IOptions,
private $deployController: DeployController,
private $iosDeviceOperations: IIOSDeviceOperations,
private $mobileHelper: Mobile.IMobileHelper,
private $devicesService: Mobile.IDevicesService,
private $injector: IInjector,
private $buildController: IBuildController,
private $analyticsService: IAnalyticsService,
private $errors: IErrors,
private $iOSSimulatorLogProvider: Mobile.IiOSSimulatorLogProvider,
private $cleanupService: ICleanupService,
private $runController: IRunController
) { }
private get $platformsDataService(): IPlatformsDataService {
return this.$injector.resolve("platformsDataService");
}
// TODO: Remove this and replace it with buildData
public getLiveSyncData(projectDir: string): ILiveSyncInfo {
const liveSyncInfo: ILiveSyncInfo = {
projectDir,
skipWatcher: !this.$options.watch || this.$options.justlaunch,
clean: this.$options.clean,
release: this.$options.release,
env: this.$options.env,
timeout: this.$options.timeout,
useHotModuleReload: this.$options.hmr,
force: this.$options.force,
emulator: this.$options.emulator
};
return liveSyncInfo;
}
public async getDeviceInstances(platform?: string): Promise<Mobile.IDevice[]> {
await this.$devicesService.initialize({
platform,
deviceId: this.$options.device,
emulator: this.$options.emulator,
skipInferPlatform: !platform,
sdk: this.$options.sdk
});
const devices = this.$devicesService.getDeviceInstances()
.filter(d => !platform || d.deviceInfo.platform.toLowerCase() === platform.toLowerCase());
return devices;
}
public async createDeviceDescriptors(devices: Mobile.IDevice[], platform: string, additionalOptions?: ILiveSyncCommandHelperAdditionalOptions): Promise<ILiveSyncDeviceDescriptor[]> {
// Now let's take data for each device:
const deviceDescriptors: ILiveSyncDeviceDescriptor[] = devices
.map(d => {
const outputPath = additionalOptions && additionalOptions.getOutputDirectory && additionalOptions.getOutputDirectory({
platform: d.deviceInfo.platform,
emulator: d.isEmulator,
projectDir: this.$projectData.projectDir
});
const buildData = this.$buildDataService.getBuildData(this.$projectData.projectDir, d.deviceInfo.platform, { ...this.$options.argv, outputPath, buildForDevice: !d.isEmulator });
const buildAction = additionalOptions && additionalOptions.buildPlatform ?
additionalOptions.buildPlatform.bind(additionalOptions.buildPlatform, d.deviceInfo.platform, buildData, this.$projectData) :
this.$buildController.build.bind(this.$buildController, buildData);
const info: ILiveSyncDeviceDescriptor = {
identifier: d.deviceInfo.identifier,
buildAction,
debuggingEnabled: additionalOptions && additionalOptions.deviceDebugMap && additionalOptions.deviceDebugMap[d.deviceInfo.identifier],
debugOptions: this.$options,
skipNativePrepare: additionalOptions && additionalOptions.skipNativePrepare,
buildData
};
return info;
});
return deviceDescriptors;
}
public getPlatformsForOperation(platform: string): string[] {
const availablePlatforms = platform ? [platform] : _.values<string>(this.$mobileHelper.platformNames.map(p => p.toLowerCase()));
return availablePlatforms;
}
public async executeCommandLiveSync(platform?: string, additionalOptions?: ILiveSyncCommandHelperAdditionalOptions) {
const devices = await this.getDeviceInstances(platform);
await this.executeLiveSyncOperation(devices, platform, additionalOptions);
}
public async executeLiveSyncOperation(devices: Mobile.IDevice[], platform: string, additionalOptions?: ILiveSyncCommandHelperAdditionalOptions): Promise<void> {
const { liveSyncInfo, deviceDescriptors } = await this.executeLiveSyncOperationCore(devices, platform, additionalOptions);
if (this.$options.release) {
await this.runInRelease(platform, deviceDescriptors);
return;
}
await this.$runController.run({
liveSyncInfo,
deviceDescriptors
});
const remainingDevicesToSync = devices.map(d => d.deviceInfo.identifier);
this.$runController.on(RunOnDeviceEvents.runOnDeviceStopped, (data: { projectDir: string, deviceIdentifier: string }) => {
_.remove(remainingDevicesToSync, d => d === data.deviceIdentifier);
if (remainingDevicesToSync.length === 0) {
process.exit(ErrorCodes.ALL_DEVICES_DISCONNECTED);
}
});
}
public async validatePlatform(platform: string): Promise<IDictionary<IValidatePlatformOutput>> {
const result: IDictionary<IValidatePlatformOutput> = {};
const availablePlatforms = this.getPlatformsForOperation(platform);
for (const availablePlatform of availablePlatforms) {
const platformData = this.$platformsDataService.getPlatformData(availablePlatform, this.$projectData);
const platformProjectService = platformData.platformProjectService;
const validateOutput = await platformProjectService.validate(this.$projectData, this.$options);
result[availablePlatform.toLowerCase()] = validateOutput;
}
return result;
}
private async executeLiveSyncOperationCore(devices: Mobile.IDevice[], platform: string, additionalOptions?: ILiveSyncCommandHelperAdditionalOptions): Promise<{liveSyncInfo: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceDescriptor[]}> {
if (!devices || !devices.length) {
if (platform) {
this.$errors.failWithoutHelp("Unable to find applicable devices to execute operation. Ensure connected devices are trusted and try again.");
} else {
this.$errors.failWithoutHelp("Unable to find applicable devices to execute operation and unable to start emulator when platform is not specified.");
}
}
const workingWithiOSDevices = !platform || this.$mobileHelper.isiOSPlatform(platform);
const shouldKeepProcessAlive = this.$options.watch || !this.$options.justlaunch;
if (shouldKeepProcessAlive) {
this.$analyticsService.setShouldDispose(false);
this.$cleanupService.setShouldDispose(false);
if (workingWithiOSDevices) {
this.$iosDeviceOperations.setShouldDispose(false);
this.$iOSSimulatorLogProvider.setShouldDispose(false);
}
}
const deviceDescriptors = await this.createDeviceDescriptors(devices, platform, additionalOptions);
const liveSyncInfo = this.getLiveSyncData(this.$projectData.projectDir);
return { liveSyncInfo, deviceDescriptors };
}
private async runInRelease(platform: string, deviceDescriptors: ILiveSyncDeviceDescriptor[]): Promise<void> {
await this.$devicesService.initialize({
platform,
deviceId: this.$options.device,
emulator: this.$options.emulator,
skipInferPlatform: !platform,
sdk: this.$options.sdk
});
await this.$deployController.deploy({ deviceDescriptors });
for (const deviceDescriptor of deviceDescriptors) {
const device = this.$devicesService.getDeviceByIdentifier(deviceDescriptor.identifier);
await device.applicationManager.startApplication({ appId: this.$projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()], projectName: this.$projectData.projectName, projectDir: this.$projectData.projectDir });
}
}
}
$injector.register("liveSyncCommandHelper", LiveSyncCommandHelper);