diff --git a/tools/cordova/run-targets.js b/tools/cordova/run-targets.js index 3c5f01c60e..bd52c0af0a 100644 --- a/tools/cordova/run-targets.js +++ b/tools/cordova/run-targets.js @@ -101,5 +101,45 @@ export class AndroidRunTarget extends CordovaRunTarget { async start(cordovaProject) { await cordovaProject.run(this.platform, this.isDevice); + + this.tailLogs(cordovaProject).done(); + } + + async checkPlatformRequirementsAndSetEnv(cordovaProject) { + // Cordova Android is fairly good at applying various heuristics to find + // suitable values for JAVA_HOME and ANDROID_HOME, and to augment the PATH + // with those variables. + // Unfortunately, this is intertwined with checking requirements, so the + // only way to get access to this functionality is to run check_reqs and + // let it modify process.env + const check_reqs = require(files.pathJoin( + cordovaProject.projectRoot, 'platforms', this.platform, + 'cordova', 'lib', 'check_reqs')); + // We can't use check_reqs.run() because that will print the values of + // JAVA_HOME and ANDROID_HOME to stdout. + await Promise.all([check_reqs.check_java(), + check_reqs.check_android().then(check_reqs.check_android_target)]); + } + + async tailLogs(cordovaProject) { + cordovaProject.runCommands(`tailing logs for ${this.displayName}`, async () => { + await this.checkPlatformRequirementsAndSetEnv(cordovaProject); + + // XXX This only works if we have at most one device or one emulator + // connected. We should find a way to get the target ID from run and use + // it instead of -d or -e. + const target = this.isDevice ? "-d" : "-e"; + + // Clear logs + await execFileAsync('adb', [target, 'logcat', '-c']); + + const filterExpressions = ['CordovaLog:I', 'chromium:I', + 'SystemWebViewClient:I', '*:S']; + + // Asynchronously start tailing logs to stdout + execFileAsync('adb', [target, 'logcat', + ...filterExpressions], + { stdio: 'inherit' }); + }); } }