@@ -23,37 +23,58 @@ export type AndroidAdbFailureClassification = Readonly<{
2323type AndroidAdbFailureMatcher = readonly [
2424 pattern : RegExp ,
2525 failure : AndroidAdbFailureClassification ,
26- matchStdout ?: true ,
26+ options ?: Readonly < {
27+ matchStdout ?: true ;
28+ /** Anchored shape of the host adb's first stderr line when it issues this refusal before the command leaves the host. */
29+ hostRefusal ?: RegExp ;
30+ } > ,
2731] ;
2832
33+ const HOST_REFUSAL_PREFIX = String . raw `(?:adb|error): ` ;
34+
35+ function hostRefusalHead ( body : string ) : RegExp {
36+ return new RegExp ( `^${ HOST_REFUSAL_PREFIX } ${ body } $` ) ;
37+ }
38+
2939const ANDROID_ADB_DEVICE_OFFLINE_FAILURE = {
3040 reason : 'device_offline' ,
3141 hint : 'The device is connected but offline — wait for it to finish booting or run adb reconnect, then retry.' ,
3242 retriable : true ,
3343} as const satisfies AndroidAdbFailureClassification ;
3444
35- const ANDROID_ADB_FAILURE_MATCHERS = [
45+ const ANDROID_ADB_FAILURE_MATCHERS : readonly AndroidAdbFailureMatcher [ ] = [
3646 [
3747 / d e v i c e u n a u t h o r i z e d | d e v i c e s t i l l a u t h o r i z i n g / ,
3848 {
3949 reason : 'device_unauthorized' ,
4050 hint : 'USB debugging is not authorized — accept the authorization prompt on the device screen (re-plug the cable if none appears), then retry.' ,
4151 } ,
52+ { hostRefusal : hostRefusalHead ( String . raw `device (?:unauthorized|still authorizing)\.?` ) } ,
53+ ] ,
54+ [
55+ / d e v i c e o f f l i n e / ,
56+ ANDROID_ADB_DEVICE_OFFLINE_FAILURE ,
57+ { hostRefusal : hostRefusalHead ( String . raw `device (?:'[^']*' )?offline` ) } ,
4258 ] ,
43- [ / d e v i c e o f f l i n e / , ANDROID_ADB_DEVICE_OFFLINE_FAILURE ] ,
4459 [
4560 / m o r e t h a n o n e (?: d e v i c e \/ e m u l a t o r | d e v i c e a n d e m u l a t o r ) / ,
4661 {
4762 reason : 'multiple_devices' ,
4863 hint : 'Multiple Android devices are connected — pass --serial <serial> (see adb devices) to select one.' ,
4964 } ,
65+ {
66+ hostRefusal : hostRefusalHead (
67+ String . raw `more than one (?:device/emulator|device and emulator)` ,
68+ ) ,
69+ } ,
5070 ] ,
5171 [
5272 / n o d e v i c e s \/ e m u l a t o r s f o u n d | n o d e v i c e s f o u n d / ,
5373 {
5474 reason : 'no_devices' ,
5575 hint : 'No Android devices detected — boot an emulator or connect a device and verify it appears in adb devices.' ,
5676 } ,
77+ { hostRefusal : hostRefusalHead ( String . raw `no devices(?:/emulators)? found` ) } ,
5778 ] ,
5879 [
5980 / d e v i c e (?: ' [ ^ ' ] * ' ) ? n o t f o u n d / ,
@@ -62,6 +83,7 @@ const ANDROID_ADB_FAILURE_MATCHERS = [
6283 hint : 'The device disconnected or is restarting — verify it is listed in adb devices, then retry.' ,
6384 retriable : true ,
6485 } ,
86+ { hostRefusal : hostRefusalHead ( String . raw `device (?:'[^']*' )?not found` ) } ,
6587 ] ,
6688 [
6789 / a d b s e r v e r v e r s i o n \( \d + \) d o e s n ' t m a t c h t h i s c l i e n t / ,
@@ -85,33 +107,33 @@ const ANDROID_ADB_FAILURE_MATCHERS = [
85107 reason : 'install_insufficient_storage' ,
86108 hint : 'The device is out of storage — free up space or uninstall unused apps, then retry the install.' ,
87109 } ,
88- true ,
110+ { matchStdout : true } ,
89111 ] ,
90112 [
91113 / i n s t a l l _ f a i l e d _ u p d a t e _ i n c o m p a t i b l e / ,
92114 {
93115 reason : 'install_update_incompatible' ,
94116 hint : 'The installed app has an incompatible signature — uninstall the existing app first, then retry the install.' ,
95117 } ,
96- true ,
118+ { matchStdout : true } ,
97119 ] ,
98120 [
99121 / i n s t a l l _ f a i l e d _ v e r s i o n _ d o w n g r a d e / ,
100122 {
101123 reason : 'install_version_downgrade' ,
102124 hint : 'The APK is older than the installed app — uninstall the app first (or install with downgrade allowed), then retry.' ,
103125 } ,
104- true ,
126+ { matchStdout : true } ,
105127 ] ,
106128 [
107129 / i n s t a l l _ f a i l e d _ \w + | i n s t a l l _ p a r s e _ f a i l e d _ \w + / ,
108130 {
109131 reason : 'install_failed' ,
110132 hint : 'The Android package installer rejected the APK — see the INSTALL_FAILED code in the error output for the exact cause.' ,
111133 } ,
112- true ,
134+ { matchStdout : true } ,
113135 ] ,
114- ] as const satisfies readonly AndroidAdbFailureMatcher [ ] ;
136+ ] ;
115137
116138const ANDROID_ADB_TIMEOUT_FAILURE : AndroidAdbFailureClassification = Object . freeze ( {
117139 reason : 'timeout' ,
@@ -121,34 +143,10 @@ const ANDROID_ADB_TIMEOUT_FAILURE: AndroidAdbFailureClassification = Object.free
121143const ANDROID_HELPER_INSTALL_TIMEOUT_HINT =
122144 'The helper install timed out — some OEM builds (ColorOS is one) hold the first install of each package behind a system install-confirmation dialog and adb waits for it. Check the device screen, confirm any pending install prompt, then retry; if none is showing, run adb kill-server && adb start-server and retry.' ;
123145
124- type HostRefusalShape = readonly [ head : RegExp , reason : AndroidAdbFailureReason ] ;
125-
126- const HOST_REFUSAL_PREFIX = String . raw `(?:adb|error): ` ;
127-
128- /** First stderr line, lowercased, of each refusal the host adb issues before the command leaves the host. */
129- const HOST_REFUSAL_SHAPES : readonly HostRefusalShape [ ] = [
130- [
131- new RegExp ( `^${ HOST_REFUSAL_PREFIX } device (?:unauthorized|still authorizing)\\.?$` ) ,
132- 'device_unauthorized' ,
133- ] ,
134- [ new RegExp ( `^${ HOST_REFUSAL_PREFIX } device (?:'[^']*' )?offline$` ) , 'device_offline' ] ,
135- [
136- new RegExp ( `^${ HOST_REFUSAL_PREFIX } more than one (?:device/emulator|device and emulator)$` ) ,
137- 'multiple_devices' ,
138- ] ,
139- [ new RegExp ( `^${ HOST_REFUSAL_PREFIX } no devices(?:/emulators)? found$` ) , 'no_devices' ] ,
140- [ new RegExp ( `^${ HOST_REFUSAL_PREFIX } device (?:'[^']*' )?not found$` ) , 'device_not_found' ] ,
141- ] ;
142-
143146/** The fixed trailer lines adb appends to a device_unauthorized refusal. */
144147const HOST_REFUSAL_TRAILER =
145148 / ^ (?: t h i s a d b s e r v e r ' s \$ a d b _ v e n d o r _ k e y s i s n o t s e t | t r y ' a d b k i l l - s e r v e r ' i f t h a t s e e m s w r o n g \. | (?: o t h e r w i s e | p l e a s e ) c h e c k (?: f o r | t h e ) (?: a ) ? c o n f i r m a t i o n d i a l o g o n y o u r d e v i c e \. ) $ / ;
146149
147- const ANDROID_ADB_FAILURES_BY_REASON = new Map <
148- AndroidAdbFailureReason ,
149- AndroidAdbFailureClassification
150- > ( ANDROID_ADB_FAILURE_MATCHERS . map ( ( [ , failure ] ) => [ failure . reason , failure ] ) ) ;
151-
152150function classifyHostRefusal (
153151 stderrText : string ,
154152 stdoutText : string ,
@@ -161,9 +159,10 @@ function classifyHostRefusal(
161159 if ( head === undefined || ! trailer . every ( ( line ) => HOST_REFUSAL_TRAILER . test ( line ) ) ) {
162160 return undefined ;
163161 }
164- const reason = HOST_REFUSAL_SHAPES . find ( ( [ shape ] ) => shape . test ( head ) ) ?. [ 1 ] ;
165- const failure = reason && ANDROID_ADB_FAILURES_BY_REASON . get ( reason ) ;
166- return failure && { ...failure , hostRefusal : true } ;
162+ const row = ANDROID_ADB_FAILURE_MATCHERS . find ( ( [ , , options ] ) =>
163+ options ?. hostRefusal ?. test ( head ) ,
164+ ) ;
165+ return row && { ...row [ 1 ] , hostRefusal : true } ;
167166}
168167
169168export function classifyAndroidAdbFailure (
@@ -174,8 +173,8 @@ export function classifyAndroidAdbFailure(
174173 const stdoutText = stdout . toLowerCase ( ) ;
175174 const refusal = classifyHostRefusal ( stderrText , stdoutText ) ;
176175 if ( refusal ) return refusal ;
177- for ( const [ pattern , classification , matchStdout ] of ANDROID_ADB_FAILURE_MATCHERS ) {
178- if ( pattern . test ( stderrText ) || ( matchStdout && pattern . test ( stdoutText ) ) ) {
176+ for ( const [ pattern , classification , options ] of ANDROID_ADB_FAILURE_MATCHERS ) {
177+ if ( pattern . test ( stderrText ) || ( options ?. matchStdout && pattern . test ( stdoutText ) ) ) {
179178 return classification ;
180179 }
181180 }
0 commit comments