@shopify/cli
Version:
A CLI tool to build for the Shopify platform
4 lines (3 loc) • 4.2 kB
JavaScript
import{d as w,n as y,r as n}from"./chunk-XONFGLJQ.js";import{w as l}from"./chunk-L2MGAEV3.js";import{k as u,l as m}from"./chunk-ZSBA6VIC.js";import{a as h}from"./chunk-F2QU6WWX.js";import{a as x}from"./chunk-MOA33ZFO.js";import{O as S,u as d,w as g,x as N}from"./chunk-6G6TMKXF.js";import{f as _}from"./chunk-M56NDIMD.js";import{Qb as a}from"./chunk-N5PQPIBF.js";import{a as f}from"./chunk-ULQG3XQS.js";import{g as p}from"./chunk-VPRTJUIN.js";p();var $="https://cdn.shopify.com/static/cli/notifications.json",D="Cache is empty",C=["notifications:list","notifications:generate","init","app:init","theme:init","hydrogen:init","cache:clear"];function c(){return process.env.SHOPIFY_CLI_NOTIFICATIONS_URL??$}var E=n.object({id:n.string(),message:n.string(),type:n.enum(["info","warning","error"]),frequency:n.enum(["always","once","once_a_day","once_a_week"]),ownerChannel:n.string(),cta:n.object({label:n.string(),url:n.string().url()}).optional(),title:n.string().optional(),minVersion:n.string().optional(),maxVersion:n.string().optional(),minDate:n.string().optional(),maxDate:n.string().optional(),commands:n.array(n.string()).optional(),surface:n.string().optional()}),I=n.object({notifications:n.array(E)});async function Q(e,t=process.env){try{let r=x();if(b(r,t)||w(t))return;let o=await k(),s=v(o.notifications,r,e);a(`Notifications to show: ${s.length}`),await V(s)}catch(r){if(r.message===D){a("Notifications to show: 0 (Cache is empty)");return}if(r.message==="abort")throw new _;let o=`Error showing notifications: ${r.message}`;a(o);let{sendErrorToBugsnag:s}=await import("./error-handler-PRZFHPRG.js");await s(o,"unexpected_error")}}function b(e,t=process.env){return f(t.CI)||f(t.SHOPIFY_UNIT_TEST)||C.includes(e)}async function V(e){e.slice(0,2).forEach(t=>{let r={headline:t.title,body:t.message.replace(/\\n/g,`
`),link:t.cta};switch(t.type){case"info":{d(r);break}case"warning":{g(r);break}case"error":throw N(r),new Error("abort")}u(`notification-${t.id}`,new Date().getTime().toString())})}async function k(){let e=`notifications-${c()}`,t=m(e)?.value;if(!t)throw new Error(D);let r=JSON.parse(t);return I.parse(r)}async function X(){a("Fetching notifications...");let e=await y(c(),void 0,{useNetworkLevelRetry:!1,useAbortSignal:!0,timeoutMs:3*1e3});if(e.status!==200)throw new Error(`Failed to fetch notifications: ${e.statusText}`);let t=await e.text(),r=JSON.parse(t),o=I.parse(r);return await O(t),o}async function O(e){u(`notifications-${c()}`,e),a(`Notifications from ${c()} stored in the cache`)}function Z(e,t=process.argv,r=process.env){if(b(e,r)||!t[0]||!t[1])return;let o=t[0],i=[t[1],"notifications","list","--ignore-errors"];S(o,i,{background:!0,env:{...process.env,SHOPIFY_CLI_NO_ANALYTICS:"1"},externalErrorHandler:async T=>{a(`Failed to fetch notifications in background: ${T.message}`)}})}function v(e,t,r,o=new Date(new Date().setUTCHours(0,0,0,0)),s=h){return e.filter(i=>B(i,s)).filter(i=>F(i,o)).filter(i=>A(i,t)).filter(i=>j(i,t,r)).filter(i=>q(i))}function B(e,t){let r=!e.minVersion||l(t,`>=${e.minVersion}`),o=!e.maxVersion||l(t,`<=${e.maxVersion}`);return r&&o}function F(e,t){let r=!e.minDate||new Date(e.minDate)<=t,o=!e.maxDate||new Date(e.maxDate)>=t;return r&&o}function A(e,t){return t===""?!0:!e.commands||e.commands.includes(t)}function j(e,t,r){let o=t.split(":")[0]??"all",s=e.surface??"all";return r?r.includes(s):s===o||s==="all"}function q(e){if(!e.frequency)return!0;let t=`notification-${e.id}`,r=m(t)?.value;if(!r)return!0;switch(e.frequency){case"always":return!0;case"once":return!1;case"once_a_day":return new Date().getTime()-Number(r)>24*3600*1e3;case"once_a_week":return new Date().getTime()-Number(r)>168*3600*1e3}}function ee(e){let t=[];return e.minDate&&t.push(`from ${e.minDate}`),e.maxDate&&t.push(`to ${e.maxDate}`),e.minVersion&&t.push(`from v${e.minVersion}`),e.maxVersion&&t.push(`to v${e.maxVersion}`),e.frequency==="once"&&t.push("show only once"),e.frequency==="once_a_day"&&t.push("show once a day"),e.frequency==="once_a_week"&&t.push("show once a week"),e.surface&&t.push(`surface = ${e.surface}`),e.commands&&t.push(`commands = ${e.commands.join(", ")}`),t.join(`
`)}export{Q as a,k as b,X as c,Z as d,v as e,ee as f};