Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:recycle: Inline Android device selection into the development script

Danielf32843f57c31
diff --git a/scripts/android-dev-run.mjs b/scripts/android-dev-run.mjs
index cd0f13fe45..053392a8da 100644
--- a/scripts/android-dev-run.mjs
+++ b/scripts/android-dev-run.mjs
@@ -8,7 +8,6 @@ import {copyFileSync, existsSync, readdirSync, readFileSync, statSync, unlinkSyn
import os from "node:os";
import path from "node:path";
import {fileURLToPath} from "node:url";
-import {selectDuplicateDevice} from "./android-device.mjs";
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const appDir = path.join(repoRoot, "app");
@@ -149,6 +148,28 @@ function wait(milliseconds) {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds);
}
+function selectDuplicateDevice(adb, devices, execute = spawnSync) {
+ let identity = null;
+ for (const serial of devices) {
+ // 连接名称可能随无线服务重新发布而变化,使用手机报告的序列号核对身份。
+ const result = execute(adb, ["-s", serial, "shell", "getprop", "ro.serialno"], {
+ encoding: "utf8",
+ timeout: 3000,
+ windowsHide: true,
+ });
+ const value = result.stdout?.trim();
+ // 无法确认身份时保留手动选择,避免把未知连接合并到另一台手机。
+ if (result.error || result.status !== 0 || !value || /^(unknown|null|0+)$/i.test(value)) {
+ return null;
+ }
+ if (identity !== null && identity !== value) {
+ return null;
+ }
+ identity = value;
+ }
+ return devices[0] || null;
+}
+
function deviceSerial(adb) {
const maxAttempts = 11;
let devices = [];
diff --git a/scripts/android-device.mjs b/scripts/android-device.mjs
deleted file mode 100644
index 317111d18e..0000000000
--- a/scripts/android-device.mjs
+++ /dev/null
@@ -1,23 +0,0 @@
-import {spawnSync} from "node:child_process";
-
-export function selectDuplicateDevice(adb, devices, execute = spawnSync) {
- let identity = null;
- for (const serial of devices) {
- // 连接名称可能随无线服务重新发布而变化,使用手机报告的序列号核对身份。
- const result = execute(adb, ["-s", serial, "shell", "getprop", "ro.serialno"], {
- encoding: "utf8",
- timeout: 3000,
- windowsHide: true,
- });
- const value = result.stdout?.trim();
- // 无法确认身份时保留手动选择,避免把未知连接合并到另一台手机。
- if (result.error || result.status !== 0 || !value || /^(unknown|null|0+)$/i.test(value)) {
- return null;
- }
- if (identity !== null && identity !== value) {
- return null;
- }
- identity = value;
- }
- return devices[0] || null;
-}
diff --git a/scripts/android-device.test.mjs b/scripts/android-device.test.mjs
deleted file mode 100644
index 68ad120b2d..0000000000
--- a/scripts/android-device.test.mjs
+++ /dev/null
@@ -1,44 +0,0 @@
-import assert from "node:assert/strict";
-import {test} from "node:test";
-import {selectDuplicateDevice} from "./android-device.mjs";
-
-const devices = ["adb-phone (2)._adb-tls-connect._tcp", "adb-phone._adb-tls-connect._tcp"];
-
-test("duplicate wireless connections use the physical serial without splitting names", () => {
- const calls = [];
- const selected = selectDuplicateDevice("adb", devices, (command, args, options) => {
- calls.push(args);
- assert.equal(command, "adb");
- assert.equal(options.timeout, 3000);
- return {status: 0, stdout: "physical-serial\r\n"};
- });
- assert.equal(selected, devices[0]);
- assert.deepEqual(calls, devices.map((serial) => ["-s", serial, "shell", "getprop", "ro.serialno"]));
-});
-
-test("similar connection names do not merge different phones", () => {
- assert.equal(selectDuplicateDevice("adb", devices, (command, args) => ({
- status: 0,
- stdout: args[1] === devices[0] ? "phone-a" : "phone-b",
- })), null);
-});
-
-test("USB and wireless connections to one phone can be merged", () => {
- assert.equal(selectDuplicateDevice("adb", ["physical-serial", devices[0]], () => ({
- status: 0, stdout: "physical-serial",
- })), "physical-serial");
-});
-
-test("unidentified or unresponsive connections require explicit selection", () => {
- for (const result of [
- {status: 0, stdout: ""},
- {status: 0, stdout: "unknown"},
- {status: 0, stdout: "00000000"},
- {status: 1, stdout: ""},
- {status: null, error: new Error("timeout")},
- ]) {
- let calls = 0;
- assert.equal(selectDuplicateDevice("adb", devices, () => ++calls === 1
- ? {status: 0, stdout: "phone-a"} : result), null);
- }
-});