-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathgoIOSTracker.ts
More file actions
160 lines (142 loc) · 4.45 KB
/
goIOSTracker.ts
File metadata and controls
160 lines (142 loc) · 4.45 KB
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
import { exec } from 'child_process';
import _ from 'lodash';
import semver from 'semver';
import { EventEmitter } from 'stream';
import { SubProcess } from 'teen_process';
import { cachePath } from './helpers';
import log from './logger';
export default class GoIosTracker extends EventEmitter {
private static instance: GoIosTracker;
private deviceMap: Map<number, string> = new Map();
private process!: SubProcess;
private started = true;
constructor() {
super();
this.start();
}
public static getInstance(): GoIosTracker {
if (!GoIosTracker.instance) {
GoIosTracker.instance = new GoIosTracker();
}
return GoIosTracker.instance;
}
start() {
if (!_.isNil(this.process) && this.process.isRunning) {
return;
}
let goIOSPath;
if (process.env.GO_IOS) {
log.info('Found GO_IOS in env');
goIOSPath = process.env.GO_IOS;
} else {
goIOSPath = `${cachePath('goIOS')}/ios`;
}
try {
this.process = new SubProcess(goIOSPath, ['listen']);
} catch (err: any) {
log.info(
`Failed to load go-ios ${goIOSPath}, iOS real device tracking not possible, please refer to link https://appium-device-farm-eight.vercel.app/troubleshooting/#ios-tracking for more details`,
);
}
this.process.on('lines-stdout', (out) => {
const parsedOutput = this.parseOutput(out);
if (!_.isNil(parsedOutput)) {
this.notify(parsedOutput);
}
});
this.process.on('exit', () => {
this.started = false;
this.emit('stop');
});
this.process.start(0);
}
async stop() {
if (_.isNil(this.process) || !this.process.isRunning) {
return;
}
this.process.stop('SIGINT');
this.started = false;
}
private parseOutput(output: any) {
try {
if (_.isArray(output)) {
return output.map((o) => JSON.parse(o));
}
} catch (err) {
return null;
}
}
private notify(messages: any[]) {
messages.forEach((message) => {
if (message.MessageType == 'Attached') {
this.deviceMap.set(message.DeviceID, message.Properties.SerialNumber);
this.emit('attached', message.Properties.SerialNumber);
} else {
const id = this.deviceMap.get(message.DeviceID);
this.emit('detached', id);
}
});
}
}
/**
* Start a go-ios tunnel for a device
* @param udid - The device UDID
* @param sdk - The device SDK version
* @param goIOSAgentPort - The port to use for the go-ios agent (optional)
*/
export async function startTunnel(
udid: string,
sdk?: string,
goIOSAgentPort?: number,
): Promise<void> {
const goIOS = process.env.GO_IOS;
log.info(`Go IOS: ${goIOS}`);
// Check if GO_IOS is configured
if (!goIOS) {
log.info('GO_IOS environment variable not set, skipping tunnel setup');
return;
}
// Check if goIOSAgentPort is provided
if (!goIOSAgentPort) {
log.info('Go IOS Agent Port not provided, skipping tunnel setup');
return;
}
// SDK version checking
const sdkRaw = sdk?.toString();
const sdkNormalized = sdkRaw ? sdkRaw.trim().toLowerCase().replace(/x/g, '0') : undefined;
const sdkCoerced = semver.coerce(sdkNormalized ?? sdkRaw)?.version;
const isAtLeast17 = sdkCoerced ? semver.satisfies(sdkCoerced, '>=17.0.0') : false;
log.info(`Device SDK: ${sdkRaw}`);
if (sdkNormalized && sdkNormalized !== sdkRaw) {
log.info(`Normalized SDK: ${sdkNormalized}`);
}
log.info(`Coerced SDK: ${sdkCoerced ?? 'invalid'}`);
log.info(`Semver satisfies (>=17.0.0): ${isAtLeast17}`);
log.info(`Go IOS Agent Port: ${goIOSAgentPort} for device ${udid}`);
// Check for version above 17+ and presence for Go IOS
if (!isAtLeast17) {
log.info(`Device SDK version ${sdkRaw} is below 17.0.0, skipping go-ios tunnel setup`);
return;
}
try {
log.info('Running go-ios agent');
const startTunnelCmd = `GO_IOS_AGENT_PORT=${goIOSAgentPort} ${goIOS} tunnel start --userspace --udid=${udid}`;
log.info(`Starting go-ios tunnel: ${startTunnelCmd}`);
exec(startTunnelCmd, (error, stdout, stderr) => {
if (error) {
log.error(`Error starting go-ios tunnel: ${error.message}`);
return;
}
if (stdout) {
log.info(`go-ios tunnel stdout: ${stdout}`);
}
if (stderr) {
log.warn(`go-ios tunnel stderr: ${stderr}`);
}
log.info('go-ios tunnel established successfully');
});
} catch (err) {
log.error(`Failed to establish go-ios tunnel: ${err}`);
throw err;
}
}