nativescript
Version:
Command-line interface for building NativeScript projects
148 lines • 6.52 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AndroidDebugBridge = void 0;
const path = require("path");
const decorators_1 = require("../../decorators");
const os_1 = require("os");
const helpers_1 = require("../../helpers");
const yok_1 = require("../../yok");
class AndroidDebugBridge {
constructor($childProcess, $errors, $logger, $staticConfig, $androidDebugBridgeResultHandler) {
this.$childProcess = $childProcess;
this.$errors = $errors;
this.$logger = $logger;
this.$staticConfig = $staticConfig;
this.$androidDebugBridgeResultHandler = $androidDebugBridgeResultHandler;
this.adbFilePath = null;
}
async init() {
this.adbFilePath = await this.$staticConfig.getAdbFilePath();
}
async executeCommand(args, options) {
let event = "close";
const deviceIdentifier = options && options.deviceIdentifier;
const command = await this.composeCommand(args, deviceIdentifier);
let treatErrorsAsWarnings = false;
let childProcessOptions = undefined;
if (options) {
event = options.fromEvent || event;
treatErrorsAsWarnings = options.treatErrorsAsWarnings;
childProcessOptions = options.childProcessOptions;
if (options.returnChildProcess) {
return this.$childProcess.spawn(command.command, command.args);
}
}
// If adb -s <invalid device id> install <smth> is executed the childProcess won't get any response
// because the adb will be waiting for valid device and will not send close or exit event.
// For example `adb -s <invalid device id> install <smth>` throws error 'error: device \'030939f508e6c773\' not found\r\n' exitCode 4294967295
const result = await this.$childProcess.spawnFromEvent(command.command, command.args, event, childProcessOptions, { throwError: false });
const errors = this.$androidDebugBridgeResultHandler.checkForErrors(result);
if (errors && errors.length > 0) {
this.$androidDebugBridgeResultHandler.handleErrors(errors, treatErrorsAsWarnings);
}
// Some adb commands returns array of strings instead of object with stdout and stderr. (adb start-server)
return result.stdout === undefined || result.stdout === null
? result
: result.stdout;
}
getPropertyValue(deviceId, propertyName) {
return this.$childProcess.execFile(this.adbFilePath, [
"-s",
deviceId,
"shell",
"getprop",
propertyName,
]);
}
async getDevices() {
const result = await this.executeCommand(["devices"], {
returnChildProcess: true,
});
return new Promise((resolve, reject) => {
let adbData = "";
let errorData = "";
let isSettled = false;
result.stdout.on("data", (data) => {
adbData += data.toString();
});
result.stderr.on("data", (data) => {
errorData += (data || "").toString();
});
result.on("error", (error) => {
if (reject && !isSettled) {
isSettled = true;
reject(error);
}
});
result.on("close", async (exitCode) => {
if (errorData && !isSettled) {
isSettled = true;
reject(errorData);
return;
}
if (!isSettled) {
isSettled = true;
const adbDevices = adbData
.split(os_1.EOL)
.filter((line) => !!line &&
line.indexOf("List of devices attached") === -1 &&
line.indexOf("* daemon ") === -1 &&
line.indexOf("adb server") === -1);
resolve(adbDevices);
}
});
});
}
async getDevicesSafe() {
let adbDevices = [];
try {
adbDevices = await this.getDevices();
}
catch (err) {
this.$logger.trace(`Getting adb devices failed with error: ${err}`);
}
return adbDevices;
}
async composeCommand(params, identifier) {
const command = await this.$staticConfig.getAdbFilePath();
let deviceIdentifier = [];
if (identifier) {
deviceIdentifier = ["-s", `${identifier}`];
}
const args = deviceIdentifier.concat(params);
return { command, args };
}
async executeShellCommand(args, options) {
args.unshift("shell");
const result = await this.executeCommand(args, options);
return result;
}
async pushFile(localFilePath, deviceFilePath) {
const fileDirectory = (0, helpers_1.fromWindowsRelativePathToUnix)(path.dirname(deviceFilePath));
// starting from API level 28, the push command is returning an error if the directory does not exist
await this.executeShellCommand(["mkdir", "-p", fileDirectory]);
await this.executeCommand(["push", localFilePath, deviceFilePath]);
await this.executeShellCommand(["chmod", "0777", fileDirectory]);
}
async removeFile(deviceFilePath) {
await this.executeShellCommand(["rm", "-rf", deviceFilePath]);
}
}
exports.AndroidDebugBridge = AndroidDebugBridge;
__decorate([
(0, decorators_1.cache)()
], AndroidDebugBridge.prototype, "init", null);
__decorate([
(0, decorators_1.invokeInit)()
], AndroidDebugBridge.prototype, "getPropertyValue", null);
__decorate([
(0, decorators_1.invokeInit)()
], AndroidDebugBridge.prototype, "getDevices", null);
yok_1.injector.register("adb", AndroidDebugBridge);
//# sourceMappingURL=android-debug-bridge.js.map
;