Extract execute and push to androidUtil

Summary: This commit doesn't change anything, just moves the android command running stuff into its own utility.

Reviewed By: passy

Differential Revision: D15393476

fbshipit-source-id: de93bbd88fa62bddff8d9ea56cfbc33bfd854d53
This commit is contained in:
John Knox
2019-05-17 09:41:36 -07:00
committed by Facebook Github Bot
parent 841d5d57a2
commit d238a958ec
2 changed files with 72 additions and 64 deletions

View File

@@ -0,0 +1,61 @@
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import {getAdbClient} from './adbClient';
const adbkit = require('adbkit-fb');
const logTag = 'androidContainerUtility';
const appNotDebuggableRegex = /debuggable/;
const allowedAppNameRegex = /^[a-zA-Z0-9._\-]+$/;
const operationNotPermittedRegex = /not permitted/;
const adb = getAdbClient();
export function executeCommandAsApp(
deviceId: string,
app: string,
command: string,
): Promise<string> {
if (!app.match(allowedAppNameRegex)) {
return Promise.reject(new Error(`Disallowed run-as user: ${app}`));
}
if (command.match(/[']/)) {
return Promise.reject(new Error(`Disallowed escaping command: ${command}`));
}
return adb
.then(client =>
client.shell(deviceId, `echo '${command}' | run-as '${app}'`),
)
.then(adbkit.util.readAll)
.then(buffer => buffer.toString())
.then(output => {
if (output.match(appNotDebuggableRegex)) {
throw new Error(
`Android app ${app} is not debuggable. To use it with Flipper, add android:debuggable="true" to the application section of AndroidManifest.xml`,
);
}
if (output.toLowerCase().match(operationNotPermittedRegex)) {
throw new Error(
`Your android device (${deviceId}) does not support the adb shell run-as command. We're tracking this at https://github.com/facebook/flipper/issues/92`,
);
}
return output;
});
}
export function push(
deviceId: string,
app: string,
filename: string,
contents: string,
): Promise<void> {
console.debug(`Deploying ${filename} to ${deviceId}:${app}`, logTag);
return executeCommandAsApp(
deviceId,
app,
`echo "${contents}" > ${filename} && chmod 600 ${filename}`,
).then(output => undefined);
}