Skip to content

Commit 5bd7a66

Browse files
authored
Extend original getPackages method, keep compatibility (#61)
* Extend original `getPackages` method, keep compatibility Signed-off-by: Sergey Volkov <[email protected]> * Rename local single character variables Signed-off-by: Sergey Volkov <[email protected]>
1 parent 537a7b3 commit 5bd7a66

File tree

2 files changed

+52
-60
lines changed

2 files changed

+52
-60
lines changed

README.md

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -396,18 +396,7 @@ Retrieves the features of the device identified by the given serial number. This
396396
* Returns: `Promise`
397397
* Resolves with: `features` (see callback)
398398

399-
#### client.getPackages(serial[, callback])
400-
401-
Retrieves the list of packages present on the device. This is analogous to `adb shell pm list packages`. If you just want to see if something's installed, consider using `client.isInstalled()` instead.
402-
403-
* **serial** The serial number of the device. Corresponds to the device ID in `client.listDevices()`.
404-
* **callback(err, packages)** Optional. Use this or the returned `Promise`.
405-
- **err** `null` when successful, `Error` otherwise.
406-
- **packages** An array of package names.
407-
* Returns: `Promise`
408-
* Resolves with: `packages` (see callback)
409-
410-
#### client.getPackagesWithFlags(serial, flags[, callback])
399+
#### client.getPackages(serial[, flags]&#91;, callback])
411400

412401
Retrieves the list of packages present on the device. This is analogous to `adb shell pm list packages`. If you just want to see if something's installed, consider using `client.isInstalled()` instead.
413402

@@ -812,7 +801,7 @@ client.listDevices()
812801
.then(function(devices) {
813802
return Promise.map(devices, function(device) {
814803
return client.shell(device.id, 'logcat') // logcat just for illustration,
815-
// prefer client.openLogcat in real use
804+
// prefer client.openLogcat in real use
816805
.then(function(conn) {
817806
var line = 0
818807
conn.on('data', function(data) {
@@ -821,8 +810,8 @@ client.listDevices()
821810
line += 1
822811
// close the stream and the running process
823812
// on the device will be gone, gracefully
824-
           if (line > 100) conn.end()
825-
         });
813+
if (line > 100) conn.end()
814+
})
826815
conn.on('close', function() {
827816
// here `ps` on the device shows the logcat process is gone
828817
console.log('100 lines read already, bye')

lib/adb/client.ts

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,24 @@ class Client extends EventEmitter {
112112
}
113113

114114
public connect(host: string, port: number | typeof callback = 5555, callback?: Callback<string>): Bluebird<string> {
115-
let p: number;
115+
let _port: number;
116116
if (typeof port === 'function') {
117117
callback = port;
118-
p = 5555;
118+
_port = 5555;
119119
} else {
120-
p = port;
120+
_port = port;
121121
}
122122
if (host.indexOf(':') !== -1) {
123123
const [h, portString] = host.split(':', 2);
124124
host = h;
125125
const parsed = parseInt(portString, 10);
126126
if (!isNaN(parsed)) {
127-
p = parsed;
127+
_port = parsed;
128128
}
129129
}
130130
return this.connection()
131131
.then(function (conn) {
132-
return new HostConnectCommand(conn).execute(host, p);
132+
return new HostConnectCommand(conn).execute(host, _port);
133133
})
134134
.nodeify(callback);
135135
}
@@ -139,24 +139,24 @@ class Client extends EventEmitter {
139139
port: number | typeof callback = 5555,
140140
callback?: Callback<string>,
141141
): Bluebird<string> {
142-
let p: number;
142+
let _port: number;
143143
if (typeof port === 'function') {
144144
callback = port;
145-
p = 5555;
145+
_port = 5555;
146146
} else {
147-
p = port;
147+
_port = port;
148148
}
149149
if (host.indexOf(':') !== -1) {
150-
const [h, portString] = host.split(':', 2);
151-
host = h;
150+
const [_host, portString] = host.split(':', 2);
151+
host = _host;
152152
const parsed = parseInt(portString, 10);
153153
if (!isNaN(parsed)) {
154-
p = parsed;
154+
_port = parsed;
155155
}
156156
}
157157
return this.connection()
158158
.then(function (conn) {
159-
return new HostDisconnectCommand(conn).execute(host, p);
159+
return new HostDisconnectCommand(conn).execute(host, _port);
160160
})
161161
.nodeify(callback);
162162
}
@@ -233,18 +233,21 @@ class Client extends EventEmitter {
233233
.nodeify(callback);
234234
}
235235

236-
public getPackages(serial: string, callback?: Callback<string[]>): Bluebird<string[]> {
237-
return this.transport(serial)
238-
.then(function (transport) {
239-
return new GetPackagesCommand(transport).execute(null);
240-
})
241-
.nodeify(callback);
242-
}
243-
244-
public getPackagesWithFlags(serial: string, flags: string, callback?: Callback<string[]>): Bluebird<string[]> {
236+
public getPackages(
237+
serial: string,
238+
flags: string | typeof callback,
239+
callback?: Callback<string[]>,
240+
): Bluebird<string[]> {
241+
let _flags: string;
242+
if (typeof flags === 'function') {
243+
callback = flags;
244+
_flags = null;
245+
} else {
246+
_flags = flags;
247+
}
245248
return this.transport(serial)
246249
.then(function (transport) {
247-
return new GetPackagesCommand(transport).execute(flags);
250+
return new GetPackagesCommand(transport).execute(_flags);
248251
})
249252
.nodeify(callback);
250253
}
@@ -356,16 +359,16 @@ class Client extends EventEmitter {
356359
format?: string | typeof callback,
357360
callback?: Callback<FramebufferStreamWithMeta>,
358361
): Bluebird<FramebufferStreamWithMeta> {
359-
let f: string;
362+
let _format: string;
360363
if (typeof format === 'function') {
361364
callback = format;
362-
f = 'raw';
365+
_format = 'raw';
363366
} else {
364-
f = format;
367+
_format = format;
365368
}
366369
return this.transport(serial)
367370
.then(function (transport) {
368-
return new FrameBufferCommand(transport).execute(f);
371+
return new FrameBufferCommand(transport).execute(_format);
369372
})
370373
.nodeify(callback);
371374
}
@@ -403,15 +406,15 @@ class Client extends EventEmitter {
403406
host?: string | typeof callback,
404407
callback?: Callback<Duplex>,
405408
): Bluebird<Duplex> {
406-
let h: string | undefined;
409+
let _host: string | undefined;
407410
if (typeof host === 'function') {
408411
callback = host;
409412
} else {
410-
h = host;
413+
_host = host;
411414
}
412415
return this.transport(serial)
413416
.then(function (transport) {
414-
return new TcpCommand(transport).execute(port, h);
417+
return new TcpCommand(transport).execute(port, _host);
415418
})
416419
.nodeify(callback);
417420
}
@@ -421,15 +424,15 @@ class Client extends EventEmitter {
421424
port: number | typeof callback = 1080,
422425
callback?: Callback<Duplex>,
423426
): Bluebird<Duplex> {
424-
let p: number;
427+
let _port: number;
425428
if (typeof port === 'function') {
426429
callback = port;
427-
p = 1080;
430+
_port = 1080;
428431
} else {
429-
p = port;
432+
_port = port;
430433
}
431434
const tryConnect = (times) => {
432-
return this.openTcp(serial, p)
435+
return this.openTcp(serial, _port)
433436
.then(function (stream) {
434437
return Monkey.connectStream(stream);
435438
})
@@ -448,7 +451,7 @@ class Client extends EventEmitter {
448451
.catch(() => {
449452
return this.transport(serial)
450453
.then(function (transport) {
451-
return new MonkeyCommand(transport).execute(p);
454+
return new MonkeyCommand(transport).execute(_port);
452455
})
453456
.then(function (out) {
454457
return tryConnect(20).then(function (monkey) {
@@ -466,16 +469,16 @@ class Client extends EventEmitter {
466469
options?: { clear?: boolean } | typeof callback,
467470
callback?: Callback<Logcat>,
468471
): Bluebird<Logcat> {
469-
let opts: { clear?: boolean };
472+
let _options: { clear?: boolean };
470473
if (typeof options === 'function') {
471474
callback = options;
472-
opts = {};
475+
_options = {};
473476
} else {
474-
opts = options;
477+
_options = options;
475478
}
476479
return this.transport(serial)
477480
.then(function (transport) {
478-
return new LogcatCommand(transport).execute(opts);
481+
return new LogcatCommand(transport).execute(_options);
479482
})
480483
.then(function (stream) {
481484
return Logcat.readStream(stream, {
@@ -635,32 +638,32 @@ class Client extends EventEmitter {
635638
mode?: number | typeof callback,
636639
callback?: Callback<PushTransfer>,
637640
): Bluebird<PushTransfer> {
638-
let m: number | undefined;
641+
let _mode: number | undefined;
639642
if (typeof mode === 'function') {
640643
callback = mode;
641644
} else {
642-
m = mode;
645+
_mode = mode;
643646
}
644647
return this.syncService(serial)
645648
.then(function (sync) {
646-
return sync.push(contents, path, m).on('end', function () {
649+
return sync.push(contents, path, _mode).on('end', function () {
647650
return sync.end();
648651
});
649652
})
650653
.nodeify(callback);
651654
}
652655

653656
public tcpip(serial: string, port: number | typeof callback = 5555, callback?: Callback<number>): Bluebird<number> {
654-
let p: number;
657+
let _port: number;
655658
if (typeof port === 'function') {
656659
callback = port;
657-
p = 5555;
660+
_port = 5555;
658661
} else {
659-
p = port;
662+
_port = port;
660663
}
661664
return this.transport(serial)
662665
.then(function (transport) {
663-
return new TcpIpCommand(transport).execute(p);
666+
return new TcpIpCommand(transport).execute(_port);
664667
})
665668
.nodeify(callback);
666669
}

0 commit comments

Comments
 (0)