@upstash/react-cli
Advanced tools
Comparing version 1.0.10 to 1.0.11
@@ -1,341 +0,1 @@ | ||
"use strict"; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
RedisCli: () => RedisCli | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/redis-cli.tsx | ||
var import_react = require("react"); | ||
var ScrollArea = __toESM(require("@radix-ui/react-scroll-area")); | ||
var import_jsx_runtime = require("react/jsx-runtime"); | ||
var RedisCli = (props) => { | ||
const [history, setHistory] = (0, import_react.useState)([]); | ||
const [historyIndex, setHistoryIndex] = (0, import_react.useState)(null); | ||
const [results, setResults] = (0, import_react.useState)([]); | ||
function addCommand(command) { | ||
command.time ??= Date.now(); | ||
setHistory((prev) => { | ||
if (prev.length === 0 || prev[0] !== command.command) { | ||
return [command.command, ...prev]; | ||
} | ||
return prev; | ||
}); | ||
setResults((prev) => [...prev, command]); | ||
} | ||
const [loading, setLoading] = (0, import_react.useState)(false); | ||
const [stdin, setStdin] = (0, import_react.useState)(""); | ||
const specialCommands = { | ||
clear: (_ctx) => { | ||
setResults([]); | ||
setHistoryIndex(null); | ||
setStdin(""); | ||
}, | ||
help: (ctx) => { | ||
ctx.addCommand({ | ||
time: Date.now(), | ||
command: "help", | ||
result: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [ | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { children: "You can execute Redis commands in the terminal:" }), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)( | ||
"a", | ||
{ | ||
className: "upstash-cli-link", | ||
href: "https://upstash.com/redis-api-compatibility", | ||
target: "_blank", | ||
rel: "noreferrer", | ||
children: "https://upstash.com/redis-api-compatibility" | ||
} | ||
), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("br", {}), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "upstash-cli-help-command", children: [ | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "Try" }), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { className: "upstash-cli-code", onClick: () => ctx.setStdin("GET key"), children: "> GET key" }), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { className: "upstash-cli-code", onClick: () => ctx.setStdin("LPUSH list e1 e2"), children: "> LPUSH list" }) | ||
] }), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "upstash-cli-help-command", children: [ | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "Special commands" }), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("button", { className: "upstash-cli-code", onClick: () => ctx.setStdin("clear"), children: [ | ||
"> clear", | ||
" " | ||
] }), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("button", { className: "upstash-cli-code", onClick: () => ctx.setStdin("help"), children: [ | ||
"> help", | ||
" " | ||
] }) | ||
] }) | ||
] }) | ||
}); | ||
}, | ||
...props.commands | ||
}; | ||
async function runRedisCommand(command) { | ||
try { | ||
const args = splitArgs(command); | ||
const res = await fetch(props.url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${props.token}` | ||
}, | ||
body: JSON.stringify(args) | ||
}); | ||
const json = await res.json(); | ||
addCommand({ command, result: json.error ?? json.result, error: !!json.error, time: Date.now() }); | ||
} catch (e) { | ||
console.error(e); | ||
addCommand({ command, error: true, result: e.message, time: Date.now() }); | ||
} | ||
} | ||
(0, import_react.useEffect)(() => { | ||
if (historyIndex === null) { | ||
return; | ||
} | ||
const cmd = history[historyIndex]; | ||
if (!cmd) { | ||
return; | ||
} | ||
setStdin(cmd); | ||
}, [historyIndex, history]); | ||
const ref = (0, import_react.useRef)(null); | ||
const onEnter = async () => { | ||
try { | ||
if (loading) { | ||
return; | ||
} | ||
setLoading(true); | ||
const command = stdin.trim(); | ||
if (!command) { | ||
addCommand({ command, time: Date.now() }); | ||
return; | ||
} | ||
if (specialCommands[command]) { | ||
specialCommands[command]({ | ||
setStdin, | ||
addCommand | ||
}); | ||
return; | ||
} | ||
await runRedisCommand(command); | ||
} catch (e) { | ||
const err = e; | ||
console.error(err.message); | ||
} finally { | ||
setHistoryIndex(null); | ||
setLoading(false); | ||
setStdin(""); | ||
setTimeout(() => ref.current?.scrollIntoView({ behavior: "smooth", block: "nearest" }), 10); | ||
} | ||
}; | ||
(0, import_react.useEffect)(() => { | ||
async function run() { | ||
if (!props.init) { | ||
return; | ||
} | ||
try { | ||
const commands = Array.isArray(props.init) ? props.init : [props.init]; | ||
for (const command of commands) { | ||
const cmd = specialCommands[command]; | ||
if (cmd) { | ||
cmd({ | ||
setStdin, | ||
addCommand | ||
}); | ||
} else { | ||
await runRedisCommand(command); | ||
} | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
run(); | ||
}, [props.init]); | ||
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( | ||
"div", | ||
{ | ||
className: `upstash-cli ${props.className}`, | ||
onMouseUp: () => { | ||
if (window?.getSelection()?.type !== "Range") { | ||
ref.current?.focus(); | ||
} | ||
}, | ||
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ScrollArea.Root, { style: { overflow: "hidden" }, children: [ | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ScrollArea.Viewport, { className: "upstash-cli-viewport", children: [ | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { style: { color: "#00e9a3" }, children: props.welcome ?? "Welcome to Upstash CLI" }), | ||
results.map((r) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Result, { result: r }, r.time)), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Line, { className: loading ? "upstash-cli-loading" : "", prefix: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "\u279C" }), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( | ||
"input", | ||
{ | ||
type: "text", | ||
spellCheck: false, | ||
style: { | ||
resize: "none" | ||
}, | ||
ref, | ||
value: stdin, | ||
onChange: (e) => setStdin(e.currentTarget.value), | ||
onKeyDown: async (e) => { | ||
if (e.ctrlKey && e.key === "c") { | ||
e.preventDefault(); | ||
addCommand({ command: `${stdin}^C`, time: Date.now() }); | ||
setStdin(""); | ||
return; | ||
} | ||
if (e.key === "Enter" && !e.shiftKey) { | ||
e.preventDefault(); | ||
await onEnter(); | ||
return; | ||
} | ||
if (e.key === "ArrowUp") { | ||
if (history.length === 0) { | ||
setHistoryIndex(null); | ||
return; | ||
} | ||
setHistoryIndex(Math.min(history.length - 1, historyIndex === null ? 0 : historyIndex + 1)); | ||
return; | ||
} | ||
if (e.key === "ArrowDown") { | ||
if (history.length === 0) { | ||
setHistoryIndex(null); | ||
return; | ||
} | ||
setHistoryIndex(Math.max(0, historyIndex === null ? history.length - 1 : historyIndex - 1)); | ||
return; | ||
} | ||
}, | ||
className: "upstash-cli-stdin" | ||
}, | ||
"stdin" | ||
) }) | ||
] }), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ScrollArea.Scrollbar, { className: "upstash-cli-scrollbar", orientation: "vertical", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ScrollArea.Thumb, { className: "upstash-cli-scrollbar-thumb" }) }), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ScrollArea.Corner, { style: { background: "black" } }) | ||
] }) | ||
} | ||
); | ||
}; | ||
var Line = ({ | ||
className, | ||
prefix, | ||
children | ||
}) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: ["upstash-cli-line", className].join(" "), children: [ | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "upstash-cli-line-prefix", children: prefix ?? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: " " }) }), | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "upstash-cli-line-content", children }) | ||
] }); | ||
function splitArgs(input) { | ||
const separator = /\s/g; | ||
let singleQuoteOpen = false; | ||
let doubleQuoteOpen = false; | ||
let tokenBuffer = []; | ||
const ret = []; | ||
const arr = input.split(""); | ||
for (let i = 0; i < arr.length; ++i) { | ||
const element = arr[i]; | ||
const matches = element.match(separator); | ||
if (element === "'" && !doubleQuoteOpen) { | ||
singleQuoteOpen = !singleQuoteOpen; | ||
continue; | ||
} else if (element === '"' && !singleQuoteOpen) { | ||
doubleQuoteOpen = !doubleQuoteOpen; | ||
continue; | ||
} | ||
if (!(singleQuoteOpen || doubleQuoteOpen) && matches) { | ||
if (tokenBuffer.length > 0) { | ||
ret.push(tokenBuffer.join("")); | ||
tokenBuffer = []; | ||
} else { | ||
ret.push(element); | ||
} | ||
} else { | ||
tokenBuffer.push(element); | ||
} | ||
} | ||
if (tokenBuffer.length > 0) { | ||
ret.push(tokenBuffer.join("")); | ||
} else { | ||
ret.push(""); | ||
} | ||
return ret; | ||
} | ||
var Result = ({ result }) => { | ||
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ | ||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)( | ||
Line, | ||
{ | ||
prefix: result.error ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)( | ||
"span", | ||
{ | ||
style: { | ||
color: "#EF4444" | ||
}, | ||
children: "\u2717" | ||
} | ||
) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "\u279C" }), | ||
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: result.command }) | ||
} | ||
), | ||
typeof result.result !== "undefined" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Line, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( | ||
"div", | ||
{ | ||
className: "upstash-cli-result", | ||
style: { | ||
color: result.error ? "#EF4444" : void 0 | ||
}, | ||
children: formatResult(result.result) | ||
} | ||
) }) : null | ||
] }); | ||
}; | ||
function formatResult(result) { | ||
switch (typeof result) { | ||
case "undefined": | ||
return ""; | ||
case "boolean": | ||
case "number": | ||
return result.toString(); | ||
case "object": | ||
if (result === null) { | ||
return "nil"; | ||
} | ||
if (Array.isArray(result)) { | ||
return result.map(formatResult).join(", "); | ||
} | ||
default: | ||
return result; | ||
} | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
RedisCli | ||
}); | ||
//# sourceMappingURL=index.js.map | ||
"use strict";var x=Object.create;var y=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var L=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var T=(e,s)=>{for(var a in s)y(e,a,{get:s[a],enumerable:!0})},S=(e,s,a,i)=>{if(s&&typeof s=="object"||typeof s=="function")for(let r of E(s))!P.call(e,r)&&r!==a&&y(e,r,{get:()=>s[r],enumerable:!(i=D(s,r))||i.enumerable});return e};var j=(e,s,a)=>(a=e!=null?x(L(e)):{},S(s||!e||!e.__esModule?y(a,"default",{value:e,enumerable:!0}):a,e)),M=e=>S(y({},"__esModule",{value:!0}),e);var U={};T(U,{RedisCli:()=>F});module.exports=M(U);var l=require("react"),u=j(require("@radix-ui/react-scroll-area"));var n=require("react/jsx-runtime"),F=e=>{let[s,a]=(0,l.useState)([]),[i,r]=(0,l.useState)(null),[d,f]=(0,l.useState)([]);function c(t){t.time??=Date.now(),a(o=>o.length===0||o[0]!==t.command?[t.command,...o]:o),f(o=>[...o,t])}let[m,g]=(0,l.useState)(!1),[C,p]=(0,l.useState)(""),b={clear:t=>{f([]),r(null),p("")},help:t=>{t.addCommand({time:Date.now(),command:"help",result:(0,n.jsxs)("div",{children:[(0,n.jsx)("p",{children:"You can execute Redis commands in the terminal:"}),(0,n.jsx)("a",{className:"upstash-cli-link",href:"https://upstash.com/redis-api-compatibility",target:"_blank",rel:"noreferrer",children:"https://upstash.com/redis-api-compatibility"}),(0,n.jsx)("br",{}),(0,n.jsxs)("div",{className:"upstash-cli-help-command",children:[(0,n.jsx)("span",{children:"Try"}),(0,n.jsx)("button",{className:"upstash-cli-code",onClick:()=>t.setStdin("GET key"),children:"> GET key"}),(0,n.jsx)("button",{className:"upstash-cli-code",onClick:()=>t.setStdin("LPUSH list e1 e2"),children:"> LPUSH list"})]}),(0,n.jsxs)("div",{className:"upstash-cli-help-command",children:[(0,n.jsx)("span",{children:"Special commands"}),(0,n.jsxs)("button",{className:"upstash-cli-code",onClick:()=>t.setStdin("clear"),children:["> clear"," "]}),(0,n.jsxs)("button",{className:"upstash-cli-code",onClick:()=>t.setStdin("help"),children:["> help"," "]})]})]})})},...e.commands};async function N(t){try{let o=H(t),h=await(await fetch(e.url,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.token}`},body:JSON.stringify(o)})).json();c({command:t,result:h.error??h.result,error:!!h.error,time:Date.now()})}catch(o){console.error(o),c({command:t,error:!0,result:o.message,time:Date.now()})}}(0,l.useEffect)(()=>{if(i===null)return;let t=s[i];t&&p(t)},[i,s]);let R=(0,l.useRef)(null),A=async()=>{try{if(m)return;g(!0);let t=C.trim();if(!t){c({command:t,time:Date.now()});return}if(b[t]){b[t]({setStdin:p,addCommand:c});return}await N(t)}catch(t){console.error(t.message)}finally{r(null),g(!1),p(""),setTimeout(()=>R.current?.scrollIntoView({behavior:"smooth",block:"nearest"}),10)}};return(0,l.useEffect)(()=>{async function t(){if(e.init)try{let o=Array.isArray(e.init)?e.init:[e.init];for(let v of o){let h=b[v];h?h({setStdin:p,addCommand:c}):await N(v)}}catch(o){console.error(o)}}t()},[e.init]),(0,n.jsx)("div",{className:`upstash-cli ${e.className}`,onMouseUp:()=>{window?.getSelection()?.type!=="Range"&&R.current?.focus()},children:(0,n.jsxs)(u.Root,{style:{overflow:"hidden"},children:[(0,n.jsxs)(u.Viewport,{className:"upstash-cli-viewport",children:[(0,n.jsx)("span",{style:{color:"#00e9a3"},children:e.welcome??"Welcome to Upstash CLI"}),d.map(t=>(0,n.jsx)(I,{result:t},t.time)),(0,n.jsx)(w,{className:m?"upstash-cli-loading":"",prefix:(0,n.jsx)("span",{children:"\u279C"}),children:(0,n.jsx)("input",{type:"text",spellCheck:!1,style:{resize:"none"},ref:R,value:C,onChange:t=>p(t.currentTarget.value),onKeyDown:async t=>{if(t.ctrlKey&&t.key==="c"){t.preventDefault(),c({command:`${C}^C`,time:Date.now()}),p("");return}if(t.key==="Enter"&&!t.shiftKey){t.preventDefault(),await A();return}if(t.key==="ArrowUp"){if(s.length===0){r(null);return}r(Math.min(s.length-1,i===null?0:i+1));return}if(t.key==="ArrowDown"){if(s.length===0){r(null);return}r(Math.max(0,i===null?s.length-1:i-1));return}},className:"upstash-cli-stdin"},"stdin")})]}),(0,n.jsx)(u.Scrollbar,{className:"upstash-cli-scrollbar",orientation:"vertical",children:(0,n.jsx)(u.Thumb,{className:"upstash-cli-scrollbar-thumb"})}),(0,n.jsx)(u.Corner,{style:{background:"black"}})]})})},w=({className:e,prefix:s,children:a})=>(0,n.jsxs)("div",{className:["upstash-cli-line",e].join(" "),children:[(0,n.jsx)("div",{className:"upstash-cli-line-prefix",children:s??(0,n.jsx)("span",{children:" "})}),(0,n.jsx)("div",{className:"upstash-cli-line-content",children:a})]});function H(e){let s=/\s/g,a=!1,i=!1,r=[],d=[],f=e.split("");for(let c=0;c<f.length;++c){let m=f[c],g=m.match(s);if(m==="'"&&!i){a=!a;continue}else if(m==='"'&&!a){i=!i;continue}!(a||i)&&g?r.length>0?(d.push(r.join("")),r=[]):d.push(m):r.push(m)}return r.length>0?d.push(r.join("")):d.push(""),d}var I=({result:e})=>(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(w,{prefix:e.error?(0,n.jsx)("span",{style:{color:"#EF4444"},children:"\u2717"}):(0,n.jsx)("span",{children:"\u279C"}),children:(0,n.jsx)("span",{children:e.command})}),typeof e.result<"u"?(0,n.jsx)(w,{children:(0,n.jsx)("div",{className:"upstash-cli-result",style:{color:e.error?"#EF4444":void 0},children:k(e.result)})}):null]});function k(e){switch(typeof e){case"undefined":return"";case"boolean":case"number":return e.toString();case"object":if(e===null)return"nil";if(Array.isArray(e))return e.map(k).join(", ");default:return e}}0&&(module.exports={RedisCli}); |
{ | ||
"name": "@upstash/react-cli", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"main": "./dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
15269
6
85
1