Small refactors
Summary: Going to make more changes here soon and wanted to apply some small changes first. Reviewed By: jknoxville Differential Revision: D26078645 fbshipit-source-id: 3a2bcd593b893160b5a332c858a514ebe89d3f4d
This commit is contained in:
committed by
Facebook GitHub Bot
parent
bf46367557
commit
5ac8f7a16f
@@ -7,10 +7,8 @@
|
|||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import child_process from 'child_process';
|
|
||||||
import {promisify} from 'util';
|
|
||||||
import {Mutex} from 'async-mutex';
|
import {Mutex} from 'async-mutex';
|
||||||
const unsafeExec = promisify(child_process.exec);
|
import {exec as unsafeExec, Output} from 'promisify-child-process';
|
||||||
import {killOrphanedInstrumentsProcesses} from './processCleanup';
|
import {killOrphanedInstrumentsProcesses} from './processCleanup';
|
||||||
import {reportPlatformFailures} from './metrics';
|
import {reportPlatformFailures} from './metrics';
|
||||||
import {promises, constants} from 'fs';
|
import {promises, constants} from 'fs';
|
||||||
@@ -48,10 +46,12 @@ function isAvailable(idbPath: string): Promise<boolean> {
|
|||||||
.catch((_) => false);
|
.catch((_) => false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function safeExec(command: string): Promise<{stdout: string; stderr: string}> {
|
function safeExec(
|
||||||
return mutex.acquire().then((release) => {
|
command: string,
|
||||||
return unsafeExec(command).finally(release);
|
): Promise<{stdout: string; stderr: string} | Output> {
|
||||||
});
|
return mutex
|
||||||
|
.acquire()
|
||||||
|
.then((release) => unsafeExec(command).finally(release));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function targets(idbPath: string): Promise<Array<DeviceTarget>> {
|
async function targets(idbPath: string): Promise<Array<DeviceTarget>> {
|
||||||
@@ -65,7 +65,10 @@ async function targets(idbPath: string): Promise<Array<DeviceTarget>> {
|
|||||||
// 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`).then(({stdout}) =>
|
||||||
stdout
|
// It is safe to assume this to be non-null as it only turns null
|
||||||
|
// if the output redirection is misconfigured:
|
||||||
|
// https://stackoverflow.com/questions/27786228/node-child-process-spawn-stdout-returning-as-null
|
||||||
|
stdout!
|
||||||
.toString()
|
.toString()
|
||||||
.trim()
|
.trim()
|
||||||
.split('\n')
|
.split('\n')
|
||||||
@@ -80,7 +83,7 @@ async function targets(idbPath: string): Promise<Array<DeviceTarget>> {
|
|||||||
} else {
|
} else {
|
||||||
await killOrphanedInstrumentsProcesses();
|
await killOrphanedInstrumentsProcesses();
|
||||||
return safeExec('instruments -s devices').then(({stdout}) =>
|
return safeExec('instruments -s devices').then(({stdout}) =>
|
||||||
stdout
|
stdout!
|
||||||
.toString()
|
.toString()
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.map((line) => line.trim())
|
.map((line) => line.trim())
|
||||||
@@ -173,8 +176,8 @@ function wrapWithErrorMessage<T>(p: Promise<T>): Promise<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
isAvailable: isAvailable,
|
isAvailable,
|
||||||
targets: targets,
|
targets,
|
||||||
push: push,
|
push,
|
||||||
pull: pull,
|
pull,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user