UNPKG

bunny-move

Version:

Move files to and from your storage zones with bunny.net in a single cli command 🐰

3 lines (2 loc) 8.85 kB
import { createRequire } from 'module';const require = createRequire(import.meta.url); import{argument as Ye}from"pastel";import qe from"react";import{z as le}from"zod";import C from"figures";import{Newline as u,Text as s}from"ink";import ue from"ink-spinner";import{Fragment as de,jsx as a,jsxs as p}from"react/jsx-runtime";function B({feedback:e}){let t=p(de,{children:[a(u,{}),e.pastStories.map(r=>p(s,{children:[a(s,{color:"green",children:C.tick}),p(s,{color:"white",children:[" ",r]}),a(u,{})]},r))]});return e.error?p(s,{children:[t,p(s,{children:[a(s,{color:"red",children:C.cross}),p(s,{color:"white",children:[" ",e.story]}),a(u,{}),a(u,{}),a(s,{color:"red",children:e.error}),a(u,{})]})]}):e.done?p(s,{children:[t,p(s,{children:[a(s,{color:"magenta",children:C.heart}),a(s,{color:"white",children:" Done"}),a(u,{})]})]}):p(s,{children:[t,p(s,{children:[a(s,{color:"cyan",children:a(ue,{})}),p(s,{color:"white",children:[" ",e.story]}),a(s,{children:e.tasks.map(r=>p(s,{children:[a(u,{}),a(s,{color:"gray",children:r})]},r))}),a(u,{})]})]})}import{createAction as h,createReducer as ye}from"@reduxjs/toolkit";import R from"react";function F(e){return e.at(0).toUpperCase()+e.slice(1)}function fe(e,t){return Object.fromEntries(Object.entries(t).map(([r,o])=>[`dispatch${F(r)}`,(...n)=>{e(o(...n))}]))}function L(e,t,r){let[o,n]=R.useReducer(e,t),i=R.useMemo(()=>fe(n,r),[n,r]);return[o,i]}var K=h("feedback/epic/end"),N=h("feedback/epic/error"),D=h("feedback/story/change"),O=h("feedback/tasks/end"),z=h("feedback/tasks/start"),ge={epicEnd:K,epicError:N,storyChange:D,taskEnd:O,taskStart:z},v={done:!1,error:null,pastStories:[],story:"",tasks:[]},he=ye(v,e=>e.addCase(D,(t,r)=>{t.story!==""&&t.pastStories.push(t.story),t.story=r.payload}).addCase(z,(t,r)=>{t.tasks.push(r.payload)}).addCase(O,(t,r)=>{let o=t.tasks.indexOf(r.payload);o!==-1&&t.tasks.splice(o,1)}).addCase(K,t=>{t.story!==""&&t.pastStories.push(t.story),t.story="",t.done=!0}).addCase(N,(t,r)=>{t.error=r.payload}));function I(){return L(he,v,ge)}import{useApp as xe}from"ink";import be from"react";function j(e){let{exit:t}=xe();be.useEffect(()=>{(e.done||e.error)&&(e.error&&(process.exitCode=1,process.env.CI&&(console.error(e.error),console.error())),t())},[e.done,e.error,t])}function Ce(e){return typeof e=="object"&&e!==null&&"message"in e&&typeof e.message=="string"}function Pe(e){if(Ce(e))return e;try{return new Error(JSON.stringify(e))}catch{return new Error(String(e))}}function M(e){return Pe(e).message}import{option as ke}from"pastel";import{z as U}from"zod";var $=U.string().trim().uuid(),Ae=U.preprocess(e=>process.env.BUNNY_ACCESS_KEY&&!e?process.env.BUNNY_ACCESS_KEY.split(","):e,$.or($.array()).optional().transform(e=>typeof e=="string"?[e]:e)),G=Ae.describe(ke({alias:"k",description:"Bunny API Access Key",valueDescription:"uuid"}));import{option as Te}from"pastel";import{z as _}from"zod";var W=_.string().trim().min(1),we=_.preprocess(e=>process.env.BUNNY_PROFILE&&!e?process.env.BUNNY_PROFILE.split(","):e,W.or(W.array()).optional().transform(e=>typeof e=="string"?[e]:e)),V=we.describe(Te({alias:"p",description:"Bunny user profile name",valueDescription:"string"}));import Ee from"node:path";import{option as Be}from"pastel";import Fe from"untildify";import{z as q}from"zod";import Se from"node:path";import Ze from"untildify";import{z as g}from"zod";var P="~/.bunny/credentials",b=Se.resolve(Ze(P)),Y=g.object({accessKey:g.string(),email:g.string(),id:g.string(),name:g.string().optional(),profile:g.string()});var Re=P,Le=q.preprocess(e=>{if(process.env.BUNNY_SHARED_CREDENTIALS_FILE&&!e){let t=process.env.BUNNY_SHARED_CREDENTIALS_FILE.trim();return Ee.resolve(Fe(t))}return e},q.string().trim().optional().default(b)),J=Le.describe(Be({alias:"c",defaultValueDescription:Re,description:"Bunny shared credentials file path",valueDescription:"file-path"}));import{createBunnyApiClient as ve}from"bunny-sdk";import Ie from"chalk";import je from"p-map";import Ke from"node:os";var f=Ke.cpus().length;var k=new Intl.ListFormat("en",{style:"long",type:"conjunction"});function y(){}import Oe from"p-map";function H(e){return typeof e=="number"?Number.isInteger(e):typeof e=="string"&&e.trim()!==""?Number.isInteger(Number(e)):!1}import vt from"tiny-invariant";async function X({bunnyApiClient:e,id:t,includeCertificate:r=!0}){return e.pullzone.byId(t).get({queryParameters:{includeCertificate:r}})}import Mt from"tiny-invariant";async function Q({bunnyApiClient:e,includeCertificate:t=!0,name:r}){let o=await e.pullzone.get({queryParameters:{includeCertificate:t,page:1,perPage:1,search:r}});return o?.items?.[0]?.name===r?o?.items?.[0]:void 0}import Ne from"node:path";import De from"untildify";function ee(e){return[...new Set(e.map(t=>{if(t.at(0)==="/"||t.startsWith("./")||t.startsWith("../")||t.startsWith("~/")||t==="."||t==="~"){let r=Ne.resolve(De(t));return r.at(-1)!=="/"?r+"/":r}return t}))]}async function te({bunnyApiClients:e,dispatchTaskEnd:t=y,dispatchTaskStart:r=y,locations:o}){if(o=ee(o),o.some(i=>i.at(0)==="/"))throw new Error(`All locations found in "${JSON.stringify(o)}" must be pull zones.`);return await Oe(o,async i=>{r(i);let m=await ze({bunnyApiClients:e,input:i});return t(i),m},{concurrency:f})}async function ze({bunnyApiClients:e,input:t}){if(t.includes("/"))throw new Error(`Location "${t}" is invalid. Pull zone names cannot include "/".`);for(let r of e){if(H(t)){let n=await X({bunnyApiClient:r,id:Number.parseInt(t,10)});if(n)return{bunnyApiClient:r,pullZone:n,type:"pull-zone"};continue}let o=await Q({bunnyApiClient:r,name:t});if(o)return{bunnyApiClient:r,pullZone:o,type:"pull-zone"}}throw new Error(`Pull zone "${t}" cannot be found. Does it exist in your account?`)}async function re({accessKeys:e,dispatchStoryChange:t=y,dispatchTaskEnd:r=y,dispatchTaskStart:o=y,highlight:n=Ie.white,locations:i}){t(`Validate locations ${k.format(i.map(c=>`"${n(c)}"`))}`);let m=e.map(c=>ve({accessKey:c})),d=await te({bunnyApiClients:m,dispatchTaskEnd:r,dispatchTaskStart:o,locations:i});t(`Purge pull zone caches for ${k.format(d.map(c=>`"${n(c.pullZone.name)}"`))}`),await je(d,async c=>{o(c.pullZone.name),await c.bunnyApiClient.pullzone.byId(c.pullZone.id).purgeCache.post({}),r(c.pullZone.name)},{concurrency:f})}import Ge from"camelcase-keys";import We from"fast-deep-equal/es6/index.js";import T from"fs-extra";import _e from"is-obj";import ae from"p-map";import{parse as Ve}from"smol-toml";function oe(e,t){return e.filter((r,o)=>o===e.findIndex(n=>n[t]===r[t]))}import{createBunnyApiClient as Me}from"bunny-sdk";import $e from"is-obj";function ne({firstName:e,lastName:t}){if(e&&t)return`${e} ${t}`;if(t)return t;if(e)return e}async function A({accessKey:e,profile:t=e}){let o=await Me({accessKey:e}).user.get().catch(i=>{throw $e(i)&&"responseStatusCode"in i&&i.responseStatusCode===401?new Error(`Cannot find user with accessKey "${e}"`):i});if(!o)throw new Error(`Cannot find user with accessKey "${e}"`);let n=ne({firstName:o.firstName,lastName:o.lastName});return{accessKey:e,email:o.email,id:o.id,...n&&{name:n},profile:t}}import{stringify as Ue}from"smol-toml";function ie(e,t){return e.reduce((r,o)=>{let{[t]:n,...i}=o;return(typeof n=="string"||typeof n=="number"||typeof n=="symbol")&&(r[n]=i),r},{})}function se(e){return Ue(ie(e.map(t=>({...t,profile:t.profile??t.accessKey})),"profile"))}async function ce({accessKey:e=[],credentialsPath:t=b,profile:r=[]}={}){let o=typeof e=="string"?[e]:e,n=typeof r=="string"?[r]:r,i=await T.pathExists(t),m=[];if(i){let l=await T.readFile(t,"utf8"),me=Ve(l),w=!1;if(m=await ae(Object.entries(me),async([x,S])=>{let Z=Y.parse({..._e(S)?Ge({...S,profile:x}):{}}),E=await A({accessKey:Z.accessKey,profile:x});return We(Z,E)||(w=!0),E},{concurrency:f}),w){let x=se(m);await T.outputFile(t,x)}}let d=await ae(o,async l=>A({accessKey:l,profile:l}),{concurrency:f}),c=oe([...m,...d],"profile");return n.length>0?c.filter(l=>n.includes(l.profile??"")):c}async function pe(e){let t=await ce(e);return[...new Set(t.map(o=>o.accessKey))]}import{jsx as Qe}from"react/jsx-runtime";var Je=le.string().describe(Ye({description:"Pull zone caches to purge",name:"location"})).array(),He=le.object({accessKey:G,profile:V,sharedCredentialsFile:J});function Xe({args:e,options:t}){let[r,{dispatchEpicEnd:o,dispatchEpicError:n,dispatchStoryChange:i,dispatchTaskEnd:m,dispatchTaskStart:d}]=I();return j(r),qe.useEffect(()=>{async function c(){try{i("Validate credentials");let l=await pe({accessKey:t.accessKey,credentialsPath:t.sharedCredentialsFile,profile:t.profile});if(l.length===0)throw new Error("No accessKeys found");await re({accessKeys:l,dispatchStoryChange:i,dispatchTaskEnd:m,dispatchTaskStart:d,locations:e}),o()}catch(l){n(M(l))}}c()},[e,t.accessKey,t.profile,t.sharedCredentialsFile,o,n,i,m,d]),Qe(B,{feedback:r})}export{Je as args,Xe as default,He as options};