-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·296 lines (260 loc) · 10 KB
/
cli.js
File metadata and controls
executable file
·296 lines (260 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env node
/**
* cli.js -- baremobile CLI entry point.
*
* See `baremobile` (no args) for full command reference.
*/
import { resolve } from 'node:path';
import { unlinkSync } from 'node:fs';
import { join } from 'node:path';
import { createInterface } from 'node:readline';
import { setupMenu, renewCert, teardown } from './src/setup.js';
const args = process.argv.slice(2);
const cmd = args[0];
const jsonMode = hasFlag('--json');
const platform = parseFlag('--platform') || 'android';
/** Write a single JSON line to stdout. */
function jsonOut(obj) {
process.stdout.write(JSON.stringify(obj) + '\n');
}
/** Write an error — JSON line or stderr depending on mode. */
function errOut(msg, exitCode = 1) {
if (jsonMode) jsonOut({ ok: false, error: msg });
else process.stderr.write(`Error: ${msg}\n`);
process.exit(exitCode);
}
/** Build the UI object for setup wizards. */
function createUi() {
let failed = false;
function prompt(question) {
const rl = createInterface({ input: process.stdin, output: process.stdout });
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim());
});
});
}
return {
prompt,
waitForEnter: (msg) => prompt(`${msg} [press Enter to continue] `),
write: (s) => process.stdout.write(s),
ok: (s) => process.stdout.write(`\x1b[32m✓\x1b[0m ${s}\n`),
fail: (s) => { failed = true; process.stdout.write(`\x1b[31m✗\x1b[0m ${s}\n`); },
warn: (s) => process.stdout.write(`\x1b[33m!\x1b[0m ${s}\n`),
step: (n, s) => process.stdout.write(`\n\x1b[33m[${n}]\x1b[0m ${s}\n`),
get failed() { return failed; },
};
}
// Hidden internal flag: --daemon-internal
if (args.includes('--daemon-internal')) {
await runDaemonInternal();
} else if (cmd === 'mcp') {
await import('./mcp-server.js');
} else if (cmd === 'open') {
await cmdOpen();
} else if (cmd === 'close') {
await cmdProxy('close');
} else if (cmd === 'status') {
await cmdStatus();
} else if (cmd === 'snapshot') {
await cmdProxy('snapshot');
} else if (cmd === 'screenshot') {
await cmdProxy('screenshot');
} else if (cmd === 'tap' && args[1]) {
await cmdProxy('tap', { ref: args[1] });
} else if (cmd === 'type' && args[1] && args[2]) {
await cmdProxy('type', { ref: args[1], text: args.slice(2).filter(a => !a.startsWith('--')).join(' '), clear: hasFlag('--clear') });
} else if (cmd === 'press' && args[1]) {
await cmdProxy('press', { key: args[1] });
} else if (cmd === 'scroll' && args[1] && args[2]) {
await cmdProxy('scroll', { ref: args[1], direction: args[2] });
} else if (cmd === 'swipe' && args[1] && args[2] && args[3] && args[4]) {
await cmdProxy('swipe', { x1: Number(args[1]), y1: Number(args[2]), x2: Number(args[3]), y2: Number(args[4]), duration: parseFlag('--duration') ? Number(parseFlag('--duration')) : undefined });
} else if (cmd === 'long-press' && args[1]) {
await cmdProxy('long-press', { ref: args[1] });
} else if (cmd === 'launch' && args[1]) {
await cmdProxy('launch', { pkg: args[1] });
} else if (cmd === 'back') {
await cmdProxy('back');
} else if (cmd === 'home') {
await cmdProxy('home');
} else if (cmd === 'tap-xy' && args[1] && args[2]) {
await cmdProxy('tap-xy', { x: Number(args[1]), y: Number(args[2]) });
} else if (cmd === 'tap-grid' && args[1]) {
await cmdProxy('tap-grid', { cell: args[1] });
} else if (cmd === 'intent' && args[1]) {
const extras = {};
for (let i = 2; i < args.length; i++) {
if (args[i] === '--extra-string' && args[i + 1]) {
const [k, ...rest] = args[++i].split('=');
extras[k] = rest.join('=');
}
}
await cmdProxy('intent', { action: args[1], extras });
} else if (cmd === 'wait-text' && args[1]) {
await cmdProxy('wait-text', { text: args[1], timeout: parseFlag('--timeout') });
} else if (cmd === 'wait-state' && args[1] && args[2]) {
await cmdProxy('wait-state', { ref: args[1], state: args[2], timeout: parseFlag('--timeout') });
} else if (cmd === 'grid') {
await cmdProxy('grid');
} else if (cmd === 'logcat') {
await cmdProxy('logcat', { filter: parseFlag('--filter'), clear: hasFlag('--clear') });
} else if (cmd === 'setup') {
const ui = createUi();
await setupMenu(ui);
if (ui.failed) process.exit(1);
} else if (cmd === 'ios' && args[1] === 'resign') {
const ui = createUi();
await renewCert(ui);
if (ui.failed) process.exit(1);
} else if (cmd === 'ios' && args[1] === 'teardown') {
await teardown();
} else {
printUsage();
}
// --- Command implementations ---
async function cmdOpen() {
const { startDaemon } = await import('./src/daemon.js');
const { isAlive } = await import('./src/session-client.js');
const outputDir = resolve('.baremobile');
// Check for existing session
if (await isAlive(outputDir)) {
errOut('Session already running. Use `baremobile close` first.');
}
const opts = {
device: parseFlag('--device'),
platform,
};
try {
const session = await startDaemon(opts, outputDir);
if (jsonMode) {
jsonOut({ ok: true, pid: session.pid, port: session.port, platform, outputDir });
} else {
process.stdout.write(`Session started (pid ${session.pid}, port ${session.port}, platform ${platform})\n`);
process.stdout.write(`Output dir: ${outputDir}\n`);
}
} catch (err) {
errOut(err.message);
}
}
async function cmdStatus() {
const { readSession, isAlive } = await import('./src/session-client.js');
const outputDir = resolve('.baremobile');
const session = readSession(outputDir);
if (!session) {
errOut('No session found.');
}
const alive = await isAlive(outputDir);
if (alive) {
if (jsonMode) {
jsonOut({ ok: true, pid: session.pid, port: session.port, platform: session.platform || 'android', startedAt: session.startedAt });
} else {
process.stdout.write(`Session running (pid ${session.pid}, port ${session.port}, platform ${session.platform || 'android'}, started ${session.startedAt})\n`);
}
} else {
errOut(`Session stale (pid ${session.pid} not responding). Run \`baremobile close\` to clean up.`);
}
}
async function cmdProxy(command, cmdArgs) {
const { sendCommand } = await import('./src/session-client.js');
const outputDir = resolve('.baremobile');
try {
const result = await sendCommand(command, cmdArgs, outputDir);
if (!result.ok) {
errOut(result.error);
}
if (jsonMode) {
// Pass through daemon JSON directly
if (command === 'close') {
const sessionPath = join(outputDir, 'session.json');
try { unlinkSync(sessionPath); } catch { /* already gone */ }
}
jsonOut(result);
return;
}
// Human-readable output
if (result.file) {
process.stdout.write(`${result.file}\n`);
} else if (result.value !== undefined) {
process.stdout.write(JSON.stringify(result.value) + '\n');
} else if (command === 'close') {
const sessionPath = join(outputDir, 'session.json');
try { unlinkSync(sessionPath); } catch { /* already gone */ }
process.stdout.write('Session closed.\n');
} else {
process.stdout.write('ok\n');
}
} catch (err) {
if (command === 'close') {
const sessionPath = join(outputDir, 'session.json');
try { unlinkSync(sessionPath); } catch { /* already gone */ }
if (jsonMode) jsonOut({ ok: true });
else process.stdout.write('Session closed.\n');
} else {
errOut(err.message);
}
}
}
async function runDaemonInternal() {
const { runDaemon } = await import('./src/daemon.js');
const opts = {
device: parseFlag('--device'),
platform: parseFlag('--platform') || 'android',
};
const outputDir = parseFlag('--output-dir') || resolve('.baremobile');
await runDaemon(opts, outputDir);
}
// --- Flag parsing helpers ---
function parseFlag(name) {
// --name=value or --name value
for (let i = 0; i < args.length; i++) {
if (args[i].startsWith(name + '=')) return args[i].slice(name.length + 1);
if (args[i] === name && args[i + 1] && !args[i + 1].startsWith('--')) return args[i + 1];
}
return undefined;
}
function hasFlag(name) {
return args.includes(name);
}
// --- Usage ---
function printUsage() {
process.stdout.write(`baremobile -- Mobile device control for autonomous agents
Session:
baremobile open [--device=SERIAL] [--platform=android|ios]
Start session (default: android)
baremobile close Close session
baremobile status Check session status
Screen:
baremobile snapshot ARIA snapshot -> .baremobile/screen-*.yml
baremobile screenshot Screenshot -> .baremobile/screenshot-*.png
baremobile grid Screen grid info (Android only)
Interaction:
baremobile tap <ref> Tap element
baremobile tap-xy <x> <y> Tap by pixel coordinates
baremobile tap-grid <cell> Tap by grid cell (e.g. C5, Android only)
baremobile type <ref> <text> Type text (--clear to replace)
baremobile press <key> Press key (back, home, enter, ...)
baremobile scroll <ref> <direction> Scroll (up/down/left/right)
baremobile swipe <x1> <y1> <x2> <y2> [--duration=N]
baremobile long-press <ref> Long-press element
baremobile launch <pkg> Launch app by identifier
baremobile intent <action> [--extra-string key=val ...] (Android only)
baremobile back Navigate back
baremobile home Go to home screen
Waiting:
baremobile wait-text <text> [--timeout=N]
baremobile wait-state <ref> <state> [--timeout=N]
Logging:
baremobile logcat [--filter=TAG] [--clear] (Android only)
Setup:
baremobile setup Interactive setup wizard
baremobile ios resign Re-sign WDA cert (7-day Apple free cert)
baremobile ios teardown Kill iOS tunnel/WDA processes
MCP:
baremobile mcp Start MCP server (JSON-RPC over stdio)
Options:
--platform=android|ios Target platform (default: android)
--json Output JSON lines (for agent consumption)
`);
}