@shopify/cli
Version:
A CLI tool to build for the Shopify platform
10 lines (7 loc) • 15.4 kB
JavaScript
import{a as w,b as W,d as H,g as B}from"../../../../chunk-NPEVESCV.js";import{a as j,c as z,d as G,e as N,f as J,h as K}from"../../../../chunk-M3RYUB22.js";import"../../../../chunk-TQ6HYDVC.js";import{a as $}from"../../../../chunk-JCL2RRU6.js";import{a as q,b as Q}from"../../../../chunk-AGNMOFJS.js";import"../../../../chunk-54CAKCYR.js";import"../../../../chunk-7ESSIN27.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{b as c}from"../../../../chunk-EKXY5COY.js";import"../../../../chunk-4DCQNGUV.js";import"../../../../chunk-QBSKKQBN.js";import"../../../../chunk-FQWB2F75.js";import{n as b}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 V}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{E as f,P as F,a as _,u as E,v as y,w as C,x as R}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,g as x}from"../../../../chunk-M56NDIMD.js";import"../../../../chunk-PD5ZHJWI.js";import"../../../../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 m,Ib as u,Mb as L,Ta as P,ma as I,ua as D}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{g as r}from"../../../../chunk-VPRTJUIN.js";r();r();r();r();var X={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"mutation",name:{kind:"Name",value:"BulkOperationRunQuery"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"query"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"bulkOperationRunQuery"},arguments:[{kind:"Argument",name:{kind:"Name",value:"query"},value:{kind:"Variable",name:{kind:"Name",value:"query"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"bulkOperation"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"type"}},{kind:"Field",name:{kind:"Name",value:"completedAt"}},{kind:"Field",name:{kind:"Name",value:"createdAt"}},{kind:"Field",name:{kind:"Name",value:"errorCode"}},{kind:"Field",name:{kind:"Name",value:"id"}},{kind:"Field",name:{kind:"Name",value:"objectCount"}},{kind:"Field",name:{kind:"Name",value:"partialDataUrl"}},{kind:"Field",name:{kind:"Name",value:"status"}},{kind:"Field",name:{kind:"Name",value:"url"}},{kind:"Field",name:{kind:"Name",value:"__typename"}}]}},{kind:"Field",name:{kind:"Name",value:"userErrors"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"code"}},{kind:"Field",name:{kind:"Name",value:"field"}},{kind:"Field",name:{kind:"Name",value:"message"}},{kind:"Field",name:{kind:"Name",value:"__typename"}}]}},{kind:"Field",name:{kind:"Name",value:"__typename"}}]}}]}}]};async function Y(n){let{adminSession:e,query:a,version:i}=n;return(await c({query:X,session:e,variables:{query:a},...i&&{version:i}})).bulkOperationRunQuery}r();r();r();var Z={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"mutation",name:{kind:"Name",value:"StagedUploadsCreate"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"input"}},type:{kind:"NonNullType",type:{kind:"ListType",type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"StagedUploadInput"}}}}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"stagedUploadsCreate"},arguments:[{kind:"Argument",name:{kind:"Name",value:"input"},value:{kind:"Variable",name:{kind:"Name",value:"input"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"stagedTargets"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"url"}},{kind:"Field",name:{kind:"Name",value:"resourceUrl"}},{kind:"Field",name:{kind:"Name",value:"parameters"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"name"}},{kind:"Field",name:{kind:"Name",value:"value"}},{kind:"Field",name:{kind:"Name",value:"__typename"}}]}},{kind:"Field",name:{kind:"Name",value:"__typename"}}]}},{kind:"Field",name:{kind:"Name",value:"userErrors"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"field"}},{kind:"Field",name:{kind:"Name",value:"message"}},{kind:"Field",name:{kind:"Name",value:"__typename"}}]}},{kind:"Field",name:{kind:"Name",value:"__typename"}}]}}]}}]};async function ee(n){let{adminSession:e,variablesJsonl:a}=n,i=Buffer.from(a??"","utf-8"),t="bulk-variables.jsonl",s=i.length,o=await pe(e,t,s),p=me(o);return await ke(i,p.url,p.parameters,t),p.stagedUploadKey}async function pe(n,e,a){return c({query:Z,session:n,variables:{input:[{filename:e,fileSize:a.toString(),httpMethod:"POST",mimeType:"text/jsonl",resource:"BULK_MUTATION_VARIABLES"}]}})}function me(n){if(!n.stagedUploadsCreate)throw new d("No response received from stagedUploadsCreate mutation");if(n.stagedUploadsCreate.userErrors.length>0){let i=n.stagedUploadsCreate.userErrors.map(t=>t.message).join(", ");throw new d(`Failed to create staged upload: ${i}`)}let e=n.stagedUploadsCreate.stagedTargets?.[0];if(!e)throw new d("No staged upload target returned from Shopify");if(!e.url||!e.resourceUrl)throw new d("Invalid staged upload target: missing required URLs");let a=e.parameters.find(i=>i.name==="key")?.value;if(!a)throw new d("No key parameter found in staged upload target");return{url:e.url,resourceUrl:e.resourceUrl,parameters:e.parameters,stagedUploadKey:a}}async function ke(n,e,a,i){let t=new FormData;for(let o of a)t.append(o.name,o.value);t.append("file",new Blob([new Uint8Array(n)],{type:"text/jsonl"}),i);let s=await f({title:u`Uploading bulk operation variables`,task:async()=>b(e,{method:"POST",body:t},"slow-request"),renderOptions:{stdout:process.stderr}});if(!s.ok){let o=await s.text();throw new d(`Failed to upload file to staged URL: ${s.statusText}
${o}`)}}r();var ne={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"mutation",name:{kind:"Name",value:"BulkOperationRunMutation"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"mutation"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"stagedUploadPath"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"clientIdentifier"}},type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"bulkOperationRunMutation"},arguments:[{kind:"Argument",name:{kind:"Name",value:"mutation"},value:{kind:"Variable",name:{kind:"Name",value:"mutation"}}},{kind:"Argument",name:{kind:"Name",value:"stagedUploadPath"},value:{kind:"Variable",name:{kind:"Name",value:"stagedUploadPath"}}},{kind:"Argument",name:{kind:"Name",value:"clientIdentifier"},value:{kind:"Variable",name:{kind:"Name",value:"clientIdentifier"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"bulkOperation"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"type"}},{kind:"Field",name:{kind:"Name",value:"completedAt"}},{kind:"Field",name:{kind:"Name",value:"createdAt"}},{kind:"Field",name:{kind:"Name",value:"errorCode"}},{kind:"Field",name:{kind:"Name",value:"id"}},{kind:"Field",name:{kind:"Name",value:"objectCount"}},{kind:"Field",name:{kind:"Name",value:"partialDataUrl"}},{kind:"Field",name:{kind:"Name",value:"status"}},{kind:"Field",name:{kind:"Name",value:"url"}},{kind:"Field",name:{kind:"Name",value:"__typename"}}]}},{kind:"Field",name:{kind:"Name",value:"userErrors"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"code"}},{kind:"Field",name:{kind:"Name",value:"field"}},{kind:"Field",name:{kind:"Name",value:"message"}},{kind:"Field",name:{kind:"Name",value:"__typename"}}]}},{kind:"Field",name:{kind:"Name",value:"__typename"}}]}}]}}]};async function te(n){let{adminSession:e,query:a,variablesJsonl:i,version:t}=n,s=await ee({adminSession:e,variablesJsonl:i});return(await c({query:ne,session:e,variables:{mutation:a,stagedUploadPath:s},...t&&{version:t}})).bulkOperationRunMutation}r();var ce=["COMPLETED","FAILED","CANCELED","EXPIRED"],ie=1,fe=5,ae=10,ge=3e3,ye=300;async function oe(n,e){return f({title:u`Starting bulk operation`,task:async()=>{let a=Date.now(),i=se({adminSession:n,operationId:e,pollIntervalSeconds:ye/1e3,useAdaptivePolling:!1}),t;do{let{value:s,done:o}=await i.next();if(t=s,o)return t}while(Date.now()-a<ge);return t},renderOptions:{stdout:process.stderr}})}async function re(n,e,a,i){return f({title:u`Polling bulk operation`,task:async t=>{let s=se({adminSession:n,operationId:e,pollIntervalSeconds:fe,initialPollIntervalSeconds:ie,initialPollCount:ae,useAdaptivePolling:!0,abortSignal:a});for(;;){let{value:o,done:p}=await s.next();if(p)return o;t(B(o))}},onAbort:i,renderOptions:{stdout:process.stderr}})}async function*se({adminSession:n,operationId:e,pollIntervalSeconds:a,useAdaptivePolling:i=!1,initialPollIntervalSeconds:t=ie,initialPollCount:s=ae,abortSignal:o}){let p=0;for(;;){let O=await ve(n,e);if(!O.bulkOperation)throw new Error("bulk operation not found");let v=O.bulkOperation;if(ce.includes(v.status)||o?.aborted)return v;yield v,p++;let k=a;i&&p<=s&&(k=t),o?await Promise.race([F(k),new Promise(S=>o.addEventListener("abort",S))]):await F(k)}}async function ve(n,e){return c({query:W,session:n,variables:{id:e},version:w})}r();async function le(n){let e=await b(n);if(!e.ok)throw new d(`Failed to download bulk operation results: ${e.statusText}`);return e.text()}async function Se(n,e){if(n)return n.join(`
`);if(e){if(!await P(e))throw new d(u`Variable file not found at ${m.path(e)}. Please check the path and try again.`);return I(e,{encoding:"utf8"})}else return}async function de(n){let{organization:e,remoteApp:a,store:i,query:t,variables:s,variableFile:o,outputFile:p,watch:O=!1,version:v}=n,{adminSession:k,version:S}=await f({title:u`Authenticating`,task:async()=>{let l=await j(a,i.shopDomain),g=await z({adminSession:l,userSpecifiedVersion:v,minimumDefaultVersion:w});return{adminSession:l,version:g}},renderOptions:{stdout:process.stderr}}),U=await Se(s,o);be(t,U),J(t,i),E({headline:"Starting bulk operation.",body:[{list:{items:G({organization:e,remoteApp:a,storeFqdn:i.shopDomain,version:S})}}]});let A=N(t)?await te({adminSession:k,query:t,variablesJsonl:U,version:S}):await Y({adminSession:k,query:t,version:S});if(A?.userErrors?.length){R({headline:"Error creating bulk operation.",body:{list:{items:A.userErrors.map(l=>l.field?`${l.field.join(".")}: ${l.message}`:l.message)}}});return}let T=A?.bulkOperation;if(T)if(O){let l=new _,g=await re(k,T.id,l.signal,()=>l.abort());l.signal.aborted?E({headline:`Bulk operation ${g.id} is still running in the background.`,body:h(g.id)}):await ue(g,p)}else{let l=await oe(k,T.id);["FAILED","CANCELED","EXPIRED"].includes(l.status)?await ue(l,p):y({headline:"Bulk operation is running.",body:h(l.id),customSections:[{body:[{list:{items:[u`ID: ${m.cyan(l.id)}`.value]}}]}]})}else throw C({headline:"Bulk operation not created successfully.",body:"This is an unexpected error. Please try again later."}),new x("Bulk operation response returned null with no error message.")}async function ue(n,e){let a=B(n).value,t=[{body:[{list:{items:[u`ID: ${m.cyan(n.id)}`.value,u`Status: ${m.yellow(n.status)}`.value,u`Created at: ${m.gray(String(n.createdAt))}`.value,...n.completedAt?[u`Completed at: ${m.gray(String(n.completedAt))}`.value]:[]]}}]}];switch(n.status){case"CREATED":y({headline:"Bulk operation started.",body:h(n.id),customSections:t});break;case"RUNNING":y({headline:"Bulk operation is running.",body:h(n.id),customSections:t});break;case"COMPLETED":if(n.url){let s=await le(n.url),o=Oe(s);e?await D(e,s):L(s),o?C({headline:"Bulk operation completed with errors.",body:e?`Results written to ${e}. Check file for error details.`:"Check results for error details.",customSections:t}):y({headline:a,body:e?[`Results written to ${e}`]:void 0,customSections:t})}else y({headline:a,customSections:t});break;case"CANCELED":case"CANCELING":case"EXPIRED":case"FAILED":R({headline:a,customSections:t});break}}function Oe(n){return n.trim().split(`
`).some(a=>{let i=JSON.parse(a);if(!i.data)return!1;let t=Object.values(i.data)[0];return t?.userErrors!==void 0&&t.userErrors.length>0})}function be(n,e){if(N(n)&&!e)throw new d(u`Bulk mutations require variables. Provide a JSONL file with ${m.yellow("--variable-file")} or individual JSON objects with ${m.yellow("--variables")}.`);if(!N(n)&&e)throw new d(u`The ${m.yellow("--variables")} and ${m.yellow("--variable-file")} flags can only be used with mutations, not queries.`)}function h(n){return[`Monitor its progress with:
`,{command:`shopify app bulk status --id=${H(n)}`}]}var M=class n extends ${static{this.summary="Execute bulk operations."}static{this.descriptionWithMarkdown=`Executes an Admin API GraphQL query or mutation on the specified store, as a bulk operation. Mutations are only allowed on dev stores.
Bulk operations allow you to process large amounts of data asynchronously. Learn more about [bulk query operations](https://shopify.dev/docs/api/usage/bulk-operations/queries) and [bulk mutation operations](https://shopify.dev/docs/api/usage/bulk-operations/imports).
Use [\`bulk status\`](https://shopify.dev/docs/api/shopify-cli/app/app-bulk-status) to check the status of your bulk operations.`}static{this.description=this.descriptionWithoutMarkdown()}static{this.flags={...V,...q,...Q}}async run(){let{flags:e}=await this.parse(n),{query:a,appContextResult:i,store:t}=await K(e);return await de({organization:i.organization,remoteApp:i.remoteApp,store:t,query:a,variables:e.variables,variableFile:e["variable-file"],watch:e.watch??!1,outputFile:e["output-file"],...e.version&&{version:e.version}}),{app:i.app}}};export{M as default};