@shopify/cli
Version:
A CLI tool to build for the Shopify platform
16 lines (11 loc) • 8.21 kB
JavaScript
import{a as C}from"../../../chunk-MWGBTOO3.js";import{c as A,i as P,m as k}from"../../../chunk-K6CTUBFC.js";import{e as T,f as R,g as I,k as q,q as L}from"../../../chunk-XITQULK4.js";import"../../../chunk-IX5ICOBV.js";import{a as b}from"../../../chunk-SXTMSGUE.js";import"../../../chunk-QFWIDATO.js";import{b as E,m as x}from"../../../chunk-CBXSPL4W.js";import"../../../chunk-UJVH7FZS.js";import"../../../chunk-EKXY5COY.js";import"../../../chunk-4DCQNGUV.js";import{xa as w}from"../../../chunk-XONFGLJQ.js";import"../../../chunk-4LNCYIS3.js";import"../../../chunk-L2MGAEV3.js";import"../../../chunk-PRKBO42R.js";import"../../../chunk-ZSBA6VIC.js";import{a as v}from"../../../chunk-F2QU6WWX.js";import"../../../chunk-XULPJ6UG.js";import{b as S}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"../../../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 a}from"../../../chunk-M56NDIMD.js";import"../../../chunk-PD5ZHJWI.js";import{r as V}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{Gb as c,Ib as m,Nb as i,Qb as l}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"../../../chunk-IU2ZQ6TE.js";import"../../../chunk-PIBY5DDZ.js";import{e as U,g as o}from"../../../chunk-VPRTJUIN.js";o();o();o();var d=class extends b{constructor(t){super(t);this.context="REPL";this.themeId=T()}async create(t,s){let r=await super.create(t,s);return await this.uploadThemeAssets(r),r}async findOrCreate(){let t=await this.fetch();if(!t){let s=`Liquid Console (${v})`;t=await this.create(E,s)}return t}setTheme(t){R(t)}removeTheme(){I()}async uploadThemeAssets(t){let s=[{key:"config/settings_data.json",value:"{}"},{key:"config/settings_schema.json",value:"[]"},{key:"snippets/eval.liquid",value:""},{key:"layout/password.liquid",value:"{{ content_for_header }}{{ content_for_layout }}"},{key:"layout/theme.liquid",value:"{{ content_for_header }}{{ content_for_layout }}"},{key:"sections/announcement-bar.liquid",value:""},{key:"templates/index.json",value:JSON.stringify({sections:{announcement:{type:"announcement-bar",settings:{}}},order:["announcement"]})}];await x(t.id,s,this.adminSession)}};o();o();async function O(e){return await G(e)??await _(e)??await H(e)??await Y(e)??void 0}async function G(e){l(`Evaluating snippet - ${e.snippet}`);let n=`{ "type": "display", "value": {{ ${e.snippet} | json }} }`,t=await f({...e,snippet:n}),s=await t.text();return j(t.status,s)?K(s):void 0}async function _(e){l(`Evaluating context - ${e.snippet}`);let n=`{ "type": "context", "value": "{% ${e.snippet.replace(/"/g,'\\"')} %}" }`,t=await f({...e,snippet:n}),s=await t.text();j(t.status,s)&&e.replSession.push(JSON.parse(n))}async function H(e){if(l(`Evaluating assignment context - ${e.snippet}`),re(e.snippet))return e.snippet=`assign ${e.snippet}`,i(m`${c.gray(`> ${e.snippet}`)}`),_(e)}async function Y(e){l(`Evaluating syntax error - ${e.snippet}`);let n="";if(Q(e.snippet)||(n=await(await f({...e,snippet:`{{ ${e.snippet} }}`})).text()),h(n)||(n=await(await f({...e,snippet:`{% ${e.snippet} %}`})).text()),h(n)){let t=n.replace(/ \(snippets\/eval line \d+\)/,"");z(e.snippet,t)}}function z(e,n){if(n.includes("Unknown tag")){i(m`${c.errorText(`Unknown object, property, tag, or filter: '${e}'`)}`);return}let t=$(n);t&&i(m`${c.errorText(t)}`)}async function f(e){let n=B(e);e.url.startsWith("/")||(e.url=`/${e.url}`);let t=await P(e.themeSession,{method:"GET",path:e.url,query:[],themeId:e.themeId,sectionId:"announcement-bar",headers:{},replaceTemplates:{"sections/announcement-bar.liquid":"{% render 'eval' %}","snippets/eval.liquid":`
${n}
`}});return X(t)&&te(),Z(t)&&ne(),ee(t)&&se(),t}function B(e){return`[${[...e.replSession.map(t=>JSON.stringify(t)),e.snippet].join(",").replace(/\\"/g,'"')}]`}function K(e){let n=$(e);if(n)return JSON.parse(n)?.find(s=>s.type==="display")?.value}function $(e){let n=e.split(`
`).slice(1,-1);if(n.length!==0)return n.join("")}function h(e){return e.includes("Liquid syntax error")}function Q(e){return/^\s*assign\s*((?:\(?[\w\-.[\]]\)?)+)\s*=\s*(.*)\s*/m.test(e)}function X(e){return e.status===401||e.status===403}function Z(e){return e.status===430||e.status===429}function ee(e){return e.headers.get("server-timing")?.includes('pageType;desc="404"')??!1}function te(){throw new a("Session expired. Please initiate a new one.")}function ne(){throw new a("Evaluations limit reached. Try again later.")}function se(){throw new a("Page not found. Please provide a valid --url value!")}function re(e){return/^\s*((?:\(?[\w\-.[\]]\)?)+)\s*=\s*(.*)\s*/m.test(e)}function j(e,n){return e===200&&!h(n)}o();function D(e){if(M(e)){i("Object can't be printed, but you can access its fields. Read more at https://shopify.dev/docs/api/liquid.");return}if(e==null){F("null");return}let n=JSON.stringify(e,null,2);F(n)}function M(e){if(Array.isArray(e))return e.length>0?M(e[0]):!1;if(e&&typeof e=="object"){let t=e.error;return typeof t=="string"&&t.includes("json not allowed for this object")}return!1}function F(e){return i(m`${c.cyan(e)}`)}import{createInterface as oe}from"readline";var ie=`Liquid Console doesn't support Liquid delimiters such as '{{ ... }}' or '{% ... %}'.
Please use 'collections.first' instead of '{{ collections.first }}'.`;async function N(e,n,t){process.stdin.isTTY&&process.stdin.ref();let s=oe({input:process.stdin,output:process.stdout}),r=[];s.on("line",u=>{ae(u,e,n,t,s,r)}),s.prompt()}async function ae(e,n,t,s,r,u){try{if(ue(e))return i(ie),r.prompt();let p=await O({snippet:e,themeSession:n,themeId:t,url:s,replSession:u});D(p),r.prompt()}catch(p){throw r.close(),p instanceof Error?(l(p.stack??"Error backtrace not found"),new a(p.message)):new a("An unknown error occurred. Please try again.")}}function ue(e){return/^\s*(\{\{|{%)/.test(e)}async function W(e,n){let t=await pe(e),s=await A(e)?await C(n,e.storeFqdn):void 0;return{themeId:t,storePassword:s}}async function pe(e){return(await new d(e).findOrCreate()).id.toString()}async function J(e,n,t,s,r){if(i(`Welcome to Shopify Liquid console
(press Ctrl + C to exit)`),s?.startsWith("shpat_"))throw new a("Unable to use Admin API tokens with the console command",`To use this command with the --password flag you must:
1. Install the Theme Access app on your shop
2. Generate a new password
Alternatively, you can authenticate normally by not passing the --password flag.
Learn more: https://shopify.dev/docs/storefronts/themes/tools/theme-access`);let u=await k(n,e,s,r);return N(u,n,t)}var g=U(V(),1);var y=class extends L{static{this.summary="Shopify Liquid REPL (read-eval-print loop) tool"}static{this.usage=["theme console","theme console --url /products/classic-leather-jacket"]}static{this.descriptionWithMarkdown=`Starts the Shopify Liquid REPL (read-eval-print loop) tool. This tool provides an interactive terminal interface for evaluating Liquid code and exploring Liquid objects, filters, and tags using real store data.
You can also provide context to the console using a URL, as some Liquid objects are context-specific`}static{this.description=this.descriptionWithoutMarkdown()}static{this.flags={...S,...q,url:g.Flags.string({description:"The url to be used as context",env:"SHOPIFY_FLAG_URL",default:"/"}),"store-password":g.Flags.string({description:"The password for storefronts with password protection.",env:"SHOPIFY_FLAG_STORE_PASSWORD"})}}static{this.multiEnvironmentsFlags=null}async command(n,t){let{url:s,password:r}=n,{themeId:u,storePassword:p}=await W(t,n["store-password"]);w("theme-command:console:single-env:authenticated"),await J(t,u,s,r,p)}};export{y as default};