@@ -14,7 +14,6 @@ import { autoUpdater } from 'electron-updater';
1414import { Client , Device } from 'adb-ts' ;
1515import { IDevice } from 'adb-ts/lib/util' ;
1616import log from 'electron-log' ;
17- import { execFile } from 'child_process' ;
1817// @ts -ignore
1918import * as mDnsSd from 'node-dns-sd' ;
2019import { nanoid } from 'nanoid' ;
@@ -58,90 +57,36 @@ async function adbScreencap(device: IDevice): Promise<string> {
5857
5958async function adbRemoveForward ( ) : Promise < boolean > {
6059 if ( adb ) {
61- execFile (
62- path . join ( ADB_PATH , ADB_BIN ) ,
63- [ 'forward' , '--list' ] ,
64- ( err , stdout ) => {
65- if ( stdout ) {
66- const lines = stdout . trim ( ) . split ( '\n' ) ;
67- // eslint-disable-next-line no-restricted-syntax, guard-for-in
68- for ( const index in lines ) {
69- const device = lines [ index ] . split ( ' ' ) ;
70- execFile (
71- path . join ( ADB_PATH , ADB_BIN ) ,
72- [ '-s' , device [ 0 ] , 'forward' , '--remove-all' ] ,
73- ( err1 : any , stdout1 : string , stderr1 : any ) => {
74- if ( err1 ) {
75- console . log ( err1 ) ;
76- }
77- if ( stderr1 && ! stdout1 ) {
78- console . log (
79- stderr1 . trim ( ) ,
80- [ '-s' , device [ 0 ] , 'forward' , '--remove-all' ] . join ( ' ' ) ,
81- ) ;
82- }
83- if ( / E r r o r / . test ( stdout1 ) ) {
84- console . log (
85- stdout1 . trim ( ) ,
86- [ '-s' , device [ 0 ] , 'forward' , '--remove-all' ] . join ( ' ' ) ,
87- ) ;
88- }
89- console . log ( 'clearing forwards' , stdout1 ) ;
90- } ,
91- ) ;
92- }
93- }
94- console . log ( stdout . trim ( ) . split ( '\n' ) ) ;
95- } ,
96- ) ;
60+ const forwards = await adb . exec ( [ 'forward' , '--list' ] ) ;
61+ const devices = forwards . trim ( ) . split ( '\n' ) ;
62+ devices . forEach ( async ( line ) => {
63+ const device = line . split ( ' ' ) [ 0 ] ;
64+ if ( device . trim ( ) !== '' ) {
65+ const removed = await adb ?. exec ( [
66+ '-s' ,
67+ device ,
68+ 'forward' ,
69+ '--remove-all' ,
70+ ] ) ;
71+ console . log ( removed ) ;
72+ }
73+ } ) ;
74+ return true ;
9775 }
9876 return false ;
9977}
10078
10179async function adbStartService ( device : IDevice ) : Promise < boolean > {
10280 if ( adb ) {
103- execFile (
104- path . join ( ADB_PATH , ADB_BIN ) ,
105- [
106- '-s' ,
107- `${ device . id } ` ,
108- 'shell' ,
109- 'am' ,
110- 'start-foreground-service' ,
111- 'com.jwlilly.accessibilityinspector/.SocketService' ,
112- ] ,
113- ( err1 : any , stdout1 : string , stderr1 : any ) => {
114- if ( err1 ) {
115- console . log ( err1 ) ;
116- }
117- if ( stderr1 && ! stdout1 ) {
118- console . log (
119- stderr1 . trim ( ) ,
120- [
121- 'shell' ,
122- 'am' ,
123- 'start-foreground-service' ,
124- 'com.jwlilly.accessibilityinspector/.SocketService' ,
125- ] . join ( ' ' ) ,
126- ) ;
127- return true ;
128- }
129- if ( / E r r o r / . test ( stdout1 ) ) {
130- console . log (
131- stdout1 . trim ( ) ,
132- [
133- 'shell' ,
134- 'am' ,
135- 'start-foreground-service' ,
136- 'com.jwlilly.accessibilityinspector/.SocketService' ,
137- ] . join ( ' ' ) ,
138- ) ;
139- return false ;
140- }
141- console . log ( 'starting service' , stdout1 ) ;
142- return false ;
143- } ,
144- ) ;
81+ const startService = await adb . execDevice ( device . id , [
82+ 'shell' ,
83+ 'am' ,
84+ 'start-foreground-service' ,
85+ 'com.jwlilly.accessibilityinspector/.SocketService' ,
86+ ] ) ;
87+ if ( ! startService . toLowerCase ( ) . includes ( 'error' ) ) {
88+ return true ;
89+ }
14590 }
14691 return false ;
14792}
@@ -220,30 +165,12 @@ function getDevice(service: any) {
220165 } ;
221166}
222167
223- function connect ( { address, port } : any ) {
168+ async function connect ( { address, port } : any ) {
224169 if ( adb ) {
225- execFile (
226- path . join ( ADB_PATH , ADB_BIN ) ,
227- [ 'pair' , `${ address } :${ port } ` , password ] ,
228- ( err1 : any , stdout1 : string , stderr1 : any ) => {
229- if ( err1 ) {
230- console . log ( err1 ) ;
231- }
232- if ( stderr1 && ! stdout1 ) {
233- console . log (
234- stderr1 . trim ( ) ,
235- [ 'pair' , `${ address } :${ port } ` , password ] . join ( ' ' ) ,
236- ) ;
237- }
238- if ( / E r r o r / . test ( stdout1 ) ) {
239- console . log (
240- stdout1 . trim ( ) ,
241- [ 'pair' , `${ address } :${ port } ` , password ] . join ( ' ' ) ,
242- ) ;
243- }
244- console . log ( 'connected over network' , stdout1 ) ;
245- } ,
246- ) ;
170+ const pair = await adb . exec ( [ 'pair' , `${ address } :${ port } ` , password ] ) ;
171+ if ( ! pair . toLowerCase ( ) . includes ( 'error' ) ) {
172+ console . log ( 'connected over network' , pair ) ;
173+ }
247174 }
248175}
249176
@@ -256,18 +183,31 @@ function stopDiscover() {
256183
257184async function startDiscover ( ) {
258185 if ( continueDiscover ) {
259- const deviceList = await mDnsSd . discover ( {
260- name : '_adb-tls-pairing._tcp.local' ,
261- } ) ;
186+ const deviceList = await mDnsSd
187+ . discover ( {
188+ name : '_adb-tls-pairing._tcp.local' ,
189+ } )
190+ . catch ( ( e ) => {
191+ console . log ( e ) ;
192+ } ) ;
262193 if ( deviceList . length === 0 ) {
263194 return startDiscover ( ) ;
264195 }
265196 const item = getDevice ( deviceList [ 0 ] ) ;
266- connect ( item ) ;
197+ connect ( item ) . catch ( ( error ) => {
198+ console . log ( error ) ;
199+ } ) ;
267200 }
268201 return stopDiscover ( ) ;
269202}
270203
204+ async function adbInstallApp ( device : IDevice ) {
205+ if ( adb ) {
206+ const apk = path . join ( ADB_PATH , 'app-debug.apk' ) ;
207+ await adb . install ( device . id , apk ) ;
208+ }
209+ }
210+
271211ipcMain . on ( 'ipc-example' , async ( event , arg ) => {
272212 const msgTemplate = ( pingPong : string ) => `IPC test: ${ pingPong } ` ;
273213 console . log ( msgTemplate ( arg ) ) ;
@@ -278,6 +218,10 @@ ipcMain.handle('adb-list-devices', async () => {
278218 return adbListDevices ( ) ;
279219} ) ;
280220
221+ ipcMain . handle ( 'adb-install-app' , async ( _event , args ) => {
222+ return adbInstallApp ( args [ 0 ] ) ;
223+ } ) ;
224+
281225ipcMain . handle ( 'adb-screencap' , async ( _event , args ) => {
282226 return adbScreencap ( args [ 0 ] ) ;
283227} ) ;
@@ -299,7 +243,9 @@ ipcMain.handle('wifi-connect-start', async () => {
299243 wifiName = `ADB_WIFI_${ nanoid ( ) } ` ;
300244 password = nanoid ( ) ;
301245 continueDiscover = true ;
302- startDiscover ( ) ;
246+ startDiscover ( ) . catch ( ( error ) => {
247+ // console.log(error);
248+ } ) ;
303249 return `WIFI:T:ADB;S:${ wifiName } ;P:${ password } ;;` ;
304250} ) ;
305251
0 commit comments