@0xobelisk/rooch-cli
Version:
Tookit for interacting with rooch framework
99 lines (85 loc) ⢠3.02 kB
text/typescript
import { execSync, spawn } from 'child_process';
import chalk from 'chalk';
function isRoochStartRunning(): boolean {
try {
const cmd =
process.platform === 'win32'
? `tasklist /FI "IMAGENAME eq rooch.exe" /FO CSV /NH`
: 'pgrep -f "rooch server start"';
const result = execSync(cmd).toString().trim();
return process.platform === 'win32'
? result.toLowerCase().includes('rooch.exe')
: result.length > 0;
} catch (error) {
return false;
}
}
export async function startLocalnode(background: boolean = false) {
let roochCliPassword = process.env.ROOCH_CLI_PASSWORD;
if (roochCliPassword === undefined) {
roochCliPassword = '';
}
console.log('\nš Checking Local Node Status...');
console.log(' āā Scanning running processes');
if (isRoochStartRunning()) {
console.log(chalk.yellow('\nā ļø Warning: Local Node Already Running'));
console.log(chalk.yellow(' āā Cannot start a new instance'));
console.log(
chalk.yellow(' āā Please stop the existing process first')
);
return;
}
console.log('\nš Starting Local Node...');
console.log(' āā Mode: ' + (background ? 'Background' : 'Foreground'));
console.log(' āā Faucet: Enabled');
console.log(' āā Force Regenesis: Yes');
try {
let runCommand = ['server', 'start', '-n', 'local'];
if (roochCliPassword !== '') {
runCommand.push('--password', roochCliPassword);
}
const roochProcess = spawn(
'rooch',
runCommand,
{
env: { ...process.env },
// env: { ...process.env, RUST_LOG: 'off,rooch_node=info' },
stdio: background ? 'ignore' : 'inherit',
detached: background,
}
);
roochProcess.on('error', error => {
console.error(chalk.red('\nā Failed to Start Local Node'));
console.error(chalk.red(` āā Error: ${error.message}`));
});
if (!background) {
roochProcess.on('exit', code => {
if (code === 0) {
console.log(chalk.green('\nā
Local Node Stopped'));
console.log(chalk.green(' āā Exit Status: Normal'));
} else {
console.error(chalk.red('\nā Local Node Crashed'));
console.error(chalk.red(` āā Exit Code: ${code}`));
}
});
console.log(chalk.cyan('\nš” Local Node Running'));
console.log(chalk.cyan(' āā Press Ctrl+C to stop'));
await new Promise(() => {});
} else {
roochProcess.unref();
console.log(chalk.green('\nā
Local Node Started in Background'));
if (process.platform === 'win32') {
console.log('\nš” Helpful Commands:');
console.log(' āā Check Process: tasklist | findstr rooch.exe');
console.log(' āā Stop Node: taskkill /PID <process_id> /F');
} else {
console.log('\nš” Helpful Commands:');
console.log(" āā Check Process: pgrep -f 'rooch server start'");
console.log(' āā Stop Node: kill <process_id>');
}
}
} catch (error: any) {
console.error(chalk.red('\nā Failed to Start Local Node'));
console.error(chalk.red(` āā Error: ${error.message}`));
}
}