Catch errors when querying physical devices

Summary:
This is our #1 error and always means there's something off with
the user's setup.

Reviewed By: mweststrate

Differential Revision: D26370974

fbshipit-source-id: 5e2644ce9de8a7d28614a22fab5ce54dab0561d3
This commit is contained in:
Pascal Hartig
2021-02-11 05:06:28 -08:00
committed by Facebook GitHub Bot
parent 92f3ab8ff4
commit eff1ce5ba7

View File

@@ -64,37 +64,47 @@ async function targets(idbPath: string): Promise<Array<DeviceTarget>> {
// But idb is MUCH more CPU efficient than instruments, so // But idb is MUCH more CPU efficient than instruments, so
// when installed, use it. // when installed, use it.
if (await memoize(isAvailable)(idbPath)) { if (await memoize(isAvailable)(idbPath)) {
return safeExec(`${idbPath} list-targets --json`).then(({stdout}) => return safeExec(`${idbPath} list-targets --json`)
// It is safe to assume this to be non-null as it only turns null .then(({stdout}) =>
// if the output redirection is misconfigured: // It is safe to assume this to be non-null as it only turns null
// https://stackoverflow.com/questions/27786228/node-child-process-spawn-stdout-returning-as-null // if the output redirection is misconfigured:
stdout! // https://stackoverflow.com/questions/27786228/node-child-process-spawn-stdout-returning-as-null
.toString() stdout!
.trim() .toString()
.split('\n') .trim()
.map((line) => line.trim()) .split('\n')
.filter(Boolean) .map((line) => line.trim())
.map((line) => JSON.parse(line)) .filter(Boolean)
.filter(({type}: IdbTarget) => type !== 'simulator') .map((line) => JSON.parse(line))
.map((target: IdbTarget) => { .filter(({type}: IdbTarget) => type !== 'simulator')
return {udid: target.udid, type: 'physical', name: target.name}; .map<DeviceTarget>((target: IdbTarget) => {
}), return {udid: target.udid, type: 'physical', name: target.name};
); }),
)
.catch((e: Error) => {
console.warn('Failed to query idb for targets:', e);
return [];
});
} else { } else {
await killOrphanedInstrumentsProcesses(); await killOrphanedInstrumentsProcesses();
return safeExec('instruments -s devices').then(({stdout}) => return safeExec('instruments -s devices')
stdout! .then(({stdout}) =>
.toString() stdout!
.split('\n') .toString()
.map((line) => line.trim()) .split('\n')
.filter(Boolean) .map((line) => line.trim())
.map((line) => /(.+) \([^(]+\) \[(.*)\]( \(Simulator\))?/.exec(line)) .filter(Boolean)
.filter(notNull) .map((line) => /(.+) \([^(]+\) \[(.*)\]( \(Simulator\))?/.exec(line))
.filter(([_match, _name, _udid, isSim]) => !isSim) .filter(notNull)
.map(([_match, name, udid]) => { .filter(([_match, _name, _udid, isSim]) => !isSim)
return {udid: udid, type: 'physical', name: name}; .map<DeviceTarget>(([_match, name, udid]) => {
}), return {udid, type: 'physical', name};
); }),
)
.catch((e) => {
console.warn('Failed to query instruments:', e);
return [];
});
} }
} }