@@ -144,16 +144,20 @@ export async function pushFile(
144
144
}
145
145
146
146
const runAsRootCommands = [
147
- [ 'su' , 'root' ] , // Used on official emulators
148
- [ 'su' , '-c' ] // Normal root
147
+ // 'su' as available on official emulators:
148
+ ( ...cmd : string [ ] ) => [ 'su' , 'root' , ...cmd ] ,
149
+ // Su on many physical rooted devices requires quotes:
150
+ ( ...cmd : string [ ] ) => [ 'su' , '-c' , `'${ cmd . join ( ' ' ) } '` ]
149
151
] ;
150
152
151
- export async function getRootCommand ( adbClient : Adb . DeviceClient ) : Promise < string [ ] | undefined > {
153
+ type RootCmd = ( ...cmd : string [ ] ) => string [ ] ;
154
+
155
+ export async function getRootCommand ( adbClient : Adb . DeviceClient ) : Promise < RootCmd | undefined > {
152
156
// Run whoami with each of the possible root commands
153
157
const rootCheckResults = await Promise . all (
154
- runAsRootCommands . map ( ( cmd ) =>
155
- run ( adbClient , cmd . concat ( 'whoami' ) , { timeout : 1000 } ) . catch ( console . log )
156
- . then ( ( whoami ) => ( { cmd, whoami } ) )
158
+ runAsRootCommands . map ( ( runAsRoot ) =>
159
+ run ( adbClient , runAsRoot ( 'whoami' ) , { timeout : 1000 } ) . catch ( console . log )
160
+ . then ( ( whoami ) => ( { cmd : runAsRoot , whoami } ) )
157
161
)
158
162
)
159
163
@@ -181,7 +185,7 @@ export async function getRootCommand(adbClient: Adb.DeviceClient): Promise<strin
181
185
} ) . catch ( console . log ) ;
182
186
183
187
return ( whoami || '' ) . trim ( ) === 'root'
184
- ? [ ] // all commands now run as root, so no prefix required.
188
+ ? ( ... cmd : string [ ] ) => cmd // All commands now run as root
185
189
: undefined ; // Still not root, no luck.
186
190
}
187
191
@@ -217,7 +221,7 @@ export async function hasCertInstalled(
217
221
218
222
export async function injectSystemCertificate (
219
223
adbClient : Adb . DeviceClient ,
220
- rootCmd : string [ ] ,
224
+ runAsRoot : RootCmd ,
221
225
certificatePath : string
222
226
) {
223
227
const injectionScriptPath = `${ ANDROID_TEMP } /htk-inject-system-cert.sh` ;
@@ -265,13 +269,13 @@ export async function injectSystemCertificate(
265
269
) ;
266
270
267
271
// Actually run the script that we just pushed above, as root
268
- const scriptOutput = await run ( adbClient , rootCmd . concat ( 'sh' , injectionScriptPath ) ) ;
272
+ const scriptOutput = await run ( adbClient , runAsRoot ( 'sh' , injectionScriptPath ) ) ;
269
273
console . log ( scriptOutput ) ;
270
274
}
271
275
272
276
export async function setChromeFlags (
273
277
adbClient : Adb . DeviceClient ,
274
- rootCmd : string [ ] ,
278
+ runAsRoot : RootCmd ,
275
279
flags : string [ ]
276
280
) {
277
281
const flagsFileContent = `chrome ${ flags . join ( ' ' ) } ` ;
@@ -313,11 +317,11 @@ export async function setChromeFlags(
313
317
) ;
314
318
315
319
// Actually run the script that we just pushed above, as root
316
- const scriptOutput = await run ( adbClient , rootCmd . concat ( 'sh' , chromeFlagsScriptPath ) ) ;
320
+ const scriptOutput = await run ( adbClient , runAsRoot ( 'sh' , chromeFlagsScriptPath ) ) ;
317
321
console . log ( scriptOutput ) ;
318
322
319
323
// Try to restart chrome, now that the flags have probably been changed:
320
- await run ( adbClient , rootCmd . concat ( 'am' , 'force-stop' , 'com.android.chrome' ) ) . catch ( ( ) => { } ) ;
324
+ await run ( adbClient , runAsRoot ( 'am' , 'force-stop' , 'com.android.chrome' ) ) . catch ( ( ) => { } ) ;
321
325
}
322
326
323
327
export async function bringToFront (
0 commit comments