UNPKG

@shopify/cli

Version:

A CLI tool to build for the Shopify platform

6 lines (4 loc) • 10.3 kB
import{a as S}from"../../../../chunk-3NBK56IK.js";import{b as nn,c as tn}from"../../../../chunk-UHJYPEY2.js";import{a as Z}from"../../../../chunk-HIBX234C.js";import{z as O}from"../../../../chunk-IZ3C5J7G.js";import{a as X}from"../../../../chunk-JCL2RRU6.js";import{a as V}from"../../../../chunk-AGNMOFJS.js";import{Ba as Q}from"../../../../chunk-54CAKCYR.js";import"../../../../chunk-7ESSIN27.js";import"../../../../chunk-UWJ73J4B.js";import"../../../../chunk-WFXRSKRK.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-4ZGSSQC6.js";import"../../../../chunk-QBSKKQBN.js";import"../../../../chunk-FQWB2F75.js";import"../../../../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 z,c as Y}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{B as U,a as C,i as J,j as K,k as b,p as T}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 B}from"../../../../chunk-M56NDIMD.js";import"../../../../chunk-PD5ZHJWI.js";import{r as yn}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{a as G}from"../../../../chunk-OBEWZXOQ.js";import{eb as P,fb as c,gb as u,ib as $,jb as j,kb as H,ma as E}from"../../../../chunk-N5PQPIBF.js";import"../../../../chunk-CERXUPGC.js";import"../../../../chunk-T4M5CWAO.js";import"../../../../chunk-PRVQAHWI.js";import{f as q}from"../../../../chunk-YTNDFQJT.js";import"../../../../chunk-ULQG3XQS.js";import{d as A}from"../../../../chunk-IU2ZQ6TE.js";import"../../../../chunk-PIBY5DDZ.js";import{e as R,g as a}from"../../../../chunk-VPRTJUIN.js";a();a();a();a();a();var y=R(P(),1);import{Writable as on}from"stream";function en({selectedRun:n,abortController:t,app:r,extension:e,appWatcher:i}){let s={type:"functionRun",input:n.payload.input,output:n.payload.output,logs:n.payload.logs,name:n.source,size:0,memory_usage:0,instructions:n.payload.fuelConsumed},[p,f]=(0,y.useState)([]),[m,l]=(0,y.useState)([s,s]),[h,g]=(0,y.useState)(void 0),{input:F,export:M}=n.payload,[mn,L]=(0,y.useState)(`Watching for changes to ${n.source}...`);return(0,y.useEffect)(()=>{let I=new C;t.signal.addEventListener("abort",()=>{I.abort()});let _=async()=>{let x=await hn(e,JSON.stringify(F),M);l(d=>[x,d[0]]),L(`Watching for changes to ${n.source}...`),f(d=>[...d,x])},fn=async()=>{L("Replaying log with local function..."),await _()},gn=async()=>{i.onEvent(async d=>{let w=d.extensionEvents.find(k=>k.extension.handle===e.handle);if(!(!w||w.type!=="changed")){if(w.buildResult?.status==="error"){g(`Error while reloading and building extension: ${w.buildResult?.error}`);return}g(void 0),L("Re-running with latest changes..."),await _()}});let x=new on({write(d,w,k){f(dn=>[...dn,{type:"systemMessage",message:d.toString()}]),k()}});await i.start({stdout:x,stderr:x,signal:I.signal},!1)};return fn().then(()=>{gn()}),()=>{I.abort()}},[F,M,r,e]),T(t.signal,async()=>{setTimeout(()=>{q()||G(process.pid,"SIGINT",!1,()=>{process.exit(0)})},2e3)}),{logs:p,statusMessage:mn,recentFunctionRuns:m,error:h}}async function hn(n,t,r){let e="",i=new on({write(p,f,m){e+=p,m()}});return await S({functionExtension:n,input:t,export:r,stdout:i,json:!0}),{...JSON.parse(e),type:"functionRun"}}var o=R(P(),1);var rn=({selectedRun:n,abortController:t,app:r,extension:e})=>{let{isAborted:i}=T(t.signal),{isRawModeSupported:s}=j(),p=new Z(r),{logs:f,statusMessage:m,recentFunctionRuns:l,error:h}=en({selectedRun:n,abortController:t,app:r,extension:e,appWatcher:p});return H((g,F)=>{K(g,F,()=>t.abort()),g==="q"&&t.abort()},{isActive:!!s}),o.default.createElement(o.default.Fragment,null,o.default.createElement($,{items:f},(g,F)=>o.default.createElement(c,{key:`replayOutputScrollerLog${F}`,flexDirection:"column"},o.default.createElement(Cn,{log:g}))),i?null:o.default.createElement(c,{marginY:1,paddingTop:1,flexDirection:"column",flexGrow:1,borderStyle:"single",borderBottom:!1,borderLeft:!1,borderRight:!1,borderTop:!0},s?o.default.createElement(c,{flexDirection:"column"},o.default.createElement(c,{flexDirection:"row"},o.default.createElement(u,null,b.pointerSmall," ",m)),o.default.createElement(Rn,{recentFunctionRuns:l}),o.default.createElement(u,null,b.pointerSmall," Press ",o.default.createElement(u,{bold:!0},"q")," ",b.lineVertical," quit")):null,h?o.default.createElement(u,{color:"red"},h):null))};function D({title:n,backgroundColor:t}){let r=Math.max(Math.round((28-n.length)/2),0),e=" ".repeat(r);return o.default.createElement(u,{backgroundColor:t,color:"black"},` `,e,n,e,` `)}function Fn({input:n}){return o.default.createElement(c,{flexDirection:"column"},o.default.createElement(D,{title:"Input",backgroundColor:"yellow"}),o.default.createElement(u,null,O(n)))}function xn({logs:n}){return o.default.createElement(c,{flexDirection:"column"},o.default.createElement(D,{title:"Logs",backgroundColor:"blueBright"}),o.default.createElement(u,null,n))}function wn({output:n}){return o.default.createElement(c,{flexDirection:"column"},o.default.createElement(D,{title:"Output",backgroundColor:"greenBright"}),o.default.createElement(u,null,O(n)))}function bn({functionRun:n}){return o.default.createElement(c,{flexDirection:"column"},o.default.createElement(D,{title:"Benchmark Results",backgroundColor:"green"}),o.default.createElement(u,null,"Name: ",n.name),o.default.createElement(u,null,"Linear Memory Usage: ",n.memory_usage,"KB"),o.default.createElement(u,null,"Instructions: ",n.instructions/1e3,"K"),o.default.createElement(u,null,"Size: ",n.size,"KB"))}function Rn({recentFunctionRuns:n}){let t=n[0].instructions-n[1].instructions;return o.default.createElement(c,{flexDirection:"column"},o.default.createElement(u,null,b.pointerSmall," Instruction count change: ",t>0?"+":"",t))}function Cn({log:n}){switch(n.type){case"functionRun":return o.default.createElement(c,{flexDirection:"column"},o.default.createElement(Fn,{input:n.input}),o.default.createElement(xn,{logs:n.logs}),o.default.createElement(wn,{output:n.output}),o.default.createElement(bn,{functionRun:n}),o.default.createElement(u,null,"\xA0"));case"systemMessage":return o.default.createElement(u,null,n.message);default:return null}}var sn=R(P(),1);async function un({selectedRun:n,abortController:t,app:r,extension:e}){return J(sn.default.createElement(rn,{selectedRun:n,abortController:t,app:r,extension:e}),{exitOnCtrlC:!1})}a();async function an(n){if(n.length===0)return;let t=i=>({label:`${i.logTimestamp} (${i.status}) - ${i.identifier}`,value:i}),r=n.map(t);return await U({message:"Which function run would you like to replay locally?",choices:r})}import{existsSync as Tn,readdirSync as Sn}from"fs";var v=100;async function cn(n){let{watch:t,extension:r,app:e}=n,i=new C;try{let s=e.getLogsDir(),p=n.log?await Dn(s,r.handle,n.log):await In(s,r.handle),{input:f,export:m}=p.payload;t?await un({selectedRun:p,abortController:i,app:e,extension:r}):await S({functionExtension:r,json:n.json,input:JSON.stringify(f),export:m})}catch(s){throw i.abort(),s}}async function Dn(n,t,r){let e=await Ln(n,t,r);if(e===void 0)throw new B(`No log found for '${r}'. Searched ${n} for function ${t}.`);let i=await E(e);return JSON.parse(i)}function pn(n){let t=n.split(/[_.]/);if(!(t.length<6))return{namespace:t[3],functionHandle:t[4],identifier:t[5]}}async function Ln(n,t,r){let e=ln(n).find(i=>{let s=pn(i);return s?.namespace==="extensions"&&s?.functionHandle===t&&s?.identifier===r});return e?A(n,e):void 0}async function In(n,t){let r=await kn(n,t),e=await an(r);if(e===void 0)throw new B(`No logs found in ${n}`);return e}async function kn(n,t){let r=ln(n).filter(i=>{let s=pn(i);return s?.namespace==="extensions"&&s?.functionHandle===t}).reverse(),e=[];for(let i=0;i<r.length&&e.length<v;i+=v){let p=r.slice(i,i+v).map(l=>A(n,l)),m=(await Promise.all(p.map(async l=>{let h=await E(l);return{...JSON.parse(h),identifier:An(l)}}))).filter(l=>l.payload.input!=null);e=e.concat(m)}return e}function An(n){return n.split("_").pop().substring(0,6)}function ln(n){return Tn(n)?Sn(n):[]}var N=R(yn(),1),W=class n extends X{static{this.summary="Replays a function run from an app log."}static{this.descriptionWithMarkdown="Runs the function from your current directory for [testing purposes](https://shopify.dev/docs/apps/functions/testing-and-debugging). To learn how you can monitor and debug functions when errors occur, refer to [Shopify Functions error handling](https://shopify.dev/docs/api/functions/errors)."}static{this.description=this.descriptionWithoutMarkdown()}static{this.flags={...z,...V,...nn,...Y,log:N.Flags.string({char:"l",description:"Specifies a log identifier to replay instead of selecting from a list. The identifier is provided in the output of `shopify app dev` and is the suffix of the log file name.",env:"SHOPIFY_FLAG_LOG"}),watch:N.Flags.boolean({char:"w",hidden:!1,allowNo:!0,default:!0,description:"Re-run the function when the source code changes.",env:"SHOPIFY_FLAG_WATCH"})}}async run(){let{flags:t}=await this.parse(n),{app:r}=await Q({directory:t.path,clientId:t["client-id"],forceRelink:t.reset,userProvidedConfigName:t.config}),e=await tn(r,t.path);return await cn({app:r,extension:e,path:t.path,log:t.log,json:t.json,watch:t.watch}),{app:r}}};export{W as default};