UNPKG

@0xobelisk/rooch-cli

Version:
37 lines (36 loc) 9.94 kB
#!/usr/bin/env node import no from"yargs";import{hideBin as so}from"yargs/helpers";import K from"fs";import{execSync as W}from"child_process";import h from"chalk";import{Dubhe as G}from"@0xobelisk/rooch-client";import m from"chalk";import{ZodError as I}from"zod";import{fromZodError as A,ValidationError as O}from"zod-validation-error";var x=class extends Error{name="NotInsideProjectError";message="You are not inside a Dubhe project"},d=class extends Error{name="DubheCliError"};function g(o){if(o instanceof O)console.log(m.redBright(o.message));else if(o instanceof I){let e=A(o,{prefixSeparator:` - `,issueSeparator:` - `});console.log(m.redBright(e.message))}else o instanceof x?(console.log(m.red(o.message)),console.log(""),console.log(m.blue("To learn more about Dubhe's configuration, please go to https://github.com/0xobelisk"))):o instanceof d?console.log(m.red(o)):console.log(o)}import{mkdirSync as B,writeFileSync as Y}from"fs";import{dirname as J}from"path";import{execSync as H,spawn as T}from"child_process";import n from"chalk";function V(){try{let o=process.platform==="win32"?'tasklist /FI "IMAGENAME eq rooch.exe" /FO CSV /NH':'pgrep -f "rooch server start"',e=H(o).toString().trim();return process.platform==="win32"?e.toLowerCase().includes("rooch.exe"):e.length>0}catch{return!1}}async function f(o=!1){let e=process.env.ROOCH_CLI_PASSWORD;if(e===void 0&&(e=""),console.log(` \u{1F50D} Checking Local Node Status...`),console.log(" \u251C\u2500 Scanning running processes"),V()){console.log(n.yellow(` \u26A0\uFE0F Warning: Local Node Already Running`)),console.log(n.yellow(" \u251C\u2500 Cannot start a new instance")),console.log(n.yellow(" \u2514\u2500 Please stop the existing process first"));return}console.log(` \u{1F680} Starting Local Node...`),console.log(" \u251C\u2500 Mode: "+(o?"Background":"Foreground")),console.log(" \u251C\u2500 Faucet: Enabled"),console.log(" \u2514\u2500 Force Regenesis: Yes");try{let t=["server","start","-n","local"];e!==""&&t.push("--password",e);let r=T("rooch",t,{env:{...process.env},stdio:o?"ignore":"inherit",detached:o});r.on("error",s=>{console.error(n.red(` \u274C Failed to Start Local Node`)),console.error(n.red(` \u2514\u2500 Error: ${s.message}`))}),o?(r.unref(),console.log(n.green(` \u2705 Local Node Started in Background`)),process.platform==="win32"?(console.log(` \u{1F4A1} Helpful Commands:`),console.log(" \u251C\u2500 Check Process: tasklist | findstr rooch.exe"),console.log(" \u2514\u2500 Stop Node: taskkill /PID <process_id> /F")):(console.log(` \u{1F4A1} Helpful Commands:`),console.log(" \u251C\u2500 Check Process: pgrep -f 'rooch server start'"),console.log(" \u2514\u2500 Stop Node: kill <process_id>"))):(r.on("exit",s=>{s===0?(console.log(n.green(` \u2705 Local Node Stopped`)),console.log(n.green(" \u2514\u2500 Exit Status: Normal"))):(console.error(n.red(` \u274C Local Node Crashed`)),console.error(n.red(` \u2514\u2500 Exit Code: ${s}`)))}),console.log(n.cyan(` \u{1F4E1} Local Node Running`)),console.log(n.cyan(" \u2514\u2500 Press Ctrl+C to stop")),await new Promise(()=>{}))}catch(t){console.error(n.red(` \u274C Failed to Start Local Node`)),console.error(n.red(` \u2514\u2500 Error: ${t.message}`))}}import{execSync as j}from"child_process";import a from"chalk";async function b(){console.log(` \u{1F50D} Checking Local Node Status...`);try{let o=process.platform==="win32"?'tasklist /FI "IMAGENAME eq rooch.exe" /FO CSV /NH':"ps aux | grep '[r]ooch server start'",e=j(o,{encoding:"utf8"});(process.platform==="win32"?e.toLowerCase().includes("rooch.exe"):e.split(` `).filter(Boolean).length>0)?(console.log(a.green(` \u2705 Node Status: Running`)),console.log(a.gray(` \u{1F4CA} Process Details:`)),console.log(a.gray(` \u2514\u2500 ${e.trim()}`))):(console.log(a.red(` \u274C Node Status: Not Running`)),console.log(a.yellow(` \u{1F4A1} Quick Start:`)),console.log(a.yellow(" \u2514\u2500 Run `dubhe localnode start`")))}catch{console.log(a.red(` \u274C Node Status: Not Running`)),console.log(a.yellow(` \u{1F4A1} Quick Start:`)),console.log(a.yellow(" \u2514\u2500 Run `dubhe localnode start`"))}}import{execSync as k}from"child_process";async function _(){console.log("Stopping local node...");try{let o=process.platform==="win32"?'tasklist /FI "IMAGENAME eq rooch.exe" /FO CSV /NH':"ps aux | grep '[r]ooch server start -n local'",e=k(o,{encoding:"utf8"});if(!e){console.log("No running local node process found");return}let t;if(process.platform==="win32"){let r=e.match(/"rooch\.exe",["']?(\d+)/i);t=r?r[1]:null}else t=e.toString().split(/\s+/)[1];if(!t){console.log("No running local node process found");return}process.platform==="win32"?k(`taskkill /PID ${t} /F`):process.kill(Number(t)),console.log("\u2705 Local node stopped successfully")}catch(o){o.code==="ESRCH"||o.message.includes("no running tasks")?console.log("No running local node process found"):console.error("\u274C Error stopping local node:",o.message)}}function S(o){if(o.startsWith("roochsecretkey"))return o.length===74?o:!1;if(o.startsWith("0x")){let e=o.slice(2);return e.length<=64?e:!1}else return o.length===64?o:!1}function v(o,e,t,r){let s={projectName:o,network:e,packageId:t,version:r},c=process.cwd(),p=JSON.stringify(s,null,2);q(p,`${c}/contracts/${o}/.history/rooch_${e}/latest.json`,"Update deploy log")}async function q(o,e,t){B(J(e),{recursive:!0}),Y(e,o),t!==void 0&&console.log(`${t}: ${e}`)}async function N(o,e,t){let r=process.env.PRIVATE_KEY;if(!r)throw new d(`Missing PRIVATE_KEY environment variable. Run 'echo "PRIVATE_KEY=YOUR_PRIVATE_KEY" > .env' in your contracts directory to use the default aptos private key.`);let s=S(r);if(s===!1)throw new d("Please check your privateKey.");let c=new G({networkType:e,secretKey:s});t===void 0?t=[{name:o.name,address:c.getHexAddress()}]:t.find(i=>i.name===o.name)||t.push({name:o.name,address:c.getHexAddress()});let p=t.map(({name:l,address:i})=>`${l}=${i}`).join(" "),u=process.cwd();try{W(`rooch move build --named-addresses ${p} --json -p ${u}/contracts/${o.name}`,{encoding:"utf-8"})}catch(l){console.error(h.red("Error executing rooch move build:")),console.error(l),process.exit(1)}let y="",w=0;try{let l=K.readFileSync(`${u}/contracts/${o.name}/build/${o.name}/package.rpd`),i=await c.publishPackage({packageBytes:l});if(i.execution_info.status.type!=="executed")throw new d(`Publish failed: ${i.error_info}`);y=c.getHexAddress(),w=1;let F=i.execution_info.tx_hash;console.log(h.blue(`${o.name} PackageId: ${y}`)),v(o.name,e,y,w),console.log(h.green(`Publish Transaction Digest: ${F}`))}catch(l){console.error(h.red("Failed to execute publish, please republish")),console.error(l.message),process.exit(1)}}import U from"chalk";function E(){console.log(U.yellow(` Welcome to Dubhe --from team@obelisk ________ ___ ___ ________ ___ ___ _______ |\\ ___ \\|\\ \\|\\ \\|\\ __ \\|\\ \\|\\ \\|\\ ___ \\ \\ \\ \\_|\\ \\ \\ \\\\\\ \\ \\ \\|\\ /\\ \\ \\\\\\ \\ \\ __/| \\ \\ \\ \\\\ \\ \\ \\\\\\ \\ \\ __ \\ \\ __ \\ \\ \\_|/__ \\ \\ \\_\\\\ \\ \\ \\\\\\ \\ \\ \\|\\ \\ \\ \\ \\ \\ \\ \\_|\\ \\ \\ \\_______\\ \\_______\\ \\_______\\ \\__\\ \\__\\ \\_______\\ \\|_______|\\|_______|\\|_______|\\|__|\\|__|\\|_______| `))}var Q={command:"localnode <action>",describe:"Manage local Sui node",builder(o){return o.positional("action",{describe:"Action to perform",choices:["start","stop","status","restart"],type:"string",demandOption:!0}).option("background",{alias:"b",type:"boolean",description:"Run node in background",default:!1})},async handler(o){let{action:e,background:t}=o;try{switch(e){case"start":await f(t);break;case"stop":await _();break;case"status":await b();break;case"restart":await Z(t);break}}catch(r){console.error("Error executing command:",r),process.exit(1)}}};async function Z(o){console.log("Restarting local Sui node..."),await _(),await f(o),console.log("Local node has been restarted")}var C=Q;import{loadConfig as z}from"@0xobelisk/sui-common";var X={command:"publish",describe:"Publish dubhe move contract",builder(o){return o.options({network:{type:"string",choices:["mainnet","testnet","devnet","localnet"],desc:"Node network (mainnet/testnet/devnet/localnet)"},"config-path":{type:"string",default:"dubhe.config.ts",desc:"Configuration file path"},"named-addresses":{type:"string",desc:'Named addresses in format "name1=address1,name2=address2"',optional:!0}})},async handler({network:o,"config-path":e,"named-addresses":t}){try{let r=await z(e),s=t?t.split(",").map(c=>{let[p,u]=c.split("=");return{name:p,address:u}}):void 0;await N(r,o,s)}catch(r){g(r),process.exit(1)}process.exit(0)}},P=X;import{execSync as oo}from"child_process";import eo from"chalk";var to={command:"test",describe:"Run tests in Dubhe contracts",builder(o){return o.options({packagePath:{type:"string",default:".",description:"Options to pass to forge test"}})},async handler({packagePath:o}){try{oo(`sui move test --path ${o}`,{encoding:"utf-8"})}catch(e){console.error(eo.red("Error executing sui move test:")),console.log(e.stdout),process.exit(0)}}},D=to;var ro={command:"hello",describe:"hello, dubhe",builder(o){return o},async handler(){E()}},$=ro;var R=[P,C,D,$];import*as M from"dotenv";import L from"chalk";M.config();no(so(process.argv)).scriptName("dubhe").command(R).strict().fail((o,e)=>{console.error(L.red(o)),o.includes("Missing required argument")&&console.log(L.yellow(`Run 'pnpm dubhe ${process.argv[2]} --help' for a list of available and required arguments.`)),console.log(""),g(e),console.log(""),process.exit(1)}).alias({h:"help"}).argv; //# sourceMappingURL=dubhe.js.map