@shopify/cli
Version:
A CLI tool to build for the Shopify platform
3 lines (2 loc) • 5.69 kB
JavaScript
import{a as k,b as N,c as w}from"./chunk-UYXEA3Y6.js";import{n as F}from"./chunk-XONFGLJQ.js";import{a as T,b as D}from"./chunk-XULPJ6UG.js";import{O as A,P,a as O}from"./chunk-6G6TMKXF.js";import{g as b}from"./chunk-M56NDIMD.js";import{Ia as _,La as U,Pa as I,Qb as u,Ua as p,xa as x,za as y}from"./chunk-N5PQPIBF.js";import{f as h}from"./chunk-YTNDFQJT.js";import{d as m,i as l,j as S}from"./chunk-IU2ZQ6TE.js";import{g as f}from"./chunk-VPRTJUIN.js";f();f();import{fileURLToPath as M}from"url";import Y from"util";import{pipeline as j}from"stream";import{execSync as q,execFileSync as z}from"child_process";var v="2024.8.2",B=`https://github.com/cloudflare/cloudflared/releases/download/${v}/`,W={arm64:"cloudflared-linux-arm64",arm:"cloudflared-linux-arm",x64:"cloudflared-linux-amd64",ia32:"cloudflared-linux-386"},G={arm64:"cloudflared-darwin-arm64.tgz",x64:"cloudflared-darwin-amd64.tgz"},V={x64:"cloudflared-windows-amd64.exe",ia32:"cloudflared-windows-386.exe",arm64:"cloudflared-windows-amd64.exe"},$={linux:W,darwin:G,win32:V};function X(e=process.platform,t=process.arch){if(!Object.keys($).includes(e))throw new Error(`Unsupported system platform: ${e}`);let n=$[e][t];if(n===void 0)throw new Error(`Unsupported system arch: ${t}`);return B+n}function Z(e=process.env,t=process.platform){return e.SHOPIFY_CLI_CLOUDFLARED_PATH?e.SHOPIFY_CLI_CLOUDFLARED_PATH:m(l(M(import.meta.url)),"..","bin",t==="win32"?"cloudflared.exe":"cloudflared")}async function g(e=process.env,t=process.platform,r=process.arch){if(e.SHOPIFY_CLI_IGNORE_CLOUDFLARED){u("Skipping cloudflared installation because SHOPIFY_CLI_IGNORE_CLOUDFLARED is set");return}let n=X(t,r),o=Z(e,t);if(p(o))try{let a=z(o,["--version"],{encoding:"utf8"}).split(" "),i=a.length>2?a[2]:"0.0.0";if(!J(v,i)){u("cloudflared already installed, skipping");return}}catch{u("version check failed, reinstalling")}if(t==="linux")await K(n,o);else if(t==="darwin")await tt(n,o);else if(t==="win32")await Q(n,o);else throw new Error(`Unsupported platform: ${t}`)}function J(e,t){let[r,n,o]=e.split(".").map(Number),[a,i,c]=t.split(".").map(Number);return r!==a?(r??0)>(a??0):n!==i?(n??0)>(i??0):(o??0)>(c??0)}async function K(e,t){await C(e,t),await I(t,"755")}async function Q(e,t){await C(e,t)}async function tt(e,t){await C(e,`${t}.tgz`);let r=S(`${t}.tgz`);q(`tar -xzf ${r}`,{cwd:l(t)}),_(`${t}.tgz`),await y(`${l(t)}/cloudflared`,t)}async function C(e,t){p(l(t))||x(l(t));let r=Y.promisify(j),n=await F(e,{redirect:"follow"},"slow-request");if(!n.ok||!n.body)throw new Error(`Couldn't download file ${e} (${n.status} ${n.statusText})`);let o=U(t);return await r(n.body,o),t}import{Writable as et}from"stream";import{fileURLToPath as rt}from"url";var kt=N({provider:w,action:st}),nt=h()?.2:40,ot=5;async function st(e){try{let t=new R(e);return await t.startTunnel(),T(t)}catch(t){let r=new k("unknown",t.message);return D(r)}}var R=class{constructor(t){this.provider=w,this.currentStatus={status:"not-started"},this.abortController=void 0,this.port=t}async startTunnel(){try{await g(),this.tunnel()}catch(t){this.currentStatus={status:"error",message:t.message,tryMessage:d()}}}getTunnelStatus(){return this.currentStatus}stopTunnel(){this.abortController?.abort()}tunnel(t=0){this.abortController=new O;let r=!1;if(t>=ot){r=!0,this.currentStatus={status:"error",message:"Could not start Cloudflare tunnel: max retries reached.",tryMessage:d()};return}let n=["tunnel","--url",`http://localhost:${this.port}`,"--no-autoupdate"],o=[],a=!1,i;this.currentStatus={status:"starting"},setTimeout(()=>{if(!r){r=!0;let s=[...new Set(o)].slice(-5).join(`
`);s===""?this.currentStatus={status:"error",message:"Could not start Cloudflare tunnel: unknown error.",tryMessage:d()}:this.currentStatus={status:"error",message:s,tryMessage:d()},this.abortController?.abort()}},nt*1e3);let c=this,E=new et({write(s,ft,H){if(u(s.toString()),r)return;i??(i=it(s)),ut(s)&&(a=!0),a&&(i?(r=!0,c.currentStatus={status:"connected",url:i}):c.currentStatus={status:"error",message:"Could not start Cloudflare tunnel: URL not found."});let L=at(s);L&&o.push(L),H()}});A(ct(),n,{stdout:E,stderr:E,signal:this.abortController.signal,externalErrorHandler:async s=>{if(r)throw new b(`Could not start Cloudflare tunnel: process crashed after stablishing a connection: ${s.message}`,d());u(`Cloudflare tunnel crashed: ${s.message}, restarting...`),h()||await P(1),this.tunnel(t+1)}})}};function d(){return["You can run the command again, or try networking with Shopify via",{command:"--use-localhost"},"or",{command:"--tunnel-url <custom tunnel>"},".",{link:{label:"See documentation for details.",url:"https://shopify.dev/docs/apps/build/cli-for-apps/networking-options"}}]}function it(e){let t=new RegExp(`(https:\\/\\/[^\\s]+\\.${dt()})`),r=e.toString().match(t)??void 0;return r&&r[1]}function at(e){if([/failed to request quick Tunnel/,/failed to unmarshal quick Tunnel/,/failed to parse quick Tunnel ID/,/failed to provision routing/,/ERR Couldn't start tunnel/,/ERR Failed to serve quic connection/,/ERR Failed to create new quic connection error/].some(n=>n.test(e.toString())))return`Could not start Cloudflare tunnel: ${lt(e.toString())}`}function lt(e){let t=/^[0-9TZ:-]+ (ERR )?/g,r=/connIndex.*/g;return e.replace(t,"").replace(r,"")}function ut(e){let t=e.toString().match(/(INF Registered tunnel connection|INF Connection)/)??void 0;return t&&t[0]}function ct(){return process.env.SHOPIFY_CLI_CLOUDFLARED_PATH?process.env.SHOPIFY_CLI_CLOUDFLARED_PATH:m(l(rt(import.meta.url)),"..","bin",process.platform==="win32"?"cloudflared.exe":"cloudflared")}function dt(){return process.env.SHOPIFY_CLI_CLOUDFLARED_DOMAIN??"trycloudflare.com"}export{kt as a};