(
options: SimDeckLaunchOptions = {},
)
| 221 | }; |
| 222 | |
| 223 | export async function connect( |
| 224 | options: SimDeckLaunchOptions = {}, |
| 225 | ): Promise<SimDeckSession> { |
| 226 | const cliPath = options.cliPath ?? "simdeck"; |
| 227 | const result: ServiceConnection = options.isolated |
| 228 | ? await startIsolatedService(cliPath, options) |
| 229 | : runJson<ServiceStartResult>(cliPath, ["service", "start"], { |
| 230 | cwd: options.projectRoot, |
| 231 | }); |
| 232 | const endpoint = result.url; |
| 233 | const createSession = (defaultUdid?: string): SimDeckSession => { |
| 234 | const simulatorPath = (udid: string, suffix: string) => |
| 235 | `/api/simulators/${encodeURIComponent(udid)}${suffix}`; |
| 236 | const actionPath = (udid: string) => simulatorPath(udid, "/action"); |
| 237 | const requestAction = <T = unknown>(udid: string, body: unknown) => |
| 238 | requestJson<T>(endpoint, "POST", actionPath(udid), body); |
| 239 | const requestActionOk = (udid: string, body: unknown) => |
| 240 | requestAction(udid, body).then(() => undefined); |
| 241 | const requireUdid = (udid?: string) => { |
| 242 | const resolved = udid ?? defaultUdid; |
| 243 | if (!resolved) { |
| 244 | throw new Error( |
| 245 | "This SimDeck session method requires a UDID. Pass one as the first argument or call connect({ udid }).", |
| 246 | ); |
| 247 | } |
| 248 | return resolved; |
| 249 | }; |
| 250 | const resolveNoArgDeviceCall = (args: unknown[]) => ({ |
| 251 | udid: requireUdid(typeof args[0] === "string" ? args[0] : undefined), |
| 252 | }); |
| 253 | const resolveStringArgDeviceCall = (args: unknown[]) => { |
| 254 | if ( |
| 255 | args.length >= 2 && |
| 256 | typeof args[0] === "string" && |
| 257 | typeof args[1] === "string" |
| 258 | ) { |
| 259 | return { udid: args[0], value: args[1] as string, rest: args.slice(2) }; |
| 260 | } |
| 261 | return { |
| 262 | udid: requireUdid(), |
| 263 | value: args[0] as string, |
| 264 | rest: args.slice(1), |
| 265 | }; |
| 266 | }; |
| 267 | const resolveObjectArgDeviceCall = <T>(args: unknown[]) => { |
| 268 | if (typeof args[0] === "string") { |
| 269 | return { udid: args[0], value: args[1] as T, rest: args.slice(2) }; |
| 270 | } |
| 271 | return { udid: requireUdid(), value: args[0] as T, rest: args.slice(1) }; |
| 272 | }; |
| 273 | const resolveOptionalObjectDeviceCall = <T>(args: unknown[]) => { |
| 274 | if (typeof args[0] === "string") { |
| 275 | return { udid: args[0], options: args[1] as T | undefined }; |
| 276 | } |
| 277 | return { udid: requireUdid(), options: args[0] as T | undefined }; |
| 278 | }; |
| 279 | const session: SimDeckSession = { |
| 280 | endpoint, |
no test coverage detected