first commit

This commit is contained in:
2026-09-08 20:28:54 +08:00
commit 2ada8d3d5a
37380 changed files with 4886169 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+104
View File
@@ -0,0 +1,104 @@
# adb-commander
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
provide adb(Android Debug Bridge) command functions
## Examples
### deviceList
```javascript
const adbCommander = require('adb-commander')
adbCommander.deviceList().then((deviceList, err) => {
if (err) {
console.error('fail to execute adb devices')
return
}
if (deviceList.length > 0) {
console.info(`devices is ${deviceList.join(',')}`)
}
})
```
### install and isInstalled
```javascript
const adbCommander = require('adb-commander')
adbCommander.install(apkPath)
.then(({ result, err }) => {
if (err) {
console.error('install failed')
}
adbCommander.isInstalled(deviceSn, 'org.hapjs.debugger').then(({isInstalled, err }) => {
if(isInstalled === true){
console.log('org.hapjs.debugger is installed')
}
})
})
)
```
### unInstall
```javascript
const adbCommander = require('adb-commander')
adbCommander.uninstall( deviceSn, 'org.hapjs.debugger')
.then(({ result, err }) => {
if (err) {
console.error('uninstall failed')
}
adbCommander.isInstalled(deviceSn, 'org.hapjs.debugger').then(({isInstalled, err }) => {
if(isInstalled === false){
console.log('org.hapjs.debugger is uninstalled')
}
})
})
)
```
### startActivity
#### params
- deviceSn `string`
- action `string` optional
- component `string` optional
- extra `[{key: string, type: string, value: <any>}, ...]` optional
```javascript
const adbCommander = require('adb-commander')
adbCommander
.startActivity(deviceSn, action, component, extra)
.then(({ result, err }) => {
if (err) {
console.error('startActivity failed')
return
}
console.log('start activity result', { result, err })
})
```
### exeCommand 执行 adb 命令
```javascript
const adbCommander = require('adb-commander')
adbCommander.exeCommand('adb devices').then(({ result, err }) => {
if (err) {
console.error("exeCommand 'adb devices' failed")
return
}
console.log('adb devices result', { result, err })
})
```
+62
View File
@@ -0,0 +1,62 @@
import Extra from './Extra';
declare class ADBCommander {
_commandFactory(command: string): Promise<{
err: any;
result?: undefined;
} | {
result: any;
err?: undefined;
}>;
deviceList(): Promise<{
deviceList: string[];
err: any;
}>;
reverse(deviceSN: string, localPort: string, remotePort: string): Promise<{
err: any;
result?: undefined;
} | {
result: any;
err?: undefined;
}>;
forward(deviceSN: string, localPort: string, remotePort: string): Promise<{
err: any;
result?: undefined;
} | {
result: any;
err?: undefined;
}>;
version(): Promise<{
version: any;
err: any;
}>;
print(cmd: string): Promise<void>;
uninstall(deviceSN: string, pkg: string): Promise<{
result: any;
err: any;
}>;
install(deviceSN: string, apkPath: string): Promise<{
result: any;
err: any;
}>;
isInstalled(deviceSN: string, pkg: string): Promise<{
isInstalled: boolean;
err: any;
}>;
startActivity(deviceSN: string, action?: string, component?: string, extra?: Extra[]): Promise<{
result: any;
err: any;
}>;
exeCommand(command: string): Promise<{
err: any;
result?: undefined;
} | {
result: any;
err?: undefined;
}>;
getProp(deviceSN: string): Promise<{
result: any;
err: any;
}>;
}
declare const adbCommander: ADBCommander;
export = adbCommander;
+123
View File
@@ -0,0 +1,123 @@
"use strict";
const adb_driver_1 = require("adb-driver");
class ADBCommander {
async _commandFactory(command) {
const cmdResult = await adb_driver_1.execADBCommand(command);
const isError = cmdResult instanceof Error || (cmdResult.stack && cmdResult.message);
if (isError) {
return { err: cmdResult };
}
else {
return { result: cmdResult };
}
}
async deviceList() {
const { result, err } = await this._commandFactory(`adb devices`);
return {
deviceList: _parseDeviceInfo(result),
err,
};
function _parseDeviceInfo(stdout) {
if (!stdout) {
return [];
}
const lines = stdout.replace(/(\n|\r\n){1,}/g, '\n').split('\n');
const result = lines
.filter((item, idx) => {
const oneDevice = item.split('\t');
return idx !== 0 && oneDevice[1] === 'device';
})
.map(item => {
return item.split('\t')[0];
});
return result;
}
}
async reverse(deviceSN, localPort, remotePort) {
const cmd = `adb -s ${deviceSN} reverse tcp:${remotePort} tcp:${localPort}`;
return await this._commandFactory(cmd);
}
async forward(deviceSN, localPort, remotePort) {
const cmd = `adb -s ${deviceSN} forward tcp:${localPort} tcp:${remotePort}`;
return await this._commandFactory(cmd);
}
async version() {
const { result, err } = await this._commandFactory(`adb version`);
return { version: result, err };
}
async print(cmd) {
const { err } = await this._commandFactory(cmd);
if (err) {
console.error(`### App Server ### print(): adb error: ${err.message}`);
}
}
async uninstall(deviceSN, pkg) {
const { result, err } = await this._commandFactory(`adb -s ${deviceSN} uninstall ${pkg}`);
return { result, err };
}
async install(deviceSN, apkPath) {
const { result, err } = await this._commandFactory(`adb -s ${deviceSN} install ${apkPath}`);
return { result, err };
}
async isInstalled(deviceSN, pkg) {
const { result, err } = await this._commandFactory(`adb -s ${deviceSN} shell pm path ${pkg}`);
let isInstalled = false;
if (result && result.indexOf('package:') > -1) {
isInstalled = true;
}
return { isInstalled, err };
}
async startActivity(deviceSN, action, component, extra) {
let commandArray = [];
commandArray.push('adb', '-s', deviceSN, 'shell', 'am', 'start');
if (action !== undefined) {
commandArray.push('-a', action);
}
if (component !== undefined) {
commandArray.push('-n', component);
}
function parseExtra(extra) {
const typeMap = {
string: '--es',
null: '--esn',
boolean: '--ez',
int: '--ei',
float: '--ef',
uri: '--eu',
component: '--ecn',
'String[]': '--esa',
'int[]': '--eia',
'long[]': '--ela',
'float[]': '--efa',
};
let extraCommands = [];
extra.forEach(item => {
if (item.type in typeMap) {
extraCommands.push(typeMap[item.type]);
extraCommands.push(item.key);
if (item.type.endsWith('[]')) {
extraCommands.push(item.value.join(','));
}
else {
extraCommands.push(item.value);
}
}
});
return extraCommands;
}
if (extra) {
commandArray.push(parseExtra(extra).join(' '));
}
const { result, err } = await this._commandFactory(commandArray.join(' '));
return { result, err };
}
async exeCommand(command) {
return await this._commandFactory(command);
}
async getProp(deviceSN) {
const { result, err } = await this._commandFactory(`adb -s ${deviceSN} shell getprop ro.serialno`);
return { result, err };
}
}
const adbCommander = new ADBCommander();
module.exports = adbCommander;
+5
View File
@@ -0,0 +1,5 @@
export default interface Extra {
key: string;
type: string;
value: any;
}
+2
View File
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
+46
View File
@@ -0,0 +1,46 @@
{
"name": "adb-commander",
"version": "0.1.9",
"description": "A typescript project",
"files": [
"build"
],
"main": "build/AdbCommander.js",
"author": "gengjiawen <jiawen.geng@vivo.com>",
"scripts": {
"start": "tsc -w",
"clean": "rimraf build",
"format": "prettier \"{examples,lib,script,test}/**/*.{js,ts}\" \"*.yml\" --write",
"test": "jest",
"build": "npm run clean && tsc -p ./tsconfig.json"
},
"jest": {
"testEnvironment": "node",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testMatch": [
"**/?(*.)(spec|test).(ts|tsx|js)",
"**/__tests__/*.(ts|tsx|js)",
"**/test/*.(ts|tsx|js)"
]
},
"dependencies": {
"adb-driver": "^0.1.8"
},
"devDependencies": {
"@types/jest": "23.3.11",
"@types/node": "10.12.18",
"jest": "23.6.0",
"prettier": "1.15.3",
"rimraf": "2.6.3",
"ts-jest": "23.10.5",
"ts-node": "6.0.0",
"typescript": "3.2.4"
}
}