appium-adb
Version:
Android Debug Bridge interface
80 lines • 3.09 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getJavaForOs = exports.getJavaHome = void 0;
exports.getSdkRootFromEnv = getSdkRootFromEnv;
exports.requireSdkRoot = requireSdkRoot;
const support_1 = require("@appium/support");
const node_path_1 = __importDefault(require("node:path"));
/**
* Gets Android SDK root from environment variables.
*
* @returns SDK root path if present
*/
function getSdkRootFromEnv() {
return process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT;
}
/**
* Resolves and validates Android SDK root path.
*
* @param customRoot - Optional explicit SDK root path override
* @returns Absolute path to a valid SDK root directory
*/
async function requireSdkRoot(customRoot = null) {
const sdkRoot = customRoot || getSdkRootFromEnv();
const docMsg = 'Read https://developer.android.com/studio/command-line/variables for more details';
if (!sdkRoot || support_1.util.isEmpty(sdkRoot)) {
throw new Error(`Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported. ${docMsg}`);
}
if (!(await support_1.fs.exists(sdkRoot))) {
throw new Error(`The Android SDK root folder '${sdkRoot}' does not exist on the local file system. ${docMsg}`);
}
const stats = await support_1.fs.stat(sdkRoot);
if (!stats.isDirectory()) {
throw new Error(`The Android SDK root '${sdkRoot}' must be a folder. ${docMsg}`);
}
return sdkRoot;
}
exports.getJavaHome = support_1.util.memoize(async function getJavaHome() {
const result = process.env.JAVA_HOME;
if (!result) {
throw new Error('The JAVA_HOME environment variable is not set for the current process');
}
if (!(await support_1.fs.exists(result))) {
throw new Error(`The JAVA_HOME location '${result}' must exist`);
}
const stats = await support_1.fs.stat(result);
if (!stats.isDirectory()) {
throw new Error(`The JAVA_HOME location '${result}' must be a valid folder`);
}
return result;
});
exports.getJavaForOs = support_1.util.memoize(async function getJavaForOs() {
let javaHome;
let errMsg;
try {
javaHome = await (0, exports.getJavaHome)();
}
catch (err) {
const error = err;
errMsg = error.message;
}
const executableName = `java${support_1.system.isWindows() ? '.exe' : ''}`;
if (javaHome) {
const resultPath = node_path_1.default.resolve(javaHome, 'bin', executableName);
if (await support_1.fs.exists(resultPath)) {
return resultPath;
}
}
try {
return await support_1.fs.which(executableName);
}
catch {
// Ignore and throw custom error below
}
throw new Error(`The '${executableName}' binary could not be found ` +
`neither in PATH nor under JAVA_HOME (${javaHome ? node_path_1.default.resolve(javaHome, 'bin') : errMsg})`);
});
//# sourceMappingURL=sdk.js.map