@coreframe/scripts 0.1.31 → 0.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/dev-device.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{fs as e,os as t}from"./environment.js";import n from"node:path";import
|
|
1
|
+
import{fs as e,os as t}from"./environment.js";import{readUserPreferences as n,updateUserPreferences as r}from"./user-preferences.js";import i from"node:path";import a from"nano-spawn";import*as o from"@clack/prompts";import s from"node:process";async function c({env:e=s.env,interactive:t=s.stdin.isTTY===!0,platform:n,target:r}){let i=await p(n,e).catch(()=>[]),a=r?D(i,r):void 0;if(a)return a;if(!r&&i.length>0)return f(i,n,t);let o=await m(n,e),c=r?D(o,r):void 0;if(c)return c;if(!r&&o.length>0)return f(o,n,t);let l=n===`ios`?`iOS`:`Android`;throw Error(r?`Could not find a ${l} device matching "${r}".`:`No connected ${l} devices or available ${l} ${n===`ios`?`simulators`:`emulators`} were detected.`)}async function l({device:e,host:t,interactive:n=s.stdin.isTTY===!0,platform:r}){if(!(e.physical||r===`android`&&s.platform===`win32`)&&t===void 0)return;if(typeof t==`string`&&t!==`0.0.0.0`&&t!==`::`)return t;let i=k();if(i.length===0)throw Error(`No external IP address was detected. Pass one explicitly with --host.`);let a=await u();if(a&&i.includes(a))return a;if(i.length===1)return await d(i[0]),i[0];if(!n)throw Error(`Multiple external IP addresses were detected (${i.join(`, `)}). Pass one explicitly with --host.`);let c=await o.select({message:`Which IP address should the mobile device use to reach the development server?`,options:i.map(e=>({label:e,value:e}))});if(o.isCancel(c))throw Error(`Mobile development IP selection was cancelled.`);return await d(c),c}async function u(){return(await n()).mobileDevHost}async function d(e){try{await r({mobileDevHost:e})}catch(e){o.log.warn(`Could not remember the mobile development IP address: ${e instanceof Error?e.message:String(e)}`)}}async function f(e,t,n){if(e.length===1)return e[0];if(!n)throw Error(`Multiple ${t===`ios`?`iOS`:`Android`} devices were detected. Pass a device name explicitly.`);let r=await o.select({message:`Which ${t===`ios`?`iOS`:`Android`} device should Coreframe use?`,options:e.map(e=>({label:e.label,value:e}))});if(o.isCancel(r))throw Error(`Mobile development device selection was cancelled.`);return r}async function p(e,t){return e===`android`?h(t):_(t)}async function m(e,t){return e===`android`?g(t):v(t)}async function h(e){let t=await b(e,`platform-tools`,s.platform===`win32`?`adb.exe`:`adb`),{stdout:n}=await a(t,[`devices`],{env:e}),r=n.split(/\r?\n/).map(e=>e.match(/^(\S+)\s+device(?:\s|$)/)?.[1]).filter(e=>e!==void 0);return(await Promise.all(r.map(async n=>{let r=await S(t,e,n,`ro.product.model`),i=n.startsWith(`emulator-`),a=i?await w(t,e,n,r):await T(t,e,n,r);return{id:n,label:a===r?a:`${a} (${r})`,name:a,physical:!i}}))).sort(A)}async function g(e){let{stdout:t}=await a(await b(e,`emulator`,s.platform===`win32`?`emulator.exe`:`emulator`),[`-list-avds`],{env:e});return t.split(/\r?\n/).map(e=>e.trim()).filter(e=>e.length>0&&!/^(INFO|WARNING|ERROR)\s+\|/.test(e)).map(e=>({id:e,label:e,name:e,physical:!1}))}async function _(n){let r=await e.mkdtemp(i.join(t.tmpdir(),`coreframe-ios-devices-`)),o=i.join(r,`devices.json`);try{return await e.writeFile(o,``),await a(`xcrun`,[`devicectl`,`list`,`devices`,`--json-output`,o],{env:n}),(JSON.parse(await e.readFile(o,`utf8`)).result?.devices??[]).filter(y).map(e=>({id:e.hardwareProperties.udid,label:`${e.deviceProperties.name} (${e.hardwareProperties.productType})`,name:e.deviceProperties.name,physical:!0})).sort(A)}finally{await e.rm(r,{force:!0,recursive:!0})}}async function v(e){let{stdout:t}=await a(`xcrun`,[`simctl`,`list`,`--json`,`devices`,`available`],{env:e}),n=JSON.parse(t);return Object.entries(n.devices??{}).filter(([e])=>e.includes(`iOS-`)).flatMap(([,e])=>e).flatMap(e=>e.name?[{id:e.udid,label:e.name,name:e.name,physical:!1}]:[]).sort(A)}function y(e){return e.connectionProperties?.tunnelState!==`unavailable`&&typeof e.hardwareProperties?.platform==`string`&&(e.hardwareProperties.platform.includes(`iOS`)||e.hardwareProperties.platform.includes(`xrOS`))&&typeof e.deviceProperties?.name==`string`&&typeof e.hardwareProperties.productType==`string`&&typeof e.hardwareProperties.udid==`string`}async function b(t,n,r){let a=t.ANDROID_HOME??t.ANDROID_SDK_ROOT,o=a?[a]:x(t);for(let t of o){let a=i.join(t,n,r);try{return await e.access(a),a}catch{}}return r}function x(e){return s.platform===`darwin`?[i.join(t.homedir(),`Library`,`Android`,`sdk`)]:s.platform===`win32`&&e.LOCALAPPDATA?[i.join(e.LOCALAPPDATA,`Android`,`Sdk`)]:[i.join(t.homedir(),`Android`,`Sdk`)]}async function S(e,t,n,r){return(await C(e,t,[`-s`,n,`shell`,`getprop`,r])).trim()}async function C(e,t,n){return(await a(e,n,{env:t,timeout:3e3})).stdout}async function w(e,t,n,r){return C(e,t,[`-s`,n,`emu`,`avd`,`name`]).then(e=>e.split(/\r?\n/)[0]?.trim()||r).catch(()=>r)}async function T(e,t,n,r){return C(e,t,[`-s`,n,`shell`,`dumpsys`,`bluetooth_manager`]).then(e=>E(e)||r).catch(()=>r)}function E(e){return e.match(/\bname: (.+)/)?.[1]?.trim()}function D(e,t){let n=e.map(e=>({device:e,score:Math.max(O(t,e.name),O(t,e.id??``))})).sort((e,t)=>t.score-e.score)[0];return n&&n.score>0?n.device:void 0}function O(e,t){let n=e.toLowerCase(),r=t.toLowerCase();if(r===n)return 1e4;let i=r.indexOf(n);if(i>=0)return 5e3-i;let a=0,o=0;for(let e of n){let t=r.indexOf(e,o);if(t<0)return 0;a+=t===o?2:1,o=t+1}return a}function k(){return[...new Set(Object.values(t.networkInterfaces()).flatMap(e=>e??[]).filter(e=>!e.internal&&e.family===`IPv4`).map(e=>e.address))].sort()}function A(e,t){return e.label.localeCompare(t.label)}export{l as resolveMobileDevHost,c as selectMobileDevDevice};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region user-preferences.d.ts
|
|
2
|
+
type UserPreferences = {
|
|
3
|
+
mobileDevHost?: string;
|
|
4
|
+
};
|
|
5
|
+
declare function readUserPreferences(): Promise<UserPreferences>;
|
|
6
|
+
declare function updateUserPreferences(updates: Partial<UserPreferences>): Promise<void>;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { UserPreferences, readUserPreferences, updateUserPreferences };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{fs as e,os as t}from"./environment.js";import n from"node:path";async function r(){let e=await a();return typeof e.mobileDevHost==`string`?{mobileDevHost:e.mobileDevHost}:{}}async function i(t){let r=await a(),i=o();await e.mkdir(n.dirname(i),{recursive:!0}),await e.writeFile(i,`${JSON.stringify({...r,...t},void 0,2)}\n`)}async function a(){try{let t=await e.readFile(o(),`utf8`),n=JSON.parse(t);return s(n)?n:{}}catch{return{}}}function o(){return n.join(t.homedir(),`.coreframe`,`preferences.json`)}function s(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}export{r as readUserPreferences,i as updateUserPreferences};
|