Skip to content

Commit 24bc75b

Browse files
authored
refactor: collocate session lifecycle command metadata (#518)
1 parent 9743fc8 commit 24bc75b

6 files changed

Lines changed: 132 additions & 85 deletions

File tree

src/cli/commands/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ const dedicatedClientApiHandlers = {
2828
[CLIENT_COMMANDS.apps]: appsCommand,
2929
'ensure-simulator': ensureSimulatorCommand,
3030
metro: metroCommand,
31-
install: installCommand,
32-
reinstall: reinstallCommand,
33-
'install-from-source': installFromSourceCommand,
31+
[CLIENT_COMMANDS.install]: installCommand,
32+
[CLIENT_COMMANDS.reinstall]: reinstallCommand,
33+
[CLIENT_COMMANDS.installFromSource]: installFromSourceCommand,
3434
connect: connectCommand,
3535
disconnect: disconnectCommand,
3636
connection: connectionCommand,
3737
auth: authCommand,
38-
open: openCommand,
39-
close: closeCommand,
38+
[CLIENT_COMMANDS.open]: openCommand,
39+
[CLIENT_COMMANDS.close]: closeCommand,
4040
[CLIENT_COMMANDS.snapshot]: snapshotCommand,
4141
[CLIENT_COMMANDS.screenshot]: screenshotCommand,
4242
[CLIENT_COMMANDS.diff]: diffCommand,

src/command-catalog.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const PUBLIC_COMMANDS = {
77
batch: 'batch',
88
boot: 'boot',
99
click: 'click',
10+
close: 'close',
1011
clipboard: 'clipboard',
1112
devices: 'devices',
1213
diff: 'diff',
@@ -15,16 +16,20 @@ export const PUBLIC_COMMANDS = {
1516
focus: 'focus',
1617
get: 'get',
1718
home: 'home',
19+
install: 'install',
20+
installFromSource: 'install-from-source',
1821
is: 'is',
1922
keyboard: 'keyboard',
2023
logs: 'logs',
2124
longPress: 'longpress',
2225
network: 'network',
26+
open: 'open',
2327
perf: 'perf',
2428
pinch: 'pinch',
2529
press: 'press',
2630
push: 'push',
2731
record: 'record',
32+
reinstall: 'reinstall',
2833
replay: 'replay',
2934
rotate: 'rotate',
3035
scroll: 'scroll',

src/commands/interactions/__tests__/definition.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import { getCommandCapability } from '../../../core/capabilities.ts';
44
import { getCommandSchema } from '../../../utils/command-schema.ts';
55
import { CAPTURE_COMMAND_DEFINITIONS } from '../../capture-definition.ts';
66
import { SELECTOR_COMMAND_DEFINITIONS } from '../../selectors-definition.ts';
7+
import { SESSION_LIFECYCLE_COMMAND_DEFINITIONS } from '../../session-lifecycle/definition.ts';
78
import { INTERACTION_COMMAND_DEFINITIONS } from '../definition.ts';
89

910
test('command definitions feed schema and capability registries', () => {
1011
for (const definition of [
1112
...INTERACTION_COMMAND_DEFINITIONS,
1213
...CAPTURE_COMMAND_DEFINITIONS,
1314
...SELECTOR_COMMAND_DEFINITIONS,
15+
...SESSION_LIFECYCLE_COMMAND_DEFINITIONS,
1416
]) {
1517
assert.deepEqual(getCommandSchema(definition.name), definition.schema);
1618
assert.deepEqual(getCommandCapability(definition.name), definition.capability);
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { PUBLIC_COMMANDS } from '../../command-catalog.ts';
2+
import type { CommandCapability } from '../../core/capabilities.ts';
3+
import { commandCapabilityMap, commandSchemaMap, defineCommand } from '../command-definition.ts';
4+
5+
const APP_RUNTIME_CAPABILITY = {
6+
apple: { simulator: true, device: true },
7+
android: { emulator: true, device: true, unknown: true },
8+
linux: { device: true },
9+
} as const satisfies CommandCapability;
10+
11+
const APP_INVENTORY_CAPABILITY = {
12+
apple: { simulator: true, device: true },
13+
android: { emulator: true, device: true, unknown: true },
14+
linux: {},
15+
} as const satisfies CommandCapability;
16+
17+
const APP_INSTALL_CAPABILITY = {
18+
apple: { simulator: true, device: true },
19+
android: { emulator: true, device: true, unknown: true },
20+
linux: {},
21+
supports: (device) => device.platform !== 'macos',
22+
} as const satisfies CommandCapability;
23+
24+
const openCommandDefinition = defineCommand({
25+
name: PUBLIC_COMMANDS.open,
26+
schema: {
27+
helpDescription:
28+
'Boot device/simulator; optionally launch app or deep link URL (macOS also supports --surface app|frontmost-app|desktop|menubar)',
29+
summary: 'Open an app, deep link or URL, save replays',
30+
positionalArgs: ['appOrUrl?', 'url?'],
31+
allowedFlags: ['activity', 'saveScript', 'relaunch', 'surface'],
32+
},
33+
capability: APP_RUNTIME_CAPABILITY,
34+
});
35+
36+
const closeCommandDefinition = defineCommand({
37+
name: PUBLIC_COMMANDS.close,
38+
schema: {
39+
helpDescription: 'Close app or just end session',
40+
summary: 'Close app or end session',
41+
positionalArgs: ['app?'],
42+
allowedFlags: ['saveScript', 'shutdown'],
43+
},
44+
capability: APP_RUNTIME_CAPABILITY,
45+
});
46+
47+
const reinstallCommandDefinition = defineCommand({
48+
name: PUBLIC_COMMANDS.reinstall,
49+
schema: {
50+
helpDescription: 'Uninstall + install app from binary path',
51+
summary: 'Reinstall app from binary path',
52+
positionalArgs: ['app', 'path'],
53+
allowedFlags: [],
54+
},
55+
capability: APP_INSTALL_CAPABILITY,
56+
});
57+
58+
const installCommandDefinition = defineCommand({
59+
name: PUBLIC_COMMANDS.install,
60+
schema: {
61+
helpDescription: 'Install app from binary path without uninstalling first',
62+
summary: 'Install app from binary path',
63+
positionalArgs: ['app', 'path'],
64+
allowedFlags: [],
65+
},
66+
capability: APP_INSTALL_CAPABILITY,
67+
});
68+
69+
const installFromSourceCommandDefinition = defineCommand({
70+
name: PUBLIC_COMMANDS.installFromSource,
71+
schema: {
72+
usageOverride:
73+
'install-from-source <url> | install-from-source --github-actions-artifact <owner/repo:artifact>',
74+
listUsageOverride: 'install-from-source <url> | install-from-source --github-actions-artifact',
75+
helpDescription: 'Install app from a URL or remote-resolved source',
76+
summary: 'Install app from a source',
77+
positionalArgs: ['url?'],
78+
allowedFlags: [
79+
'header',
80+
'githubActionsArtifact',
81+
'installSource',
82+
'retainPaths',
83+
'retentionMs',
84+
],
85+
},
86+
capability: APP_INSTALL_CAPABILITY,
87+
});
88+
89+
const appsCommandDefinition = defineCommand({
90+
name: PUBLIC_COMMANDS.apps,
91+
schema: {
92+
helpDescription: 'List installed apps (includes default/system apps by default)',
93+
summary: 'List installed apps',
94+
positionalArgs: [],
95+
allowedFlags: ['appsFilter'],
96+
defaults: { appsFilter: 'all' },
97+
},
98+
capability: APP_INVENTORY_CAPABILITY,
99+
});
100+
101+
export const SESSION_LIFECYCLE_COMMAND_DEFINITIONS = [
102+
openCommandDefinition,
103+
closeCommandDefinition,
104+
reinstallCommandDefinition,
105+
installCommandDefinition,
106+
installFromSourceCommandDefinition,
107+
appsCommandDefinition,
108+
] as const;
109+
110+
export const SESSION_LIFECYCLE_COMMAND_SCHEMAS = commandSchemaMap(
111+
SESSION_LIFECYCLE_COMMAND_DEFINITIONS,
112+
);
113+
114+
export const SESSION_LIFECYCLE_COMMAND_CAPABILITIES = commandCapabilityMap(
115+
SESSION_LIFECYCLE_COMMAND_DEFINITIONS,
116+
);

src/core/capabilities.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { isApplePlatform, type DeviceInfo } from '../utils/device.ts';
22
import { CAPTURE_COMMAND_CAPABILITIES } from '../commands/capture-definition.ts';
33
import { INTERACTION_COMMAND_CAPABILITIES } from '../commands/interactions/definition.ts';
44
import { SELECTOR_COMMAND_CAPABILITIES } from '../commands/selectors-definition.ts';
5+
import { SESSION_LIFECYCLE_COMMAND_CAPABILITIES } from '../commands/session-lifecycle/definition.ts';
56

67
type KindMatrix = {
78
simulator?: boolean;
@@ -52,11 +53,7 @@ const COMMAND_CAPABILITY_MATRIX: Record<string, CommandCapability> = {
5253
linux: LINUX_NONE,
5354
supports: isNotMacOs,
5455
},
55-
apps: {
56-
apple: { simulator: true, device: true },
57-
android: { emulator: true, device: true, unknown: true },
58-
linux: LINUX_NONE,
59-
},
56+
...SESSION_LIFECYCLE_COMMAND_CAPABILITIES,
6057
back: {
6158
apple: { simulator: true, device: true },
6259
android: { emulator: true, device: true, unknown: true },
@@ -91,11 +88,6 @@ const COMMAND_CAPABILITY_MATRIX: Record<string, CommandCapability> = {
9188
supports: (device) =>
9289
device.platform === 'android' || (device.platform === 'ios' && device.target !== 'tv'),
9390
},
94-
close: {
95-
apple: { simulator: true, device: true },
96-
android: { emulator: true, device: true, unknown: true },
97-
linux: LINUX_DEVICE,
98-
},
9991
fill: {
10092
apple: { simulator: true, device: true },
10193
android: { emulator: true, device: true, unknown: true },
@@ -129,34 +121,11 @@ const COMMAND_CAPABILITY_MATRIX: Record<string, CommandCapability> = {
129121
android: { emulator: true, device: true, unknown: true },
130122
linux: LINUX_DEVICE,
131123
},
132-
open: {
133-
apple: { simulator: true, device: true },
134-
android: { emulator: true, device: true, unknown: true },
135-
linux: LINUX_DEVICE,
136-
},
137124
perf: {
138125
apple: { simulator: true, device: true },
139126
android: { emulator: true, device: true, unknown: true },
140127
linux: LINUX_NONE,
141128
},
142-
install: {
143-
apple: { simulator: true, device: true },
144-
android: { emulator: true, device: true, unknown: true },
145-
linux: LINUX_NONE,
146-
supports: isNotMacOs,
147-
},
148-
'install-from-source': {
149-
apple: { simulator: true, device: true },
150-
android: { emulator: true, device: true, unknown: true },
151-
linux: LINUX_NONE,
152-
supports: isNotMacOs,
153-
},
154-
reinstall: {
155-
apple: { simulator: true, device: true },
156-
android: { emulator: true, device: true, unknown: true },
157-
linux: LINUX_NONE,
158-
supports: isNotMacOs,
159-
},
160129
press: {
161130
apple: { simulator: true, device: true },
162131
android: { emulator: true, device: true, unknown: true },

src/utils/command-schema.ts

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
SELECTOR_COMMAND_SCHEMAS,
99
SELECTOR_SNAPSHOT_FLAGS,
1010
} from '../commands/selectors-definition.ts';
11+
import { SESSION_LIFECYCLE_COMMAND_SCHEMAS } from '../commands/session-lifecycle/definition.ts';
1112

1213
export type CliFlags = RemoteConfigMetroOptions & {
1314
json: boolean;
@@ -1417,13 +1418,7 @@ const COMMAND_SCHEMAS: Record<string, CommandSchema> = {
14171418
positionalArgs: [],
14181419
allowedFlags: ['headless'],
14191420
},
1420-
open: {
1421-
helpDescription:
1422-
'Boot device/simulator; optionally launch app or deep link URL (macOS also supports --surface app|frontmost-app|desktop|menubar)',
1423-
summary: 'Open an app, deep link or URL, save replays',
1424-
positionalArgs: ['appOrUrl?', 'url?'],
1425-
allowedFlags: ['activity', 'saveScript', 'relaunch', 'surface'],
1426-
},
1421+
...SESSION_LIFECYCLE_COMMAND_SCHEMAS,
14271422
connect: {
14281423
usageOverride:
14291424
'connect [--remote-config <path>] [--tenant <id>] [--run-id <id>] [--lease-backend <backend>] [--force] [--no-login]',
@@ -1485,39 +1480,6 @@ const COMMAND_SCHEMAS: Record<string, CommandSchema> = {
14851480
allowedFlags: [],
14861481
skipCapabilityCheck: true,
14871482
},
1488-
close: {
1489-
helpDescription: 'Close app or just end session',
1490-
summary: 'Close app or end session',
1491-
positionalArgs: ['app?'],
1492-
allowedFlags: ['saveScript', 'shutdown'],
1493-
},
1494-
reinstall: {
1495-
helpDescription: 'Uninstall + install app from binary path',
1496-
summary: 'Reinstall app from binary path',
1497-
positionalArgs: ['app', 'path'],
1498-
allowedFlags: [],
1499-
},
1500-
install: {
1501-
helpDescription: 'Install app from binary path without uninstalling first',
1502-
summary: 'Install app from binary path',
1503-
positionalArgs: ['app', 'path'],
1504-
allowedFlags: [],
1505-
},
1506-
'install-from-source': {
1507-
usageOverride:
1508-
'install-from-source <url> | install-from-source --github-actions-artifact <owner/repo:artifact>',
1509-
listUsageOverride: 'install-from-source <url> | install-from-source --github-actions-artifact',
1510-
helpDescription: 'Install app from a URL or remote-resolved source',
1511-
summary: 'Install app from a source',
1512-
positionalArgs: ['url?'],
1513-
allowedFlags: [
1514-
'header',
1515-
'githubActionsArtifact',
1516-
'installSource',
1517-
'retainPaths',
1518-
'retentionMs',
1519-
],
1520-
},
15211483
push: {
15221484
helpDescription: 'Simulate push notification payload delivery',
15231485
summary: 'Deliver push payload',
@@ -1538,13 +1500,6 @@ const COMMAND_SCHEMAS: Record<string, CommandSchema> = {
15381500
allowedFlags: [],
15391501
skipCapabilityCheck: true,
15401502
},
1541-
apps: {
1542-
helpDescription: 'List installed apps (includes default/system apps by default)',
1543-
summary: 'List installed apps',
1544-
positionalArgs: [],
1545-
allowedFlags: ['appsFilter'],
1546-
defaults: { appsFilter: 'all' },
1547-
},
15481503
appstate: {
15491504
helpDescription: 'Show foreground app/activity',
15501505
positionalArgs: [],

0 commit comments

Comments
 (0)