* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { execCommand } from './commandExecutor';
* Utility methods for managing devices and processes
*/
export class DeviceUtil {
* Gets the PID of a process on the device by process name
*
* @param deviceId Device identifier
* @param processName Process name
* @returns Process ID
*/
static async getPidByProcessName(deviceId: string, processName: string): Promise<number | null> {
let cmd = `xcrun devicectl device info processes --device ${deviceId} | grep "/${processName}.app/${processName}" | awk '{print $1}'`;
try {
let execResult = await execCommand(cmd);
if (!execResult) {
return null;
}
let pid = parseInt(execResult.output.split('\n')[0], 10);
return isNaN(pid) ? null : pid;
} catch {
return null;
}
}
* Gets the PID of a process on the device by bundle identifier
*
* @param deviceId Device identifier
* @param bundleId Application bundle identifier
* @returns Process ID
*/
static async getPidByBundleId(deviceId: string, bundleId: string): Promise<number | null> {
try {
let cmd = `xcrun devicectl device info apps --device ${deviceId} 2>/dev/null | awk 'tolower($2)==tolower("${bundleId}"){print $1}'`;
let execResult = await execCommand(cmd);
if (!execResult) {
return null;
}
return await this.getPidByProcessName(deviceId, execResult.output.trim());
} catch {
return null;
}
}
}