@@ -2,7 +2,7 @@ const { spawn, execSync } = require("child_process");
22const { appLog, appLogError, exitProcess } = require ( './stdio' ) ;
33const chalk = require ( "chalk" ) ;
44
5- function checkAdbDevice ( ) {
5+ function checkAdbDevice ( serial ) {
66 const outputText = execSync ( "adb devices" , { encoding : "utf8" } ) ;
77 const outputLines = outputText . trim ( ) . split ( "\n" ) ;
88 appLog ( `\n$ adb devices\n${ outputText . trim ( ) } \n ` ) ;
@@ -19,7 +19,15 @@ function checkAdbDevice() {
1919 exitProcess ( 1 ) ;
2020 }
2121
22- if ( process . env . ANDROID_SERIAL ) {
22+ if ( serial ) {
23+ const exist = validDevices . find ( ( device ) => serial === device . split ( '\t' ) [ 0 ] ) ;
24+ if ( ! exist ) {
25+ appLog ( chalk . red
26+ ( `The device with serial ${ serial } not connected.\n` )
27+ ) ;
28+ exitProcess ( 1 ) ;
29+ }
30+ } else if ( process . env . ANDROID_SERIAL ) {
2331 appLog ( `Environment Variable ${ chalk . yellow ( 'ANDROID_SERIAL' ) } : ${ process . env . ANDROID_SERIAL } ` ) ;
2432 const exist = validDevices . find ( ( device ) => process . env . ANDROID_SERIAL === device . split ( '\t' ) [ 0 ] ) ;
2533 if ( ! exist ) {
@@ -32,19 +40,22 @@ Please check the device serial or unset the ${chalk.yellow("ANDROID_SERIAL")} en
3240 } else if ( devices . length > 1 ) {
3341 appLog (
3442 `Multiple adb devices detected, will use the first device.\n` +
35- `You can also set the ${ chalk . yellow ( "ANDROID_SERIAL" ) } env to specify device`
43+ `You can also use --serial <SERIAL> to specify device`
3644 ) ;
3745 const [ firstDevice ] = validDevices [ 0 ] . split ( "\t" ) ;
3846 appLog ( chalk . green ( `Using adb device: ${ firstDevice } ` ) ) ;
3947 process . env . ANDROID_SERIAL = firstDevice ;
4048 }
4149}
4250
43- function listenAdbLogCat ( onLogCallback ) {
51+ function listenAdbLogCat ( params ) {
52+ const { onLog, serial } = params ;
53+
4454 // clear logcat buffer
45- execSync ( " adb logcat -c" ) ;
55+ execSync ( ` adb${ serial ? ` -s ${ serial } ` : '' } logcat -c` ) ;
4656
47- const adbProcess = spawn ( "adb" , [ "logcat" ] ) ;
57+ const extraArgs = serial ? [ '-s' , serial ] : [ ]
58+ const adbProcess = spawn ( "adb" , [ ...extraArgs , 'logcat' ] ) ;
4859
4960 let leftover = "" ;
5061 adbProcess . stdout . on ( "data" , ( data ) => {
@@ -53,7 +64,7 @@ function listenAdbLogCat(onLogCallback) {
5364 const lines = fullChunk . split ( "\n" ) ;
5465 leftover = lines . pop ( ) ;
5566
56- lines . forEach ( onLogCallback ) ;
67+ lines . forEach ( onLog ) ;
5768 } ) ;
5869
5970 adbProcess . stderr . on ( "data" , ( data ) => {
0 commit comments