From f61caa774d043985d2965b6a56a4bd7838b8ad76 Mon Sep 17 00:00:00 2001 From: conganhhcmus Date: Wed, 3 Jul 2024 21:49:58 +0700 Subject: [PATCH] feat: refactor code --- exec/.env | 3 --- package-lock.json | 49 +-------------------------------------- package.json | 3 +-- src/lib/adb.js | 53 +++++++++++++++++++++++-------------------- src/utils/platform.js | 10 -------- src/websocket.js | 7 +++--- 6 files changed, 34 insertions(+), 91 deletions(-) delete mode 100644 src/utils/platform.js diff --git a/exec/.env b/exec/.env index bef6cbf..9e1007e 100755 --- a/exec/.env +++ b/exec/.env @@ -1,5 +1,2 @@ IS_BUILDED=FALSE PORT=9090 -ADB_PORT=5037 -ADB_HOST=127.0.0.1 -MONKEY_PORT=1080 diff --git a/package-lock.json b/package-lock.json index d4da6b2..974733b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,6 @@ "name": "auto-tools", "license": "ISC", "dependencies": { - "@devicefarmer/adbkit": "^3.2.6", "appium": "^2.11.1", "bluebird": "^3.7.2", "buffer": "^6.0.3", @@ -3035,42 +3034,6 @@ "kuler": "^2.0.0" } }, - "node_modules/@devicefarmer/adbkit": { - "version": "3.2.6", - "resolved": "https://registry.npmjs.org/@devicefarmer/adbkit/-/adbkit-3.2.6.tgz", - "integrity": "sha512-8lO1hSeTgtxcOHhp4tTWq/JaOysp5KNbbyFoxNEBnwkCDZu/Bji3ZfOaG++Riv9jN6c9bgdLBOZqJTC5VJPRKQ==", - "dependencies": { - "@devicefarmer/adbkit-logcat": "^2.1.2", - "@devicefarmer/adbkit-monkey": "~1.2.1", - "bluebird": "~3.7", - "commander": "^9.1.0", - "debug": "~4.3.1", - "node-forge": "^1.3.1", - "split": "~1.0.1" - }, - "bin": { - "adbkit": "bin/adbkit" - }, - "engines": { - "node": ">= 0.10.4" - } - }, - "node_modules/@devicefarmer/adbkit-logcat": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/@devicefarmer/adbkit-logcat/-/adbkit-logcat-2.1.3.tgz", - "integrity": "sha512-yeaGFjNBc/6+svbDeul1tNHtNChw6h8pSHAt5D+JsedUrMTN7tla7B15WLDyekxsuS2XlZHRxpuC6m92wiwCNw==", - "engines": { - "node": ">= 4" - } - }, - "node_modules/@devicefarmer/adbkit-monkey": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/@devicefarmer/adbkit-monkey/-/adbkit-monkey-1.2.1.tgz", - "integrity": "sha512-ZzZY/b66W2Jd6NHbAhLyDWOEIBWC11VizGFk7Wx7M61JZRz7HR9Cq5P+65RKWUU7u6wgsE8Lmh9nE4Mz+U2eTg==", - "engines": { - "node": ">= 0.10.4" - } - }, "node_modules/@discoveryjs/json-ext": { "version": "0.5.7", "resolved": "https://registry.npmjs.org/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz", @@ -16825,6 +16788,7 @@ "version": "1.3.1", "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz", "integrity": "sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==", + "dev": true, "engines": { "node": ">= 6.13.0" } @@ -20253,17 +20217,6 @@ "node": ">= 6" } }, - "node_modules/split": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", - "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", - "dependencies": { - "through": "2" - }, - "engines": { - "node": "*" - } - }, "node_modules/split2": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz", diff --git a/package.json b/package.json index 9144ff6..d128f6e 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ } }, "dependencies": { - "@devicefarmer/adbkit": "^3.2.6", "appium": "^2.11.1", "bluebird": "^3.7.2", "buffer": "^6.0.3", @@ -61,4 +60,4 @@ "webpack-cli": "^5.1.4", "webpack-dev-server": "^5.0.4" } -} \ No newline at end of file +} diff --git a/src/lib/adb.js b/src/lib/adb.js index c90e850..3f506ca 100644 --- a/src/lib/adb.js +++ b/src/lib/adb.js @@ -1,35 +1,40 @@ -const { Adb } = require('@devicefarmer/adbkit') const Bluebird = require('bluebird') +const { runExecAsync, runSpawn } = require('../utils/shell') -const { isMacOS } = require('../utils/platform') -const { runExecAsync } = require('../utils/shell') -const fs = require('fs') - -const client = Adb.createClient({ port: process.env.ADB_PORT || 5037, host: process.env.ADB_HOST || '127.0.0.1' }) +const getDeviceNameById = async (deviceId) => { + switch (process.platform) { + case 'darwin': + const output = await runExecAsync(`adb -s ${deviceId} emu avd name`) + return output.match(/([^\r\n|OK]+)/g)[0].replaceAll('_', ' ') + default: + return deviceId + } +} class ADBHelper { static getDevices = async () => { - const devices = await client.listDevices() - return await Bluebird.map(devices, async (_) => { - if (isMacOS) { - const output = await runExecAsync(`adb -s ${_.id} emu avd name`) - return { - id: _.id, - name: output.match(/([^\r\n|OK]+)/g)[0].replaceAll('_', ' '), - } - } - return { - id: _.id, - name: _.id, - } - }) + const ignoreText = ['device', 'offline'] + const output = await runExecAsync(`adb devices`) + const deviceIds = output + .substring(output.indexOf('\n') + 1) + .match(/[\S]+/g) + .filter((text) => !ignoreText.includes(text)) + + return await Bluebird.map(deviceIds, async (id) => ({ + id: id, + name: await getDeviceNameById(id), + })) } - static screenCap = async (deviceId, path) => { - const device = client.getDevice(deviceId) - const data = await device.screencap().then(Adb.util.readAll) + static screenCap = async (deviceId, path) => await runExecAsync(`adb -s ${deviceId} exec-out screencap -p > ${path}`) + + static screenRecord = (deviceId, outputHandler = null) => { + const streamProcess = runSpawn(`adb -s ${deviceId} exec-out screenrecord --output-format=h264 -`) + streamProcess.stdout.on('data', (data) => { + outputHandler && outputHandler(data) + }) - fs.writeFileSync(path, Buffer.from(data), 'binary') + return streamProcess } } diff --git a/src/utils/platform.js b/src/utils/platform.js deleted file mode 100644 index da01887..0000000 --- a/src/utils/platform.js +++ /dev/null @@ -1,10 +0,0 @@ -const platform = process.platform - -const isWindows = platform === 'win32' - -const isMacOS = platform === 'darwin' - -module.exports = { - isWindows, - isMacOS, -} diff --git a/src/websocket.js b/src/websocket.js index bf56bbb..58980ac 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -1,11 +1,10 @@ -const { runSpawn } = require('./utils/shell') +const { ADBHelper } = require('./lib/adb') function getLiveScreen(ws, req) { const deviceId = req.params.id - - const streamProcess = runSpawn(`adb -s ${deviceId} exec-out screenrecord --output-format=h264 -`) let chunk = Buffer.from([]) - streamProcess.stdout.on('data', (data) => { + + const streamProcess = ADBHelper.screenRecord(deviceId, (data) => { if (isBeginChunk(data)) { if (chunk.length > 0) { if (ws.readyState == 2 || ws.readyState == 3) {