|
1 | 1 | import type { |
2 | | - AlertCommandOptions, |
3 | 2 | AppStateCommandResult, |
4 | | - ClipboardCommandOptions, |
5 | 3 | ClipboardCommandResult, |
6 | | - KeyboardCommandOptions, |
7 | 4 | KeyboardCommandResult, |
8 | | - RotateCommandOptions, |
9 | 5 | } from '../../client.ts'; |
10 | 6 | import type { CliFlags } from '../../utils/command-schema.ts'; |
11 | | -import { AppError } from '../../utils/errors.ts'; |
12 | 7 | import { readCommandMessage } from '../../utils/success-text.ts'; |
13 | | -import { waitCommandCodec } from '../../command-codecs.ts'; |
14 | | -import { parseDeviceRotation } from '../../core/device-rotation.ts'; |
15 | | -import { buildSelectionOptions, writeCommandMessage, writeCommandOutput } from './shared.ts'; |
| 8 | +import { runSemanticCliCommand, type SemanticCliCommand } from '../../commands/semantic-cli.ts'; |
| 9 | +import { writeCommandMessage, writeCommandOutput } from './shared.ts'; |
16 | 10 | import type { ClientCommandHandlerMap } from './router-types.ts'; |
17 | 11 |
|
18 | 12 | export const clientCommandMethodHandlers = { |
19 | 13 | wait: async ({ positionals, flags, client }) => { |
20 | 14 | writeCommandMessage( |
21 | 15 | flags, |
22 | | - await client.command.wait(waitCommandCodec.decode(positionals, flags)), |
| 16 | + await runClientCommand({ command: 'wait', positionals, flags, client }), |
23 | 17 | ); |
24 | 18 | return true; |
25 | 19 | }, |
26 | 20 | alert: async ({ positionals, flags, client }) => { |
27 | | - writeCommandMessage(flags, await client.command.alert(readAlertOptions(positionals, flags))); |
| 21 | + writeCommandMessage( |
| 22 | + flags, |
| 23 | + await runClientCommand({ command: 'alert', positionals, flags, client }), |
| 24 | + ); |
28 | 25 | return true; |
29 | 26 | }, |
30 | 27 | appstate: async ({ flags, client }) => { |
31 | | - const result = await client.command.appState(buildSelectionOptions(flags)); |
| 28 | + const result = (await runClientCommand({ |
| 29 | + command: 'appstate', |
| 30 | + positionals: [], |
| 31 | + flags, |
| 32 | + client, |
| 33 | + })) as AppStateCommandResult; |
32 | 34 | writeCommandOutput(flags, result, () => formatAppState(result)); |
33 | 35 | return true; |
34 | 36 | }, |
35 | 37 | back: async ({ flags, client }) => { |
36 | 38 | writeCommandMessage( |
37 | 39 | flags, |
38 | | - await client.command.back({ ...buildSelectionOptions(flags), mode: flags.backMode }), |
| 40 | + await runClientCommand({ command: 'back', positionals: [], flags, client }), |
39 | 41 | ); |
40 | 42 | return true; |
41 | 43 | }, |
42 | 44 | home: async ({ flags, client }) => { |
43 | | - writeCommandMessage(flags, await client.command.home(buildSelectionOptions(flags))); |
| 45 | + writeCommandMessage( |
| 46 | + flags, |
| 47 | + await runClientCommand({ command: 'home', positionals: [], flags, client }), |
| 48 | + ); |
44 | 49 | return true; |
45 | 50 | }, |
46 | 51 | rotate: async ({ positionals, flags, client }) => { |
47 | | - writeCommandMessage(flags, await client.command.rotate(readRotateOptions(positionals, flags))); |
| 52 | + writeCommandMessage( |
| 53 | + flags, |
| 54 | + await runClientCommand({ command: 'rotate', positionals, flags, client }), |
| 55 | + ); |
48 | 56 | return true; |
49 | 57 | }, |
50 | 58 | 'app-switcher': async ({ flags, client }) => { |
51 | | - writeCommandMessage(flags, await client.command.appSwitcher(buildSelectionOptions(flags))); |
| 59 | + writeCommandMessage( |
| 60 | + flags, |
| 61 | + await runClientCommand({ command: 'app-switcher', positionals: [], flags, client }), |
| 62 | + ); |
52 | 63 | return true; |
53 | 64 | }, |
54 | 65 | keyboard: async ({ positionals, flags, client }) => { |
55 | 66 | writeKeyboardOutput( |
56 | 67 | flags, |
57 | | - await client.command.keyboard(readKeyboardOptions(positionals, flags)), |
| 68 | + (await runClientCommand({ |
| 69 | + command: 'keyboard', |
| 70 | + positionals, |
| 71 | + flags, |
| 72 | + client, |
| 73 | + })) as KeyboardCommandResult, |
58 | 74 | ); |
59 | 75 | return true; |
60 | 76 | }, |
61 | 77 | clipboard: async ({ positionals, flags, client }) => { |
62 | 78 | writeClipboardOutput( |
63 | 79 | flags, |
64 | | - await client.command.clipboard(readClipboardOptions(positionals, flags)), |
| 80 | + (await runClientCommand({ |
| 81 | + command: 'clipboard', |
| 82 | + positionals, |
| 83 | + flags, |
| 84 | + client, |
| 85 | + })) as ClipboardCommandResult, |
65 | 86 | ); |
66 | 87 | return true; |
67 | 88 | }, |
68 | 89 | } satisfies ClientCommandHandlerMap; |
69 | 90 |
|
70 | | -function readAlertOptions(positionals: string[], flags: CliFlags): AlertCommandOptions { |
71 | | - if (positionals.length > 2) { |
72 | | - throw new AppError('INVALID_ARGS', 'alert accepts at most action and timeout arguments.'); |
73 | | - } |
74 | | - const action = readAlertAction(positionals[0]); |
75 | | - const timeoutMs = readFiniteNumber(positionals[1], 'alert timeout'); |
76 | | - return { |
77 | | - ...buildSelectionOptions(flags), |
78 | | - ...(action ? { action } : {}), |
79 | | - ...(timeoutMs !== undefined ? { timeoutMs } : {}), |
80 | | - }; |
81 | | -} |
82 | | - |
83 | | -function readRotateOptions(positionals: string[], flags: CliFlags): RotateCommandOptions { |
84 | | - if (positionals.length > 1) { |
85 | | - throw new AppError('INVALID_ARGS', 'rotate accepts exactly one orientation argument.'); |
86 | | - } |
87 | | - return { |
88 | | - ...buildSelectionOptions(flags), |
89 | | - orientation: parseDeviceRotation(positionals[0]), |
90 | | - }; |
91 | | -} |
92 | | - |
93 | | -function readKeyboardOptions(positionals: string[], flags: CliFlags): KeyboardCommandOptions { |
94 | | - if (positionals.length > 1) { |
95 | | - throw new AppError('INVALID_ARGS', 'keyboard accepts at most one action argument.'); |
96 | | - } |
97 | | - const action = readKeyboardAction(positionals[0]); |
98 | | - return { |
99 | | - ...buildSelectionOptions(flags), |
100 | | - ...(action ? { action } : {}), |
101 | | - }; |
| 91 | +function runClientCommand(options: { |
| 92 | + command: Extract< |
| 93 | + SemanticCliCommand, |
| 94 | + | 'wait' |
| 95 | + | 'alert' |
| 96 | + | 'appstate' |
| 97 | + | 'back' |
| 98 | + | 'home' |
| 99 | + | 'rotate' |
| 100 | + | 'app-switcher' |
| 101 | + | 'keyboard' |
| 102 | + | 'clipboard' |
| 103 | + >; |
| 104 | + positionals: string[]; |
| 105 | + flags: CliFlags; |
| 106 | + client: Parameters<typeof runSemanticCliCommand>[0]['client']; |
| 107 | +}) { |
| 108 | + return runSemanticCliCommand(options); |
102 | 109 | } |
103 | 110 |
|
104 | 111 | function writeKeyboardOutput(flags: CliFlags, result: KeyboardCommandResult): void { |
@@ -132,60 +139,6 @@ function androidKeyboardNextAction( |
132 | 139 | return 'Keyboard is hidden; focus an app field before type, or use fill with a concrete target.'; |
133 | 140 | } |
134 | 141 |
|
135 | | -function readClipboardOptions(positionals: string[], flags: CliFlags): ClipboardCommandOptions { |
136 | | - const action = positionals[0]?.toLowerCase(); |
137 | | - if (action !== 'read' && action !== 'write') { |
138 | | - throw new AppError('INVALID_ARGS', 'clipboard requires a subcommand: read or write.'); |
139 | | - } |
140 | | - const base = buildSelectionOptions(flags); |
141 | | - if (action === 'read') { |
142 | | - if (positionals.length !== 1) { |
143 | | - throw new AppError('INVALID_ARGS', 'clipboard read does not accept additional arguments.'); |
144 | | - } |
145 | | - return { ...base, action }; |
146 | | - } |
147 | | - if (positionals.length < 2) { |
148 | | - throw new AppError('INVALID_ARGS', 'clipboard write requires text.'); |
149 | | - } |
150 | | - return { |
151 | | - ...base, |
152 | | - action, |
153 | | - text: positionals.slice(1).join(' '), |
154 | | - }; |
155 | | -} |
156 | | - |
157 | | -function readAlertAction(value: string | undefined): AlertCommandOptions['action'] | undefined { |
158 | | - const action = value?.toLowerCase(); |
159 | | - if ( |
160 | | - action === undefined || |
161 | | - action === 'get' || |
162 | | - action === 'accept' || |
163 | | - action === 'dismiss' || |
164 | | - action === 'wait' |
165 | | - ) { |
166 | | - return action; |
167 | | - } |
168 | | - throw new AppError('INVALID_ARGS', 'alert action must be get, accept, dismiss, or wait.'); |
169 | | -} |
170 | | - |
171 | | -function readKeyboardAction( |
172 | | - value: string | undefined, |
173 | | -): KeyboardCommandOptions['action'] | undefined { |
174 | | - const action = value?.toLowerCase(); |
175 | | - if (action === 'get') return 'status'; |
176 | | - if (action === undefined || action === 'status' || action === 'dismiss') { |
177 | | - return action; |
178 | | - } |
179 | | - throw new AppError('INVALID_ARGS', 'keyboard action must be status, get, or dismiss.'); |
180 | | -} |
181 | | - |
182 | | -function readFiniteNumber(value: string | undefined, label: string): number | undefined { |
183 | | - if (value === undefined) return undefined; |
184 | | - const parsed = Number(value); |
185 | | - if (Number.isFinite(parsed)) return parsed; |
186 | | - throw new AppError('INVALID_ARGS', `${label} must be a finite number.`); |
187 | | -} |
188 | | - |
189 | 142 | function formatAppState(data: AppStateCommandResult): string | null { |
190 | 143 | if (data.platform === 'ios') { |
191 | 144 | const lines = [`Foreground app: ${data.appName ?? data.appBundleId ?? 'unknown'}`]; |
|
0 commit comments