Skip to content

Commit 343b43d

Browse files
committed
refactor: trim lazy loading cleanup
1 parent 850dccb commit 343b43d

5 files changed

Lines changed: 43 additions & 42 deletions

File tree

src/cli/commands/connection.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,9 @@ function resolveRemoteConnectFlags(flags: CliFlags): {
149149
}
150150

151151
export const disconnectCommand: ClientCommandHandler = async ({ flags, client }) => {
152-
const session = flags.session ?? 'default';
153-
const stateDir = resolveDaemonPaths(flags.stateDir).baseDir;
154-
const state =
155-
readRemoteConnectionState({ stateDir, session }) ??
156-
(flags.session ? null : readActiveConnectionState({ stateDir }));
152+
const { session, stateDir, state } = readRequestedConnectionState(flags);
157153
if (!state) {
158-
writeCommandOutput(
159-
flags,
160-
{ connected: false, session },
161-
() => `No remote connection for "${session}".`,
162-
);
154+
writeNoRemoteConnectionOutput(flags, session);
163155
return true;
164156
}
165157
const connectedSession = state.session;
@@ -197,17 +189,9 @@ export const connectionCommand: ClientCommandHandler = async ({ positionals, fla
197189
if (positionals[0] !== 'status') {
198190
throw new AppError('INVALID_ARGS', 'connection accepts only: status');
199191
}
200-
const session = flags.session ?? 'default';
201-
const stateDir = resolveDaemonPaths(flags.stateDir).baseDir;
202-
const state =
203-
readRemoteConnectionState({ stateDir, session }) ??
204-
(flags.session ? null : readActiveConnectionState({ stateDir }));
192+
const { session, state } = readRequestedConnectionState(flags);
205193
if (!state) {
206-
writeCommandOutput(
207-
flags,
208-
{ connected: false, session },
209-
() => `No remote connection for "${session}".`,
210-
);
194+
writeNoRemoteConnectionOutput(flags, session);
211195
return true;
212196
}
213197
const leasePreparation = buildLeasePreparationNotice(state);
@@ -237,6 +221,30 @@ function createRemoteSessionName(stateDir: string): string {
237221
return `adc-${Date.now().toString(36)}-${crypto.randomBytes(2).toString('hex')}`;
238222
}
239223

224+
function readRequestedConnectionState(flags: CliFlags): {
225+
session: string;
226+
stateDir: string;
227+
state: RemoteConnectionState | null;
228+
} {
229+
const session = flags.session ?? 'default';
230+
const stateDir = resolveDaemonPaths(flags.stateDir).baseDir;
231+
return {
232+
session,
233+
stateDir,
234+
state:
235+
readRemoteConnectionState({ stateDir, session }) ??
236+
(flags.session ? null : readActiveConnectionState({ stateDir })),
237+
};
238+
}
239+
240+
function writeNoRemoteConnectionOutput(flags: CliFlags, session: string): void {
241+
writeCommandOutput(
242+
flags,
243+
{ connected: false, session },
244+
() => `No remote connection for "${session}".`,
245+
);
246+
}
247+
240248
function isCompatibleConnection(
241249
state: RemoteConnectionState,
242250
options: {

src/commands/client-command-metadata.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { MetroPrepareOptions, RecordOptions } from '../client-types.ts';
22
import type { DaemonInstallSource } from '../contracts.ts';
3-
import { getCommandDescription } from './command-descriptions.ts';
3+
import { requireCommandDescription } from './command-descriptions.ts';
44
import {
55
booleanField,
66
booleanSchema,
@@ -230,7 +230,5 @@ function defineClientCommandMetadata<
230230
const TName extends string,
231231
const TFields extends CommandFieldMap,
232232
>(name: TName, fields: TFields) {
233-
const description = getCommandDescription(name);
234-
if (!description) throw new Error(`Missing command description for ${name}`);
235-
return defineFieldCommandMetadata(name, description, fields);
233+
return defineFieldCommandMetadata(name, requireCommandDescription(name), fields);
236234
}

src/commands/command-descriptions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ const COMMAND_DESCRIPTIONS = {
4949

5050
export type DescribedCommandName = keyof typeof COMMAND_DESCRIPTIONS;
5151

52-
export function getCommandDescription(command: string): string | undefined {
52+
function getCommandDescription(command: string): string | undefined {
5353
return COMMAND_DESCRIPTIONS[command as DescribedCommandName];
5454
}
5555

56+
export function requireCommandDescription(command: string): string {
57+
const description = getCommandDescription(command);
58+
if (!description) throw new Error(`Missing command description for ${command}`);
59+
return description;
60+
}
61+
5662
export function listCommandDescriptionMetadata(): Array<{
5763
name: DescribedCommandName;
5864
description: string;

src/commands/interaction-command-metadata.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCommandDescription } from './command-descriptions.ts';
1+
import { requireCommandDescription } from './command-descriptions.ts';
22
import { defineCommandMetadata } from './command-contract.ts';
33
import {
44
booleanField,
@@ -185,19 +185,19 @@ export type GestureInput = PanInput | FlingInput | PinchInput | RotateInput | Tr
185185
export const interactionCommandMetadata = [
186186
defineCommandMetadata({
187187
name: 'click',
188-
description: descriptionFor('click'),
188+
description: requireCommandDescription('click'),
189189
inputSchema: fieldsInputSchema(clickFields),
190190
readInput: (input) => readFieldInput(input, clickFields),
191191
}),
192192
defineCommandMetadata({
193193
name: 'press',
194-
description: descriptionFor('press'),
194+
description: requireCommandDescription('press'),
195195
inputSchema: fieldsInputSchema(pressFields),
196196
readInput: (input) => readFieldInput(input, pressFields),
197197
}),
198198
defineCommandMetadata({
199199
name: 'fill',
200-
description: descriptionFor('fill'),
200+
description: requireCommandDescription('fill'),
201201
inputSchema: fieldsInputSchema(fillFields),
202202
readInput: (input) => readFieldInput(input, fillFields),
203203
}),
@@ -211,7 +211,7 @@ export const interactionCommandMetadata = [
211211
defineInteractionCommandMetadata('find', findFields),
212212
defineCommandMetadata({
213213
name: 'gesture',
214-
description: descriptionFor('gesture'),
214+
description: requireCommandDescription('gesture'),
215215
inputSchema: fieldsInputSchema(gestureFields),
216216
readInput: readGestureInput,
217217
}),
@@ -272,13 +272,7 @@ function defineInteractionCommandMetadata<
272272
const TName extends string,
273273
const TFields extends CommandFieldMap,
274274
>(name: TName, fields: TFields) {
275-
return defineFieldCommandMetadata(name, descriptionFor(name), fields);
276-
}
277-
278-
function descriptionFor(name: string): string {
279-
const description = getCommandDescription(name);
280-
if (!description) throw new Error(`Missing command description for ${name}`);
281-
return description;
275+
return defineFieldCommandMetadata(name, requireCommandDescription(name), fields);
282276
}
283277

284278
function optionalPoint(record: Record<string, unknown>, key: string): PointInput | undefined {

src/core/dispatch-resolve.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,6 @@ export async function resolveTargetDevice(flags: ResolveDeviceFlags): Promise<De
181181
);
182182
}
183183

184-
if (selector.platform === 'android') {
185-
const { ensureAdb } = await import('../platforms/android/adb.ts');
186-
await ensureAdb();
187-
}
188-
189184
const devices = await listLocalDeviceInventory({
190185
...selector,
191186
iosSimulatorSetPath,

0 commit comments

Comments
 (0)