-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathmobile-helper.ts
78 lines (62 loc) · 2.46 KB
/
mobile-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
import * as helpers from "../helpers";
import * as shell from "shelljs";
export class MobileHelper implements Mobile.IMobileHelper {
private static DEVICE_PATH_SEPARATOR = "/";
constructor(private $errors: IErrors,
private $fs: IFileSystem,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $tempService: ITempService) { }
public get platformNames(): string[] {
return [this.$devicePlatformsConstants.iOS, this.$devicePlatformsConstants.Android];
}
public isAndroidPlatform(platform: string): boolean {
return !!(platform && (this.$devicePlatformsConstants.Android.toLowerCase() === platform.toLowerCase()));
}
public isiOSPlatform(platform: string): boolean {
return !!(platform && (this.$devicePlatformsConstants.iOS.toLowerCase() === platform.toLowerCase()));
}
public normalizePlatformName(platform: string): string {
if (this.isAndroidPlatform(platform)) {
return "Android";
} else if (this.isiOSPlatform(platform)) {
return "iOS";
}
return undefined;
}
public validatePlatformName(platform: string): string {
if (!platform) {
this.$errors.failWithHelp("No device platform specified.");
}
const normalizedPlatform = this.normalizePlatformName(platform);
if (!normalizedPlatform || !_.includes(this.platformNames, normalizedPlatform)) {
this.$errors.fail("'%s' is not a valid device platform. Valid platforms are %s.",
platform, helpers.formatListOfNames(this.platformNames));
}
return normalizedPlatform;
}
public buildDevicePath(...args: string[]): string {
return this.correctDevicePath(args.join(MobileHelper.DEVICE_PATH_SEPARATOR));
}
public correctDevicePath(filePath: string): string {
return helpers.stringReplaceAll(filePath, '\\', '/');
}
public isiOSTablet(deviceName: string): boolean {
return deviceName && deviceName.toLowerCase().indexOf("ipad") !== -1;
}
public async getDeviceFileContent(device: Mobile.IDevice, deviceFilePath: string, projectData: IProjectData): Promise<string> {
const uniqueFilePath = await this.$tempService.path({ suffix: ".tmp" });
const platform = device.deviceInfo.platform.toLowerCase();
try {
await device.fileSystem.getFile(deviceFilePath, projectData.projectIdentifiers[platform], uniqueFilePath);
} catch (e) {
return null;
}
if (this.$fs.exists(uniqueFilePath)) {
const text = this.$fs.readText(uniqueFilePath);
shell.rm(uniqueFilePath);
return text;
}
return null;
}
}
$injector.register("mobileHelper", MobileHelper);