UNPKG

agent-device

Version:

Open source device layer for AI coding agents: a CLI, MCP server, and typed Node.js API to control mobile, web, TV, and desktop apps on devices and simulators, locally or in the cloud.

1 lines • 3.4 kB
import{v as e}from"./sdk-contracts.js";import{n as t,r as n}from"./archive.js";import{a as r,c as i,o as a,r as o,s}from"./install-source2.js";import{T as c,_ as l,h as u}from"./host-file.js";import{r as d}from"./plist.js";import f from"node:path";async function p(e,t){let n=f.join(e,`Info.plist`),[r,i,a]=await Promise.all([d(n,`CFBundleIdentifier`,t),d(n,`CFBundleDisplayName`,t),d(n,`CFBundleName`,t)]);return{bundleId:r,appName:i??a}}async function m(e,t){return await i(async()=>await h(e,t))}async function h(e,t){let n=await o({source:e,isInstallablePath:(e,t)=>t.isDirectory()&&e.toLowerCase().endsWith(`.app`)||t.isFile()&&e.toLowerCase().endsWith(`.ipa`),installableLabel:`iOS installable (.app or .ipa)`,signal:t.signal,...t.materializationDeadlineMs===void 0?{}:{materializationDeadlineMs:t.materializationDeadlineMs},environment:t.environment}),r;try{r=await _(n.installablePath,t);let e=r,i=await p(e.installPath,t?.signal),a=n.archivePath??(n.installablePath.toLowerCase().endsWith(`.ipa`)?n.installablePath:void 0),o=g(n);return{archivePath:a,installablePath:e.installPath,...o?{uploadPath:o}:{},bundleId:i.bundleId,appName:i.appName,cleanup:async()=>{await e.cleanup(),await n.cleanup()}}}catch(e){try{await r?.cleanup()}finally{await n.cleanup()}throw e}}function g(e){if(e.installablePath.toLowerCase().endsWith(`.ipa`))return e.installablePath;let{containingArchivePath:n}=e;return n&&t(n)===`zip`?n:void 0}async function _(e,t){return e.toLowerCase().endsWith(`.ipa`)?await v(e,t):{installPath:e,cleanup:async()=>{}}}async function v(t,i){let o=await u(`agent-device-ios-ipa-`),d=async()=>{await c(o)};try{let c=f.join(o,`extracted`),u=a()+1,p=r();await n({archivePath:t,outputRoot:c,type:`zip`,budget:p,depth:u,signal:i?.signal}),s(u);let m=f.join(c,`Payload`),h=(await l(m,{withFileTypes:!0}).catch(()=>{throw new e(`INVALID_ARGS`,`Invalid IPA: missing Payload directory`)})).filter(e=>e.isDirectory()&&e.name.toLowerCase().endsWith(`.app`)).map(e=>({installPath:f.join(m,e.name),bundleName:e.name.replace(/\.app$/i,``)})),g=h[0];if(g!==void 0&&h.length===1)return{installPath:g.installPath,cleanup:d};if(h.length===0)throw new e(`INVALID_ARGS`,`Invalid IPA: expected at least one .app under Payload, found 0`);await y(h,i?.signal);let _=i?.appIdentifierHint?.trim();if(_){let t=b(h,_);if(t)return{installPath:t.installPath,cleanup:d};throw new e(`INVALID_ARGS`,`Invalid IPA: found ${h.length} .app bundles under Payload and none matched "${_}". Available bundles: ${h.map(x).join(`, `)}`)}throw new e(`INVALID_ARGS`,`Invalid IPA: found ${h.length} .app bundles under Payload. Pass an app identifier or bundle name matching one of: ${h.map(x).join(`, `)}`)}catch(e){throw await d(),e}}async function y(e,t){await Promise.all(e.map(async e=>{if(e.bundleId&&e.appName)return;let n=await p(e.installPath,t);e.bundleId=e.bundleId??n.bundleId,e.appName=e.appName??n.appName}))}function b(t,n){let r=n.toLowerCase(),i=t.filter(e=>e.bundleName.toLowerCase()===r);if(i.length===1)return i[0];if(i.length>1)throw new e(`INVALID_ARGS`,`Invalid IPA: multiple app bundles matched "${n}" by name. Use a bundle id hint instead.`);if(n.includes(`.`)){let e=t.filter(e=>e.bundleId?.toLowerCase()===r);if(e.length===1)return e[0]}}function x(e){let t=e.bundleId??e.appName;return t?`${e.bundleName}.app (${t})`:`${e.bundleName}.app`}export{m as prepareIosInstallArtifact,p as readIosBundleInfo};