UNPKG

@shopify/cli

Version:

A CLI tool to build for the Shopify platform

2 lines (1 loc) 1.16 kB
import{P as m}from"./chunk-6G6TMKXF.js";import{d as c}from"./chunk-M56NDIMD.js";import{Gb as l,Ib as n,Qb as a}from"./chunk-N5PQPIBF.js";import{g as s}from"./chunk-VPRTJUIN.js";s();import{createServer as f}from"net";var u=new Set;async function P(r,t){if(r&&await g(r))return a(n`Port ${r.toString()} is free`),r;a(n`Getting a random port...`);let e=await w(()=>d(),t?.maxTries,t?.waitTimeInSeconds);for(let o=0;o<(t?.maxTries??5)&&u.has(e);o++)e=await w(()=>d(),t?.maxTries,t?.waitTimeInSeconds);return a(n`Random port obtained: ${l.raw(`${e}`)}`),u.add(e),e}async function g(r){return new Promise(t=>{let e=f();e.unref(),e.once("error",()=>t(!1)),e.listen(r,"localhost",()=>{e.close(()=>t(!0))})})}function d(){return new Promise((r,t)=>{let e=f();e.unref(),e.once("error",t),e.listen(0,"localhost",()=>{let o=e.address();if(o&&typeof o=="object"){let i=o.port;e.close(()=>r(i))}else e.close(()=>t(new Error("Unable to determine assigned port")))})})}async function w(r,t=5,e=1){let o=1;for(;;)try{return await r()}catch(i){if(o++<t)a(n`Unknown problem getting a random port: ${i.message}`),await m(e);else throw new c(i.message)}}export{P as a,g as b};