UNPKG

@shopify/cli

Version:

A CLI tool to build for the Shopify platform

3 lines (2 loc) • 14.3 kB
import{a as re,b as se,c as ne,d as ie,f as F,h as v,i as ae,j as pe,l as le,m as ue,n as ce,q as me,r as fe,s as ge,t as de,u as B,v as xe,w as Te,x as he,y as I,z as w}from"../../../chunk-IZ3C5J7G.js";import{a as oe}from"../../../chunk-TQ6HYDVC.js";import{a as te}from"../../../chunk-JCL2RRU6.js";import{a as Q}from"../../../chunk-AGNMOFJS.js";import{Ba as ee,D as K,W as X,Z,k as Y}from"../../../chunk-54CAKCYR.js";import{Y as W}from"../../../chunk-7ESSIN27.js";import"../../../chunk-YOZNRLFC.js";import"../../../chunk-XEFQQPL4.js";import"../../../chunk-GZS44BUW.js";import"../../../chunk-K2WUCOQJ.js";import"../../../chunk-7QIOUDCX.js";import"../../../chunk-7MUKLZOL.js";import"../../../chunk-VBUZWRUL.js";import"../../../chunk-DCPBRWVC.js";import"../../../chunk-KR6QDE7D.js";import"../../../chunk-UXVZ2P63.js";import"../../../chunk-QSTEVZFQ.js";import"../../../chunk-5Y7GIF2W.js";import"../../../chunk-EKXY5COY.js";import"../../../chunk-4DCQNGUV.js";import"../../../chunk-QBSKKQBN.js";import"../../../chunk-FQWB2F75.js";import{z as j}from"../../../chunk-XONFGLJQ.js";import"../../../chunk-4LNCYIS3.js";import"../../../chunk-L2MGAEV3.js";import"../../../chunk-PRKBO42R.js";import"../../../chunk-ZSBA6VIC.js";import"../../../chunk-F2QU6WWX.js";import"../../../chunk-XULPJ6UG.js";import{b as G,c as H}from"../../../chunk-XR6GMMEU.js";import"../../../chunk-3TNEIDOD.js";import"../../../chunk-MHWV5RQV.js";import"../../../chunk-XOTA6JTZ.js";import"../../../chunk-MOA33ZFO.js";import"../../../chunk-JUVAGMIH.js";import{i as V,u as $}from"../../../chunk-6G6TMKXF.js";import"../../../chunk-P6XE4MH5.js";import"../../../chunk-KLMDWDT2.js";import"../../../chunk-5CH3B62S.js";import"../../../chunk-QUTQDXSL.js";import"../../../chunk-WSDN25F5.js";import{d as E}from"../../../chunk-M56NDIMD.js";import"../../../chunk-PD5ZHJWI.js";import{r as Le}from"../../../chunk-LDGAHMS7.js";import"../../../chunk-ZR76GGZ6.js";import"../../../chunk-EENHXSWU.js";import"../../../chunk-FUOIGXI4.js";import"../../../chunk-6M3ZYNGO.js";import"../../../chunk-QYR5VPQA.js";import"../../../chunk-OBEWZXOQ.js";import{Mb as N,Nb as S,da as z,eb as O,fb as f,gb as s}from"../../../chunk-N5PQPIBF.js";import"../../../chunk-CERXUPGC.js";import"../../../chunk-T4M5CWAO.js";import"../../../chunk-PRVQAHWI.js";import"../../../chunk-YTNDFQJT.js";import"../../../chunk-ULQG3XQS.js";import{j as R}from"../../../chunk-IU2ZQ6TE.js";import"../../../chunk-PIBY5DDZ.js";import{e as L,g as x}from"../../../chunk-VPRTJUIN.js";x();x();x();x();x();x();var k=L(O(),1);function ye(t){let r=(0,k.useRef)(t);(0,k.useEffect)(()=>{r.current=t},[t]),(0,k.useEffect)(()=>{let n;function i(){r.current().then(({retryIntervalMs:p})=>{p&&(n=setTimeout(i,p))}).catch(()=>{})}return n=setTimeout(i,0),()=>n&&clearTimeout(n)},[])}x();var M=async({pollOptions:{jwtToken:t,cursor:r,filters:n},developerPlatformClient:i,organizationId:a})=>{let p=await i.appLogs({jwtToken:t,cursor:r},a),{errors:c,status:u}=p;if(u!==200){if(u===401||u===429||u>=500)return{errors:c.map(y=>({status:u,message:y}))};throw new E(`${c.join(", ")} while fetching app logs`)}let{cursor:o,app_logs:l}=p,g=Ee(l,n);return{cursor:o,appLogs:g}};function Ee(t,r){let n=t;return(r.status!==void 0||r.sources!==void 0)&&(n=n.filter(i=>{let a=r.status===void 0?!0:i.status===r.status,p=r.sources===void 0?!0:r.sources.includes(`${i.source_namespace}.${i.source}`);return a&&p})),n}var P=L(O(),1);async function ke({jwtToken:t,cursor:r,filters:n,storeNameById:i,organizationId:a,setErrors:p,setAppLogOutputs:c,resubscribeCallback:u,developerPlatformClient:o}){let l=t,g=F,y=r,A=await M({pollOptions:{jwtToken:t,cursor:r,filters:n},developerPlatformClient:o,organizationId:a}),m="not_attempted",b=A;if(b.errors){let T=await B({response:b,onThrottle:d=>{p(["Request throttled while polling app logs.",`Retrying in ${d/1e3}s`])},onUnknownError:d=>{p(["Error while polling app logs",`Retrying in ${d/1e3}s`])},onResubscribe:()=>u()});m=T.resubscribeResult,T.nextJwtToken&&(l=T.nextJwtToken),g=T.retryIntervalMs}else{p(d=>d.length?[]:d);let{appLogs:T}=A;if(y=A.cursor,T)for(let d of T){let h,C,D,J=i.get(d.shop_id.toString());if(J===void 0)continue;switch(d.log_type){case pe:h=me(d.payload),C=`export "${h.export}" executed in ${(h.fuelConsumed/ae).toFixed(4)}M instructions`;break;case le:h=fe(d.payload),C="network access response retrieved from cache";break;case ue:h=ge(d.payload),C="network access request executing in background";break;case ce:h=de(d.payload),D=h.connectTimeMs&&h.writeReadTimeMs?h.connectTimeMs+h.writeReadTimeMs:null,C=`network access request executed${D?` in ${D} ms`:""}`;break;default:continue}let Ie={status:d.status==="success"?"Success":"Failure",source:d.source,storeName:J,description:C,logTimestamp:z(d.log_timestamp)};h&&c(Ce=>[...Ce,{appLog:h,prefix:Ie}])}}return{nextJwtToken:l,retryIntervalMs:g,cursor:y??r,resubscribeResult:m}}function be({initialJwt:t,filters:r,resubscribeCallback:n,storeNameById:i,developerPlatformClient:a,organizationId:p}){let[c,u]=(0,P.useState)([]),[o,l]=(0,P.useState)([]),g=(0,P.useRef)(t),y=(0,P.useRef)(0),A=(0,P.useRef)(""),m=(0,P.useRef)(0),b=(0,P.useCallback)(async()=>{let T=await ke({jwtToken:g.current,cursor:A.current,filters:r,storeNameById:i,setErrors:u,setAppLogOutputs:l,resubscribeCallback:n,developerPlatformClient:a,organizationId:p});if(T.resubscribeResult==="failed"){if(m.current+=1,m.current>=v)return u(["App log streaming session has expired. Please restart your dev session."]),{retryIntervalMs:0}}else T.resubscribeResult==="succeeded"&&(m.current=0);return g.current=T.nextJwtToken,A.current=T.cursor,y.current=T.retryIntervalMs,{retryIntervalMs:y.current}},[]);return ye(b),{appLogOutputs:o,errors:c}}var e=L(O(),1);var Oe=t=>{switch(t){case 0:return"No cached response available";case 1:return"Cache is about to expire";case 2:return"Unknown reason"}},Pe=({pollOptions:{jwtToken:t,filters:r},resubscribeCallback:n,storeNameById:i,developerPlatformClient:a,organizationId:p})=>{let{appLogOutputs:c,errors:u}=be({filters:r,initialJwt:t,resubscribeCallback:n,storeNameById:i,developerPlatformClient:a,organizationId:p});return e.default.createElement(e.default.Fragment,null,c.map(({appLog:o,prefix:l},g)=>e.default.createElement(f,{flexDirection:"column",key:g},e.default.createElement(f,{flexDirection:"row",gap:1},e.default.createElement(s,null,e.default.createElement(s,{color:"green"},l.logTimestamp," "),e.default.createElement(s,{color:"blueBright"},`${l.storeName.split(".")[0]}`," "),e.default.createElement(s,{color:"blueBright"},l.source," "),e.default.createElement(s,{color:l.status==="Success"?"green":"red"},l.status," "),e.default.createElement(s,null,l.description))),e.default.createElement(f,{flexDirection:"column",marginLeft:4},o instanceof re&&e.default.createElement(f,{flexDirection:"column"},e.default.createElement(s,null,o.logs),o.inputQueryVariablesMetafieldKey&&o.inputQueryVariablesMetafieldNamespace?e.default.createElement(f,{flexDirection:"column",marginTop:1},e.default.createElement(s,{bold:!0},"Input Query Variables:"),e.default.createElement(f,{flexDirection:"row",marginLeft:1,marginTop:1},e.default.createElement(s,{dimColor:!0},"Namespace:"),e.default.createElement(s,null," ",o.inputQueryVariablesMetafieldNamespace)),e.default.createElement(f,{flexDirection:"row",marginLeft:1},e.default.createElement(s,{dimColor:!0},"Key:"),e.default.createElement(s,null," ",o.inputQueryVariablesMetafieldKey)),e.default.createElement(f,{marginLeft:1,marginTop:1},e.default.createElement(s,null,w(o.inputQueryVariablesMetafieldValue)??e.default.createElement(s,{color:"red"},"Metafield is not set")))):null,o.input?e.default.createElement(f,{flexDirection:"column",marginTop:1},e.default.createElement(s,{bold:!0},"Input ",e.default.createElement(s,{dimColor:!0},"(",o.inputBytes," bytes):")),e.default.createElement(f,{marginLeft:1,marginTop:1},e.default.createElement(s,null,w(o.input)))):null,o.output?e.default.createElement(f,{flexDirection:"column",marginTop:1},e.default.createElement(s,{bold:!0},"Output ",e.default.createElement(s,{dimColor:!0},"(",o.outputBytes," bytes):")),e.default.createElement(f,{marginLeft:1,marginTop:1},e.default.createElement(s,null,w(o.output)))):null),o instanceof se&&e.default.createElement(f,{flexDirection:"column"},e.default.createElement(s,null,"Cache write time: ",new Date(o.cacheEntryEpochMs).toISOString()),e.default.createElement(s,null,"Cache TTL: ",o.cacheTtlMs/1e3," s"),e.default.createElement(s,null,"HTTP request:"),e.default.createElement(s,null,w(o.httpRequest)),e.default.createElement(s,null,"HTTP response:"),e.default.createElement(s,null,w(o.httpResponse))),o instanceof ne&&e.default.createElement(f,{flexDirection:"column"},e.default.createElement(s,null,"Reason: ",Oe(o.reason)),e.default.createElement(s,null,"HTTP request:"),e.default.createElement(s,null,w(o.httpRequest))),o instanceof ie&&e.default.createElement(f,{flexDirection:"column"},e.default.createElement(s,null,"Attempt: ",o.attempt),o.connectTimeMs?e.default.createElement(s,null,"Connect time: ",o.connectTimeMs," ms"):null,o.writeReadTimeMs?e.default.createElement(s,null,"Write read time: ",o.writeReadTimeMs," ms"):null,e.default.createElement(s,null,"HTTP request:"),e.default.createElement(s,null,w(o.httpRequest)),o.httpResponse?e.default.createElement(f,{flexDirection:"column"},e.default.createElement(s,null,"HTTP response:"),e.default.createElement(s,null,w(o.httpResponse))):null,o.error?e.default.createElement(s,null,"Error: ",o.error):null)))),u.length>0&&e.default.createElement(f,{flexDirection:"column"},u.map((o,l)=>e.default.createElement(f,{key:l},e.default.createElement(s,{color:"red"},o)))))};var Ae=L(O(),1);async function we({pollOptions:t,options:{variables:r,developerPlatformClient:n},storeNameById:i,organizationId:a}){return V(Ae.default.createElement(Pe,{pollOptions:t,resubscribeCallback:async()=>I(n,r,a),storeNameById:i,developerPlatformClient:n,organizationId:a}))}x();async function U({pollOptions:t,options:{variables:r,developerPlatformClient:n},storeNameById:i,organizationId:a,consecutiveResubscribeFailures:p=0}){let c=await M({pollOptions:t,developerPlatformClient:n,organizationId:a}),u=F,o=t.jwtToken,l=p,g=c;if(g.errors){let m=await B({response:g,onThrottle:b=>{S(JSON.stringify({message:"Request throttled while polling app logs.",retry_in_ms:b}))},onUnknownError:b=>{S(JSON.stringify({message:"Error while polling app logs.",retry_in_ms:b}))},onResubscribe:()=>I(n,r,a)});if(m.resubscribeResult==="failed"){if(l+=1,l>=v){S(JSON.stringify({message:"App log streaming session has expired. Please restart your dev session."}));return}}else m.resubscribeResult==="succeeded"&&(l=0);m.nextJwtToken&&(o=m.nextJwtToken),u=m.retryIntervalMs}else l=0;let{cursor:y,appLogs:A}=c;A&&A.forEach(m=>{let b=i.get(m.shop_id?.toString());b!==void 0&&N(Te({appLog:m,appLogPayload:he(m.payload,m.log_type),storeName:b,prettyPrint:!1}))}),setTimeout(()=>{U({options:{variables:r,developerPlatformClient:n},pollOptions:{jwtToken:o||t.jwtToken,cursor:y??t.cursor,filters:t.filters},storeNameById:i,organizationId:a,consecutiveResubscribeFailures:l}).catch(m=>{throw m})},u)}async function Se(t){let{app:r,remoteApp:n,developerPlatformClient:i}=t,a=await Ne(t),p=xe(r);if(p.length===0)throw new E("This app has no log sources. Learn more about app logs at https://shopify.dev/docs/api/shopify-cli/app/app-logs");if(t.sources){let g=t.sources.filter(y=>!p.includes(y));if(g.length)throw new E(`Invalid sources: ${g.join(", ")}. Valid sources are: ${p.join(", ")}`)}let c={shopIds:a.storeIds.map(Number),apiKey:n.apiKey},u=await I(i,c,t.organization.id),o={status:t.status,sources:t.sources},l={jwtToken:u,filters:o};t.format==="json"?(N(JSON.stringify({subscribedToStores:t.storeFqdns})),S(JSON.stringify({message:"Waiting for app logs..."})),await U({options:{variables:c,developerPlatformClient:i},pollOptions:l,storeNameById:a.storeNameById,organizationId:t.organization.id})):(S(`Waiting for app logs... `),await we({options:{variables:c,developerPlatformClient:i},pollOptions:l,storeNameById:a.storeNameById,organizationId:t.organization.id}))}async function Ne(t){let{app:r,remoteApp:n,developerPlatformClient:i,primaryStore:a,organization:p}=t,c=R(r.configPath);t.format==="text"&&Fe(n.title,a.shopDomain,t.storeFqdns,c,p.businessName);let u=new Map;return u.set(a.shopId,a.shopDomain),t.storeFqdns&&t.storeFqdns.length>1&&await Promise.all(t.storeFqdns?.slice(1).map(async l=>{let g=await W(p,l,i);u.set(g.shopId,l)})),{storeIds:Array.from(u.keys()),storeNameById:u}}function Fe(t,r,n,i,a){let p=[];n&&n.length>0?n.forEach(u=>p.push(u)):p.push(r);let c=i?Y(i):void 0;$({headline:i?`Using ${c} for default values:`:"Using these settings:",body:X({appName:t,org:a,devStores:p,messages:[Z]})})}var _=L(Le(),1);var q=class t extends te{static{this.summary="Stream detailed logs for your Shopify app."}static{this.descriptionWithMarkdown="\n Opens a real-time stream of detailed app logs from the selected app and store.\n Use the `--source` argument to limit output to a particular log source, such as a specific Shopify Function handle. Use the `shopify app logs sources` command to view a list of sources.\n Use the `--status` argument to filter on status, either `success` or `failure`.\n ```\n shopify app logs --status=success --source=extension.discount-function\n ```\n "}static{this.description=this.descriptionWithoutMarkdown()}static{this.flags={...G,...Q,...H,store:_.Flags.string({char:"s",description:"Store URL. Must be an existing development or Shopify Plus sandbox store.",env:"SHOPIFY_FLAG_STORE",multiple:!0,parse:async r=>j(r)}),source:_.Flags.string({description:"Filters output to the specified log source.",env:"SHOPIFY_FLAG_SOURCE",multiple:!0}),status:_.Flags.string({description:"Filters output to the specified status (success or failure).",options:["success","failure"],env:"SHOPIFY_FLAG_STATUS"})}}async run(){let{flags:r}=await this.parse(t);await K(r.path);let n=await ee({directory:r.path,clientId:r["client-id"],forceRelink:r.reset,userProvidedConfigName:r.config}),i=await oe({appContextResult:n,storeFqdn:r.store?.[0],forceReselectStore:r.reset}),a={...n,primaryStore:i,storeFqdns:r.store,sources:r.source,status:r.status,format:r.json?"json":"text"};return await Se(a),{app:n.app}}};export{q as default};