-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathlivesync-provider.ts
107 lines (92 loc) · 5 KB
/
livesync-provider.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
import * as path from "path";
import * as temp from "temp";
export class LiveSyncProvider implements ILiveSyncProvider {
constructor(private $androidLiveSyncServiceLocator: {factory: Function},
private $androidPlatformLiveSyncServiceLocator: {factory: Function},
private $iosLiveSyncServiceLocator: {factory: Function},
private $iosPlatformLiveSyncServiceLocator: {factory: Function},
private $platformService: IPlatformService,
private $platformsData: IPlatformsData,
private $logger: ILogger,
private $childProcess: IChildProcess,
private $options: IOptions,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }
private static FAST_SYNC_FILE_EXTENSIONS = [".css", ".xml" ,".html"];
private deviceSpecificLiveSyncServicesCache: IDictionary<any> = {};
public get deviceSpecificLiveSyncServices(): IDictionary<any> {
return {
android: (_device: Mobile.IDevice, $injector: IInjector) => {
if(!this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier]) {
this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier] = $injector.resolve(this.$androidLiveSyncServiceLocator.factory, {_device: _device});
}
return this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier];
},
ios: (_device: Mobile.IDevice, $injector: IInjector) => {
if(!this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier]) {
this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier] = $injector.resolve(this.$iosLiveSyncServiceLocator.factory, {_device: _device});
}
return this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier];
}
};
}
private platformSpecificLiveSyncServicesCache: IDictionary<any> = {};
public get platformSpecificLiveSyncServices(): IDictionary<any> {
return {
android: (_liveSyncData: ILiveSyncData, $injector: IInjector) => {
if(!this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.Android]) {
this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.Android] = $injector.resolve(this.$androidPlatformLiveSyncServiceLocator.factory, { _liveSyncData: _liveSyncData });
}
return this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.Android];
},
ios: (_liveSyncData: ILiveSyncData, $injector: IInjector) => {
if(!this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.iOS]) {
this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.iOS] = $injector.resolve(this.$iosPlatformLiveSyncServiceLocator.factory, { _liveSyncData: _liveSyncData });
}
return this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.iOS];
}
};
}
public buildForDevice(device: Mobile.IDevice): IFuture<string> {
return (() => {
this.$platformService.buildPlatform(device.deviceInfo.platform, {buildForDevice: !device.isEmulator}).wait();
let platformData = this.$platformsData.getPlatformData(device.deviceInfo.platform);
if (device.isEmulator) {
return this.$platformService.getLatestApplicationPackageForEmulator(platformData).wait().packageName;
}
return this.$platformService.getLatestApplicationPackageForDevice(platformData).wait().packageName;
}).future<string>()();
}
public preparePlatformForSync(platform: string): IFuture<void> {
return (() => {
this.$platformService.preparePlatform(platform).wait();
}).future<void>()();
}
public canExecuteFastSync(filePath: string, platform: string): boolean {
let platformData = this.$platformsData.getPlatformData(platform);
let fastSyncFileExtensions = LiveSyncProvider.FAST_SYNC_FILE_EXTENSIONS.concat(platformData.fastLivesyncFileExtensions);
return _.includes(fastSyncFileExtensions, path.extname(filePath));
}
public transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, isFullSync: boolean): IFuture<void> {
return (() => {
if (deviceAppData.platform.toLowerCase() === "android" || !deviceAppData.deviceSyncZipPath || !isFullSync) {
deviceAppData.device.fileSystem.transferFiles(deviceAppData, localToDevicePaths).wait();
} else {
temp.track();
let tempZip = temp.path({prefix: "sync", suffix: ".zip"});
this.$logger.trace("Creating zip file: " + tempZip);
if (this.$options.syncAllFiles) {
this.$childProcess.spawnFromEvent("zip", [ "-r", "-0", tempZip, "app" ], "close", { cwd: path.dirname(projectFilesPath) }).wait();
} else {
this.$childProcess.spawnFromEvent("zip", [ "-r", "-0", tempZip, "app", "-x", "app/tns_modules/*" ], "close", { cwd: path.dirname(projectFilesPath) }).wait();
}
deviceAppData.device.fileSystem.transferFiles(deviceAppData, [{
getLocalPath: () => tempZip,
getDevicePath: () => deviceAppData.deviceSyncZipPath,
getRelativeToProjectBasePath: () => "../sync.zip",
deviceProjectRootPath: deviceAppData.deviceProjectRootPath
}]).wait();
}
}).future<void>()();
}
}
$injector.register("liveSyncProvider", LiveSyncProvider);