openuispec 0.2.19 → 0.2.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/check/audit.js +392 -0
- package/dist/check/index.js +216 -0
- package/dist/cli/configure-target.js +391 -0
- package/dist/cli/index.js +510 -0
- package/dist/cli/init.js +1047 -0
- package/dist/drift/index.js +903 -0
- package/dist/mcp-server/index.js +886 -0
- package/dist/mcp-server/preview-render.js +1761 -0
- package/dist/mcp-server/preview.js +233 -0
- package/dist/mcp-server/screenshot-android.js +458 -0
- package/dist/mcp-server/screenshot-ios.js +639 -0
- package/dist/mcp-server/screenshot-shared.js +180 -0
- package/dist/mcp-server/screenshot.js +459 -0
- package/dist/prepare/index.js +1216 -0
- package/dist/runtime/package-paths.js +33 -0
- package/dist/schema/semantic-lint.js +564 -0
- package/dist/schema/validate.js +689 -0
- package/dist/status/index.js +194 -0
- package/package.json +12 -13
- package/check/audit.ts +0 -426
- package/check/index.ts +0 -320
- package/cli/configure-target.ts +0 -523
- package/cli/index.ts +0 -537
- package/cli/init.ts +0 -1253
- package/drift/index.ts +0 -1165
- package/mcp-server/index.ts +0 -1041
- package/mcp-server/preview-render.ts +0 -1922
- package/mcp-server/preview.ts +0 -292
- package/mcp-server/screenshot-android.ts +0 -621
- package/mcp-server/screenshot-ios.ts +0 -753
- package/mcp-server/screenshot-shared.ts +0 -237
- package/mcp-server/screenshot.ts +0 -563
- package/prepare/index.ts +0 -1530
- package/schema/semantic-lint.ts +0 -692
- package/schema/validate.ts +0 -870
- package/scripts/regenerate-previews.ts +0 -136
- package/scripts/take-all-screenshots.ts +0 -507
- package/status/index.ts +0 -275
|
@@ -1,621 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Android screenshot tool — builds the app, installs on an emulator,
|
|
3
|
-
* and captures real screenshots via adb screencap.
|
|
4
|
-
*
|
|
5
|
-
* Gives pixel-perfect results with real navigation, images, themes,
|
|
6
|
-
* and all runtime behavior. Requires a running Android emulator.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { exec as execCb, execSync } from "node:child_process";
|
|
10
|
-
import { promisify } from "node:util";
|
|
11
|
-
import { existsSync, readFileSync, unlinkSync, mkdirSync, copyFileSync } from "node:fs";
|
|
12
|
-
import { join, resolve } from "node:path";
|
|
13
|
-
import {
|
|
14
|
-
type ScreenshotResult,
|
|
15
|
-
findPlatformAppDir,
|
|
16
|
-
buildScreenshotResponse,
|
|
17
|
-
} from "./screenshot-shared.js";
|
|
18
|
-
|
|
19
|
-
const exec = promisify(execCb);
|
|
20
|
-
const androidScreenshotQueues = new Map<string, Promise<void>>();
|
|
21
|
-
|
|
22
|
-
// ── types ───────────────────────────────────────────────────────────
|
|
23
|
-
|
|
24
|
-
export interface AndroidScreenshotOptions {
|
|
25
|
-
screen?: string;
|
|
26
|
-
route?: string;
|
|
27
|
-
nav?: string[];
|
|
28
|
-
theme?: "light" | "dark";
|
|
29
|
-
wait_for?: number;
|
|
30
|
-
output_dir?: string;
|
|
31
|
-
project_dir?: string;
|
|
32
|
-
module?: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface AndroidBatchCapture {
|
|
36
|
-
screen: string;
|
|
37
|
-
route?: string;
|
|
38
|
-
nav?: string[];
|
|
39
|
-
wait_for?: number;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface AndroidScreenshotBatchOptions {
|
|
43
|
-
captures: AndroidBatchCapture[];
|
|
44
|
-
theme?: "light" | "dark";
|
|
45
|
-
output_dir?: string;
|
|
46
|
-
project_dir?: string;
|
|
47
|
-
module?: string;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// ── constants ───────────────────────────────────────────────────────
|
|
51
|
-
|
|
52
|
-
const ADB_SCREENSHOT_PATH = "/sdcard/openuispec_screenshot.png";
|
|
53
|
-
|
|
54
|
-
// ── Android app directory discovery ─────────────────────────────────
|
|
55
|
-
|
|
56
|
-
function isAndroidProject(dir: string): boolean {
|
|
57
|
-
return existsSync(join(dir, "gradlew")) ||
|
|
58
|
-
existsSync(join(dir, "app", "build.gradle.kts")) ||
|
|
59
|
-
existsSync(join(dir, "app", "build.gradle"));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function findAndroidAppDir(projectCwd: string, directDir?: string): string {
|
|
63
|
-
return findPlatformAppDir(projectCwd, "android", isAndroidProject, directDir);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// ── app module auto-detection ────────────────────────────────────────
|
|
67
|
-
|
|
68
|
-
export function detectAppModule(androidDir: string): string {
|
|
69
|
-
// Read settings.gradle.kts or settings.gradle to find included modules
|
|
70
|
-
for (const settingsFile of ["settings.gradle.kts", "settings.gradle"]) {
|
|
71
|
-
const settingsPath = join(androidDir, settingsFile);
|
|
72
|
-
if (!existsSync(settingsPath)) continue;
|
|
73
|
-
|
|
74
|
-
try {
|
|
75
|
-
const content = readFileSync(settingsPath, "utf-8");
|
|
76
|
-
// Match include(":app"), include(":module1", ":module2"), include ":app"
|
|
77
|
-
const modules: string[] = [];
|
|
78
|
-
const includeMatches = content.matchAll(/include\s*\(?([^)\n]+)\)?/g);
|
|
79
|
-
for (const m of includeMatches) {
|
|
80
|
-
const args = m[1];
|
|
81
|
-
const moduleNames = args.matchAll(/[":]+([^",:)\s]+)/g);
|
|
82
|
-
for (const mn of moduleNames) {
|
|
83
|
-
modules.push(mn[1]);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// For each module, check if its build.gradle.kts/.gradle has com.android.application
|
|
88
|
-
for (const mod of modules) {
|
|
89
|
-
const modDir = join(androidDir, mod);
|
|
90
|
-
for (const buildFile of ["build.gradle.kts", "build.gradle"]) {
|
|
91
|
-
const buildPath = join(modDir, buildFile);
|
|
92
|
-
if (!existsSync(buildPath)) continue;
|
|
93
|
-
try {
|
|
94
|
-
const buildContent = readFileSync(buildPath, "utf-8");
|
|
95
|
-
if (buildContent.includes("com.android.application")) {
|
|
96
|
-
return mod;
|
|
97
|
-
}
|
|
98
|
-
} catch { /* skip */ }
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
} catch { /* skip */ }
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return "app"; // fallback
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// ── app package / activity extraction ───────────────────────────────
|
|
108
|
-
|
|
109
|
-
export interface AppInfo {
|
|
110
|
-
applicationId: string;
|
|
111
|
-
launchActivity: string;
|
|
112
|
-
moduleName: string;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function readBuildFile(androidDir: string, moduleName: string): string | null {
|
|
116
|
-
for (const filename of ["build.gradle.kts", "build.gradle"]) {
|
|
117
|
-
try { return readFileSync(join(androidDir, moduleName, filename), "utf-8"); } catch { /* skip */ }
|
|
118
|
-
}
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function extractAppInfo(androidDir: string, moduleOverride?: string): AppInfo {
|
|
123
|
-
const moduleName = moduleOverride ?? detectAppModule(androidDir);
|
|
124
|
-
|
|
125
|
-
// Try to get applicationId from build.gradle.kts or build.gradle
|
|
126
|
-
let applicationId = "";
|
|
127
|
-
const buildContent = readBuildFile(androidDir, moduleName);
|
|
128
|
-
if (buildContent) {
|
|
129
|
-
// Kotlin DSL: applicationId = "..."
|
|
130
|
-
const appIdMatch = buildContent.match(/applicationId\s*=\s*"([^"]+)"/);
|
|
131
|
-
if (appIdMatch) applicationId = appIdMatch[1];
|
|
132
|
-
if (!applicationId) {
|
|
133
|
-
// Groovy DSL: applicationId "..." or applicationId '...'
|
|
134
|
-
const groovyMatch = buildContent.match(/applicationId\s+['"]([^'"]+)['"]/);
|
|
135
|
-
if (groovyMatch) applicationId = groovyMatch[1];
|
|
136
|
-
}
|
|
137
|
-
if (!applicationId) {
|
|
138
|
-
const nsMatch = buildContent.match(/namespace\s*=?\s*['"]([^'"]+)['"]/);
|
|
139
|
-
if (nsMatch) applicationId = nsMatch[1];
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Get launch activity from AndroidManifest.xml
|
|
144
|
-
let launchActivity = ".MainActivity";
|
|
145
|
-
const manifestPath = join(androidDir, moduleName, "src", "main", "AndroidManifest.xml");
|
|
146
|
-
try {
|
|
147
|
-
const manifest = readFileSync(manifestPath, "utf-8");
|
|
148
|
-
// Find activity with MAIN/LAUNCHER intent filter
|
|
149
|
-
const activityMatch = manifest.match(
|
|
150
|
-
/<activity[^>]*android:name="([^"]+)"[^]*?action android:name="android\.intent\.action\.MAIN"/,
|
|
151
|
-
);
|
|
152
|
-
if (activityMatch) launchActivity = activityMatch[1];
|
|
153
|
-
|
|
154
|
-
// If applicationId still empty, try manifest package
|
|
155
|
-
if (!applicationId) {
|
|
156
|
-
const pkgMatch = manifest.match(/package="([^"]+)"/);
|
|
157
|
-
if (pkgMatch) applicationId = pkgMatch[1];
|
|
158
|
-
}
|
|
159
|
-
} catch { /* use defaults */ }
|
|
160
|
-
|
|
161
|
-
if (!applicationId) {
|
|
162
|
-
throw new Error(
|
|
163
|
-
`Could not determine applicationId from ${moduleName}/build.gradle.kts (or .gradle) or AndroidManifest.xml`,
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Resolve relative activity name
|
|
168
|
-
const fullActivity = launchActivity.startsWith(".")
|
|
169
|
-
? `${applicationId}${launchActivity}`
|
|
170
|
-
: launchActivity;
|
|
171
|
-
|
|
172
|
-
return { applicationId, launchActivity: fullActivity, moduleName };
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// ── adb helpers ─────────────────────────────────────────────────────
|
|
176
|
-
|
|
177
|
-
export function findAdb(): string {
|
|
178
|
-
// Check ANDROID_HOME first
|
|
179
|
-
const androidHome = process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT;
|
|
180
|
-
if (androidHome) {
|
|
181
|
-
const adbPath = join(androidHome, "platform-tools", "adb");
|
|
182
|
-
if (existsSync(adbPath)) return adbPath;
|
|
183
|
-
}
|
|
184
|
-
// Fall back to PATH
|
|
185
|
-
try {
|
|
186
|
-
execSync("which adb", { stdio: "pipe" });
|
|
187
|
-
return "adb";
|
|
188
|
-
} catch {
|
|
189
|
-
throw new Error(
|
|
190
|
-
"adb not found. Set ANDROID_HOME or add platform-tools to PATH.\n" +
|
|
191
|
-
"Install via Android Studio or: sdkmanager 'platform-tools'",
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
export async function getConnectedEmulator(adb: string): Promise<string> {
|
|
197
|
-
try {
|
|
198
|
-
const { stdout } = await exec(`${adb} devices`);
|
|
199
|
-
const lines = stdout.trim().split("\n").slice(1); // skip header
|
|
200
|
-
for (const line of lines) {
|
|
201
|
-
const [serial, state] = line.split("\t");
|
|
202
|
-
if (state === "device" && (serial.startsWith("emulator-") || serial.includes(":"))) {
|
|
203
|
-
return serial;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
// Also accept physical devices if no emulator
|
|
207
|
-
for (const line of lines) {
|
|
208
|
-
const [serial, state] = line.split("\t");
|
|
209
|
-
if (state === "device") return serial;
|
|
210
|
-
}
|
|
211
|
-
} catch { /* fall through */ }
|
|
212
|
-
|
|
213
|
-
throw new Error(
|
|
214
|
-
"No connected Android device or emulator found.\n" +
|
|
215
|
-
"Start an emulator from Android Studio or run: emulator -avd <avd_name>",
|
|
216
|
-
);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export async function adbShell(adb: string, serial: string, cmd: string): Promise<string> {
|
|
220
|
-
const { stdout } = await exec(`${adb} -s ${serial} shell ${cmd}`, { timeout: 30_000 });
|
|
221
|
-
return stdout.trim();
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
export async function adbExec(adb: string, serial: string, args: string): Promise<string> {
|
|
225
|
-
const { stdout } = await exec(`${adb} -s ${serial} ${args}`, { timeout: 60_000 });
|
|
226
|
-
return stdout.trim();
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// ── emulator storage cleanup ─────────────────────────────────────────
|
|
230
|
-
|
|
231
|
-
export async function cleanEmulatorStorage(adb: string, serial: string): Promise<void> {
|
|
232
|
-
const cmds = [
|
|
233
|
-
`pm trim-caches 2G`, // aggressively trim package cache
|
|
234
|
-
`rm -rf /data/local/tmp/*.apk`, // leftover APKs from previous installs
|
|
235
|
-
`rm -f /sdcard/openuispec_screenshot.png /sdcard/ui_dump.xml /sdcard/screenshot.png`,
|
|
236
|
-
];
|
|
237
|
-
for (const cmd of cmds) {
|
|
238
|
-
try { await adbShell(adb, serial, cmd); } catch { /* ignore */ }
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// ── build APK ───────────────────────────────────────────────────────
|
|
243
|
-
|
|
244
|
-
export async function buildApk(androidDir: string, moduleName: string): Promise<string> {
|
|
245
|
-
if (!existsSync(join(androidDir, "gradlew"))) {
|
|
246
|
-
throw new Error(`No gradlew found in ${androidDir}. Initialize the Gradle wrapper first.`);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
try {
|
|
250
|
-
await exec(`./gradlew :${moduleName}:assembleDebug`, {
|
|
251
|
-
cwd: androidDir,
|
|
252
|
-
timeout: 300_000,
|
|
253
|
-
env: { ...process.env, JAVA_OPTS: "-Xmx2g" },
|
|
254
|
-
});
|
|
255
|
-
} catch (err: any) {
|
|
256
|
-
const output = ((err.stderr ?? "") + "\n" + (err.stdout ?? "")).slice(-500);
|
|
257
|
-
throw new Error(`APK build failed:\n${output}`);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// Find the built APK
|
|
261
|
-
const apkCandidates = [
|
|
262
|
-
join(androidDir, moduleName, "build", "outputs", "apk", "debug", `${moduleName}-debug.apk`),
|
|
263
|
-
join(androidDir, moduleName, "build", "outputs", "apk", "debug", `${moduleName}-debug-unsigned.apk`),
|
|
264
|
-
// Common fallback names
|
|
265
|
-
join(androidDir, moduleName, "build", "outputs", "apk", "debug", "app-debug.apk"),
|
|
266
|
-
join(androidDir, moduleName, "build", "outputs", "apk", "debug", "app-debug-unsigned.apk"),
|
|
267
|
-
];
|
|
268
|
-
for (const apk of apkCandidates) {
|
|
269
|
-
if (existsSync(apk)) return apk;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
throw new Error(`APK not found after build in ${moduleName}/build/outputs/. Check Gradle output.`);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// ── install & launch ────────────────────────────────────────────────
|
|
276
|
-
|
|
277
|
-
export async function installAndLaunch(
|
|
278
|
-
adb: string,
|
|
279
|
-
serial: string,
|
|
280
|
-
apkPath: string,
|
|
281
|
-
appInfo: AppInfo,
|
|
282
|
-
route?: string,
|
|
283
|
-
): Promise<void> {
|
|
284
|
-
// Force-stop and uninstall to free storage + wipe saved nav state
|
|
285
|
-
await adbShell(adb, serial, `am force-stop ${appInfo.applicationId}`);
|
|
286
|
-
try { await adbShell(adb, serial, `pm uninstall ${appInfo.applicationId}`); } catch { /* not installed */ }
|
|
287
|
-
|
|
288
|
-
// Install fresh (not -r replace, since we uninstalled)
|
|
289
|
-
await adbExec(adb, serial, `install "${apkPath}"`);
|
|
290
|
-
|
|
291
|
-
if (route) {
|
|
292
|
-
await adbShell(adb, serial,
|
|
293
|
-
`am start -W -a android.intent.action.VIEW -d '${route}' ` +
|
|
294
|
-
`${appInfo.applicationId}/${appInfo.launchActivity}`);
|
|
295
|
-
} else {
|
|
296
|
-
await adbShell(adb, serial,
|
|
297
|
-
`am start -W -n ${appInfo.applicationId}/${appInfo.launchActivity}`);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
export async function launchInstalledApp(
|
|
302
|
-
adb: string,
|
|
303
|
-
serial: string,
|
|
304
|
-
appInfo: AppInfo,
|
|
305
|
-
route?: string,
|
|
306
|
-
): Promise<void> {
|
|
307
|
-
await adbShell(adb, serial, `am force-stop ${appInfo.applicationId}`);
|
|
308
|
-
// Clear saved nav state so deep links route correctly
|
|
309
|
-
try { await adbShell(adb, serial, `pm clear ${appInfo.applicationId}`); } catch { /* ignore */ }
|
|
310
|
-
if (route) {
|
|
311
|
-
await adbShell(
|
|
312
|
-
adb,
|
|
313
|
-
serial,
|
|
314
|
-
`am start -W -a android.intent.action.VIEW -d '${route}' ` +
|
|
315
|
-
`${appInfo.applicationId}/${appInfo.launchActivity}`,
|
|
316
|
-
);
|
|
317
|
-
} else {
|
|
318
|
-
await adbShell(adb, serial, `am start -W -n ${appInfo.applicationId}/${appInfo.launchActivity}`);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// ── theme control ───────────────────────────────────────────────────
|
|
323
|
-
|
|
324
|
-
export async function setTheme(adb: string, serial: string, theme: "light" | "dark"): Promise<void> {
|
|
325
|
-
const mode = theme === "dark" ? "yes" : "no";
|
|
326
|
-
await adbShell(adb, serial, `cmd uimode night ${mode}`);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// ── UI navigation via tap ───────────────────────────────────────────
|
|
330
|
-
|
|
331
|
-
export async function tapByText(adb: string, serial: string, text: string): Promise<void> {
|
|
332
|
-
// Dump UI hierarchy
|
|
333
|
-
await adbShell(adb, serial, `uiautomator dump /sdcard/ui_dump.xml`);
|
|
334
|
-
const xml = await adbShell(adb, serial, `cat /sdcard/ui_dump.xml`);
|
|
335
|
-
await adbShell(adb, serial, `rm /sdcard/ui_dump.xml`);
|
|
336
|
-
|
|
337
|
-
const lowerText = text.toLowerCase();
|
|
338
|
-
|
|
339
|
-
// Parse each <node .../> extracting text, content-desc, and bounds
|
|
340
|
-
const nodeRegex = /<node\s[^>]+>/g;
|
|
341
|
-
interface UiNode { text: string; desc: string; cx: number; cy: number }
|
|
342
|
-
const nodes: UiNode[] = [];
|
|
343
|
-
|
|
344
|
-
let nodeMatch;
|
|
345
|
-
while ((nodeMatch = nodeRegex.exec(xml)) !== null) {
|
|
346
|
-
const attrs = nodeMatch[0];
|
|
347
|
-
const textMatch = attrs.match(/\btext="([^"]*)"/);
|
|
348
|
-
const descMatch = attrs.match(/\bcontent-desc="([^"]*)"/);
|
|
349
|
-
const boundsMatch = attrs.match(/\bbounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"/);
|
|
350
|
-
if (!boundsMatch) continue;
|
|
351
|
-
|
|
352
|
-
nodes.push({
|
|
353
|
-
text: textMatch?.[1] ?? "",
|
|
354
|
-
desc: descMatch?.[1] ?? "",
|
|
355
|
-
cx: Math.round((parseInt(boundsMatch[1]) + parseInt(boundsMatch[3])) / 2),
|
|
356
|
-
cy: Math.round((parseInt(boundsMatch[2]) + parseInt(boundsMatch[4])) / 2),
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
// Exact match on text or content-desc
|
|
361
|
-
for (const node of nodes) {
|
|
362
|
-
if (node.text.toLowerCase() === lowerText || node.desc.toLowerCase() === lowerText) {
|
|
363
|
-
await adbShell(adb, serial, `input tap ${node.cx} ${node.cy}`);
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
// Partial/contains match
|
|
369
|
-
for (const node of nodes) {
|
|
370
|
-
if (node.text.toLowerCase().includes(lowerText) || node.desc.toLowerCase().includes(lowerText)) {
|
|
371
|
-
await adbShell(adb, serial, `input tap ${node.cx} ${node.cy}`);
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
throw new Error(`UI element with text "${text}" not found on screen.`);
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
export async function navigateByTaps(
|
|
380
|
-
adb: string,
|
|
381
|
-
serial: string,
|
|
382
|
-
steps: string[],
|
|
383
|
-
): Promise<void> {
|
|
384
|
-
for (const step of steps) {
|
|
385
|
-
await tapByText(adb, serial, step);
|
|
386
|
-
// Wait for navigation animation
|
|
387
|
-
await new Promise(r => setTimeout(r, 800));
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
// ── screenshot capture ──────────────────────────────────────────────
|
|
392
|
-
|
|
393
|
-
export async function captureScreenshot(
|
|
394
|
-
adb: string,
|
|
395
|
-
serial: string,
|
|
396
|
-
localPath: string,
|
|
397
|
-
): Promise<void> {
|
|
398
|
-
try {
|
|
399
|
-
await exec(`${adb} -s ${serial} exec-out screencap -p > "${localPath}"`, { timeout: 60_000, shell: "/bin/bash" });
|
|
400
|
-
} catch (err: any) {
|
|
401
|
-
const output = ((err.stderr ?? "") + "\n" + (err.stdout ?? "")).trim();
|
|
402
|
-
throw new Error(`Android screenshot capture failed${output ? `:\n${output}` : "."}`);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// ── wait for app ready ──────────────────────────────────────────────
|
|
407
|
-
|
|
408
|
-
async function waitForAppReady(
|
|
409
|
-
adb: string,
|
|
410
|
-
serial: string,
|
|
411
|
-
applicationId: string,
|
|
412
|
-
waitMs: number,
|
|
413
|
-
): Promise<void> {
|
|
414
|
-
// Wait for the activity to be in resumed state
|
|
415
|
-
const startTime = Date.now();
|
|
416
|
-
const timeout = Math.min(waitMs, 15_000);
|
|
417
|
-
|
|
418
|
-
while (Date.now() - startTime < timeout) {
|
|
419
|
-
try {
|
|
420
|
-
const output = await adbShell(adb, serial,
|
|
421
|
-
`dumpsys activity activities | grep -E "mResumedActivity|topResumedActivity"`,
|
|
422
|
-
);
|
|
423
|
-
if (output.includes(applicationId)) {
|
|
424
|
-
// App is in foreground, wait the remaining time for content to load
|
|
425
|
-
const elapsed = Date.now() - startTime;
|
|
426
|
-
const remaining = Math.max(waitMs - elapsed, 500);
|
|
427
|
-
await new Promise(r => setTimeout(r, remaining));
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
} catch { /* activity not ready yet */ }
|
|
431
|
-
await new Promise(r => setTimeout(r, 500));
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
// Fallback: just wait the full duration
|
|
435
|
-
await new Promise(r => setTimeout(r, waitMs));
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
async function takeSingleAndroidCapture(
|
|
439
|
-
adb: string,
|
|
440
|
-
serial: string,
|
|
441
|
-
androidDir: string,
|
|
442
|
-
appInfo: AppInfo,
|
|
443
|
-
capture: AndroidBatchCapture,
|
|
444
|
-
theme: "light" | "dark" | undefined,
|
|
445
|
-
defaultOutputDir: string | undefined,
|
|
446
|
-
): Promise<{ screen: string; path: string; data: string }> {
|
|
447
|
-
await launchInstalledApp(adb, serial, appInfo, capture.route);
|
|
448
|
-
await waitForAppReady(adb, serial, appInfo.applicationId, capture.wait_for ?? 3000);
|
|
449
|
-
|
|
450
|
-
if (capture.nav && capture.nav.length > 0) {
|
|
451
|
-
await navigateByTaps(adb, serial, capture.nav);
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
const themeLabel = theme ?? "default";
|
|
455
|
-
const filename = `${capture.screen}_${themeLabel}.png`;
|
|
456
|
-
const tmpPath = join(androidDir, `.openuispec-screenshot-${capture.screen}.png`);
|
|
457
|
-
await captureScreenshot(adb, serial, tmpPath);
|
|
458
|
-
|
|
459
|
-
let savedPath = filename;
|
|
460
|
-
if (defaultOutputDir) {
|
|
461
|
-
const outDir = resolve(androidDir, defaultOutputDir);
|
|
462
|
-
mkdirSync(outDir, { recursive: true });
|
|
463
|
-
savedPath = join(outDir, filename);
|
|
464
|
-
copyFileSync(tmpPath, savedPath);
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
try {
|
|
468
|
-
const data = readFileSync(tmpPath).toString("base64");
|
|
469
|
-
return {
|
|
470
|
-
screen: capture.screen,
|
|
471
|
-
path: savedPath,
|
|
472
|
-
data,
|
|
473
|
-
};
|
|
474
|
-
} finally {
|
|
475
|
-
try { unlinkSync(tmpPath); } catch { /* ignore */ }
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// ── main entry point ────────────────────────────────────────────────
|
|
480
|
-
|
|
481
|
-
export async function takeAndroidScreenshot(
|
|
482
|
-
projectCwd: string,
|
|
483
|
-
options: AndroidScreenshotOptions,
|
|
484
|
-
): Promise<ScreenshotResult> {
|
|
485
|
-
const {
|
|
486
|
-
screen,
|
|
487
|
-
route,
|
|
488
|
-
nav,
|
|
489
|
-
theme,
|
|
490
|
-
wait_for = 3000,
|
|
491
|
-
output_dir,
|
|
492
|
-
project_dir,
|
|
493
|
-
module,
|
|
494
|
-
} = options;
|
|
495
|
-
|
|
496
|
-
// 1. Find Android project
|
|
497
|
-
const androidDir = findAndroidAppDir(projectCwd, project_dir);
|
|
498
|
-
const appInfo = extractAppInfo(androidDir, module);
|
|
499
|
-
|
|
500
|
-
// 2. Find adb and emulator
|
|
501
|
-
const adb = findAdb();
|
|
502
|
-
const serial = await getConnectedEmulator(adb);
|
|
503
|
-
|
|
504
|
-
const previousRun = androidScreenshotQueues.get(serial) ?? Promise.resolve();
|
|
505
|
-
let releaseQueue: (() => void) | undefined;
|
|
506
|
-
const currentRun = new Promise<void>((resolve) => {
|
|
507
|
-
releaseQueue = resolve;
|
|
508
|
-
});
|
|
509
|
-
androidScreenshotQueues.set(serial, previousRun.then(() => currentRun));
|
|
510
|
-
|
|
511
|
-
await previousRun;
|
|
512
|
-
|
|
513
|
-
try {
|
|
514
|
-
// 3. Free emulator storage before build/install
|
|
515
|
-
await cleanEmulatorStorage(adb, serial);
|
|
516
|
-
|
|
517
|
-
// 4. Build APK
|
|
518
|
-
const apkPath = await buildApk(androidDir, appInfo.moduleName);
|
|
519
|
-
|
|
520
|
-
// 5. Set theme if requested
|
|
521
|
-
if (theme) {
|
|
522
|
-
await setTheme(adb, serial, theme);
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
// 6. Install fresh once, then capture
|
|
526
|
-
await installAndLaunch(adb, serial, apkPath, appInfo, route);
|
|
527
|
-
|
|
528
|
-
const snapshot = await takeSingleAndroidCapture(
|
|
529
|
-
adb,
|
|
530
|
-
serial,
|
|
531
|
-
androidDir,
|
|
532
|
-
appInfo,
|
|
533
|
-
{ screen: screen ?? "main", route, nav, wait_for },
|
|
534
|
-
theme,
|
|
535
|
-
output_dir,
|
|
536
|
-
);
|
|
537
|
-
|
|
538
|
-
return buildScreenshotResponse([snapshot], (s) => ({
|
|
539
|
-
screen: s.screen,
|
|
540
|
-
path: snapshot.path ?? null,
|
|
541
|
-
emulator: serial,
|
|
542
|
-
theme: theme ?? "default",
|
|
543
|
-
applicationId: appInfo.applicationId,
|
|
544
|
-
}));
|
|
545
|
-
} finally {
|
|
546
|
-
releaseQueue?.();
|
|
547
|
-
if (androidScreenshotQueues.get(serial) === currentRun) {
|
|
548
|
-
androidScreenshotQueues.delete(serial);
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
export async function takeAndroidScreenshotBatch(
|
|
554
|
-
projectCwd: string,
|
|
555
|
-
options: AndroidScreenshotBatchOptions,
|
|
556
|
-
): Promise<ScreenshotResult> {
|
|
557
|
-
const { captures, theme, output_dir, project_dir, module } = options;
|
|
558
|
-
if (captures.length === 0) {
|
|
559
|
-
return {
|
|
560
|
-
content: [{ type: "text", text: "No Android captures specified." }],
|
|
561
|
-
isError: true,
|
|
562
|
-
};
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
const androidDir = findAndroidAppDir(projectCwd, project_dir);
|
|
566
|
-
const appInfo = extractAppInfo(androidDir, module);
|
|
567
|
-
const adb = findAdb();
|
|
568
|
-
const serial = await getConnectedEmulator(adb);
|
|
569
|
-
|
|
570
|
-
const previousRun = androidScreenshotQueues.get(serial) ?? Promise.resolve();
|
|
571
|
-
let releaseQueue: (() => void) | undefined;
|
|
572
|
-
const currentRun = new Promise<void>((resolve) => {
|
|
573
|
-
releaseQueue = resolve;
|
|
574
|
-
});
|
|
575
|
-
androidScreenshotQueues.set(serial, previousRun.then(() => currentRun));
|
|
576
|
-
|
|
577
|
-
await previousRun;
|
|
578
|
-
|
|
579
|
-
try {
|
|
580
|
-
await cleanEmulatorStorage(adb, serial);
|
|
581
|
-
const apkPath = await buildApk(androidDir, appInfo.moduleName);
|
|
582
|
-
|
|
583
|
-
if (theme) {
|
|
584
|
-
await setTheme(adb, serial, theme);
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
await installAndLaunch(adb, serial, apkPath, appInfo);
|
|
588
|
-
|
|
589
|
-
// Pre-create output dir once
|
|
590
|
-
if (output_dir) mkdirSync(resolve(androidDir, output_dir), { recursive: true });
|
|
591
|
-
|
|
592
|
-
const snapshots = [];
|
|
593
|
-
for (let index = 0; index < captures.length; index += 1) {
|
|
594
|
-
const capture = captures[index];
|
|
595
|
-
snapshots.push(
|
|
596
|
-
await takeSingleAndroidCapture(
|
|
597
|
-
adb,
|
|
598
|
-
serial,
|
|
599
|
-
androidDir,
|
|
600
|
-
appInfo,
|
|
601
|
-
capture,
|
|
602
|
-
theme,
|
|
603
|
-
output_dir,
|
|
604
|
-
),
|
|
605
|
-
);
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
return buildScreenshotResponse(snapshots, (snapshot) => ({
|
|
609
|
-
screen: snapshot.screen,
|
|
610
|
-
path: snapshot.path,
|
|
611
|
-
emulator: serial,
|
|
612
|
-
theme: theme ?? "default",
|
|
613
|
-
applicationId: appInfo.applicationId,
|
|
614
|
-
}));
|
|
615
|
-
} finally {
|
|
616
|
-
releaseQueue?.();
|
|
617
|
-
if (androidScreenshotQueues.get(serial) === currentRun) {
|
|
618
|
-
androidScreenshotQueues.delete(serial);
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
}
|