UNPKG

@shopify/cli

Version:

A CLI tool to build for the Shopify platform

13 lines (9 loc) • 7.22 kB
import{a as H}from"./chunk-IX5ICOBV.js";import{k as U,t as F}from"./chunk-CBXSPL4W.js";import{M as T,O as _,o as w,va as p,xa as h}from"./chunk-XONFGLJQ.js";import{a as C}from"./chunk-F2QU6WWX.js";import{P,x,y as A}from"./chunk-6G6TMKXF.js";import{d as m}from"./chunk-M56NDIMD.js";import{Gb as v,Ib as q,Qb as c}from"./chunk-N5PQPIBF.js";import{u as D}from"./chunk-YTNDFQJT.js";import{g as d}from"./chunk-VPRTJUIN.js";d();d();function O(e){let r=new URLSearchParams;for(let[t,o]of Object.entries(e.replaceTemplates??[]))r.append(`replace_templates[${t}]`,o);for(let[t,o]of Object.entries(e.replaceExtensionTemplates??[])){let s=t.split("/")[0];r.append(`replace_extension_templates[${s}][${t}]`,o)}return r.append("_method",e.method),r}function l(){return{"User-Agent":`Shopify CLI; v=${C}`}}function R(e){return delete e.cookie,delete e.authorization,e}d();function S(e){let r={};return e.split(";").forEach(t=>{let o=t.match(/(.*?)=(.*)$/)??[],s=o[1]?.trim(),n=o[2]?.trim()??"";s&&(r[s]=n)}),r}function k(e){return Object.entries(e).map(([r,t])=>`${r}=${t}`).join("; ")}var y=class extends m{};async function ge(e){return F(e)}async function Se(e,r){let t=z(r),o=new URLSearchParams;o.append("form_type","storefront_password"),o.append("utf8","\u2713"),o.append("password",e??""),h("theme-service:storefront-session:check-storefront-password");let s=await w(`${t}/password`,{headers:{"cache-control":"no-cache","content-type":"application/x-www-form-urlencoded"},body:o.toString(),method:"POST",redirect:"manual"});if(s.status===429)throw p(new m(`Too many incorrect password attempts. Please try again after ${s.headers.get("retry-after")} seconds.`));return j(s,t)}async function L(e,r,t,o,s={}){let n={},i=await I(e,t,s),a=z(r);if(n._shopify_essential=i,h(`theme-service:storefront-session:is-password-protected:${!!o}`),!o)return n;let f=await G(i,e,a,o,s);return{...n,...f}}async function I(e,r,t,o=1){let s=new URLSearchParams({preview_theme_id:r,_fd:"0",pb:"0"}),n=`${e}?${s}`;h("theme-service:storefront-session:get-session-essential-cookie");let i=await w(n,{method:"HEAD",redirect:"manual",headers:{...t,...l()}}),a=i.headers.raw()["set-cookie"]??[],f=E(a,"_shopify_essential");if(!f){if(c(`Failed to obtain _shopify_essential cookie. -Request ID: ${i.headers.get("x-request-id")??"unknown"} -Body: ${await i.text()} -Status: ${i.status} `),o>3)throw p(new y('Your development session could not be created because the "_shopify_essential" could not be defined. Please, check your internet connection.'));return c("Retrying to obtain the _shopify_essential cookie..."),await P(o),I(e,r,t,o+1)}return f}async function G(e,r,t,o,s){let n=new URLSearchParams({password:o}),i=await w(`${r}/password`,{method:"POST",redirect:"manual",body:n,headers:{...s,...l(),Cookie:k({_shopify_essential:e})}});if(!j(i,t))throw p(new m("Your development session could not be created because the store password is invalid. Please, retry with a different password."));let a=i.headers.raw()["set-cookie"]??[],f=E(a,"storefront_digest"),u=E(a,"_shopify_essential"),g={};return f&&(g.storefront_digest=f),u&&(g._shopify_essential=u),g}function j(e,r){let t=e.headers.get("location")??"",o;try{o=new URL(t,r)}catch(n){if(n instanceof TypeError)return!1;throw n}let s=new URL(r).origin;return e.status===302&&o.origin===s}function E(e,r){let t=e.find(s=>S(s)[r]);return t?S(t)[r]:void 0}function z(e){return`https://${e}`}d();d();function K(e,r){p(r),r instanceof m?(r.message=`${e} ${r.message}`,A(r)):(x({headline:e,body:r.message}),c(`${e} ${r.stack??r.message}`))}function be(e,r){let t={delete:`Failed to delete file "${e}" from remote theme.`,upload:`Failed to upload file "${e}" to remote theme.`,default:e}[r??"default"]??e;return o=>{K(t,o)}}function $(e,r){return H({status:e.status??502,statusText:e.statusText??"Bad Gateway",cause:e,data:{url:(r??e.url)?.toString(),requestId:e.headers?.get("x-request-id")??void 0}})}function Ce(e,r="Unexpected error during fetch"){let t=e,o=t.statusCode??502,s=t.statusMessage??"Bad Gateway",n=`${r.replace(/\.$/,"")} with status ${o} (${s}).`;t.data?.requestId&&(n+=` Request ID: ${t.data.requestId}`),t.data?.url&&(n+=` URL: ${t.data.url}`);let i=t.cause??e;return{headline:n,body:i.stack??t.stack??t.message??s,status:o,statusText:s,cause:i,requestId:t.data?.requestId,url:t.data?.url}}async function Fe(e,r){let t=J(e,r),o=await V(e,r),s,n=Object.keys({...r.replaceTemplates,...r.replaceExtensionTemplates});if(n.length>0){c(`\u2192 Rendering ${t} (with ${n})...`);let u=O(r);s=await fetch(t,{method:"POST",body:u,headers:{...o,...l()}}).catch(g=>{throw $(p(g),t)})}else c(`\u2192 Rendering ${t}...`),s=await fetch(t,{method:r.method,headers:{...o,...l()}}).catch(u=>{throw $(p(u),t)});let i=s.headers.get("x-request-id");c(`\u2190 ${s.status} (request_id: ${i})`);let f=s.headers.get("Content-Type")?.includes("application/json");return s=new Response(s.body,s),f||s.headers.delete("Content-Type"),s}async function V(e,r){return N(e)?Y(e,r):W(e,r)}async function W(e,r){let t=await B(e,r),o=e.storefrontToken;return R({...r.headers,Authorization:`Bearer ${o}`,Cookie:t})}async function Y(e,r){let t=await B(e,r),o=e.storefrontToken,s={},n=["ACCEPT","CONTENT-TYPE","CONTENT-LENGTH"];for(let[i,a]of Object.entries(r.headers))n.includes(i.toUpperCase())&&(s[i]=a);return R({...s,...Q(e),Authorization:`Bearer ${o}`,Cookie:t})}function B(e,r){let t=S(r.headers.cookie??r.headers.Cookie??""),o=e.sessionCookies??{};return k({...t,...o})}function J(e,{path:r,sectionId:t,appBlockId:o,query:s}){let i=`${b(e)}${r}`,a=new URLSearchParams({_fd:"0",pb:"0"});for(let[f,u]of s)a.append(f,u);return t?a.append("section_id",t):o&&a.append("app_block_id",o),`${i}?${a}`}function b(e){return N(e)?`https://${D()}/cli/sfr`:`https://${e.storeFqdn}`}function N(e){return e.token.startsWith("shptka_")}function Q(e){return{"X-Shopify-Shop":e.storeFqdn,"X-Shopify-Access-Token":e.token}}d();var Z=1800*1e3,M=["layout/theme.liquid","config/settings_schema.json"];async function Ge(e,r,t,o){let s=await X(e,r,t,o);return s.refresh=async()=>{c("Refreshing theme session...");let n=await X(e,r,t,o);return h("theme-service:session:refreshed"),Object.assign(s,n),n},setInterval(()=>{s.refresh&&s.refresh().then(()=>c("Refreshed theme session via auto-refresher...")).catch(()=>c("Session could not be refreshed."))},Z),s}async function X(e,r,t,o){let s=b(r),n=await _(r.storeFqdn,t,[],{forceRefresh:!1,noPrompt:!0}),i=await T([],t,{forceRefresh:!1,noPrompt:!0}),a=await ee(s,e,r,i,o);return{...n,sessionCookies:a,storefrontToken:i}}async function ee(e,r,t,o,s){try{return await L(e,t.storeFqdn,r,s,{"X-Shopify-Shop":t.storeFqdn,"X-Shopify-Access-Token":t.token,Authorization:`Bearer ${o}`})}catch(n){throw n instanceof y&&await re(r,t),n}}async function re(e,r){if(c(`Verifying if theme with id ${e} has required files...`),(await U(Number(e),M,r)).length!==M.length)throw p(new m(q`Theme ${v.cyan(e)} is missing required files. Run ${v.cyan(`shopify theme delete -t ${e}`)} to delete it, then try your command again.`.value));c(`Theme with id ${e} has required files.`)}export{l as a,R as b,ge as c,Se as d,K as e,be as f,$ as g,Ce as h,Fe as i,V as j,B as k,b as l,Ge as m,X as n};