@onurege3467/zerohelper
Version:
ZeroHelper is a versatile high-performance utility library and database framework for Node.js, fully written in TypeScript.
97 lines (96 loc) ⢠4.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.replCommand = void 0;
const commander_1 = require("commander");
const chalk_1 = __importDefault(require("chalk"));
const config_1 = require("../utils/config");
exports.replCommand = new commander_1.Command().name('repl');
exports.replCommand
.description('Interactive ZeroHelper REPL mode')
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
.action(async (options) => {
console.log(chalk_1.default.bold.cyan('\nš§ ZeroHelper REPL Mode\n'));
console.log(chalk_1.default.gray('Type .exit to quit, .help for commands\n'));
try {
const db = await (0, config_1.getDatabase)(options.config);
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: chalk_1.default.blue('zero> ')
});
rl.prompt();
rl.on('line', async (line) => {
const cmd = line.trim();
if (cmd === '.exit') {
await db.close();
rl.close();
console.log(chalk_1.default.green('\nš Goodbye!\n'));
process.exit(0);
}
else if (cmd === '.help') {
console.log(chalk_1.default.bold('\nAvailable REPL Commands:'));
console.log(' .exit Exit REPL');
console.log(' .help Show this help');
console.log(' .stats Show database stats');
console.log(' .metrics Show performance metrics');
console.log(' select <table> Select all from table');
console.log(' count <table> Count records in table');
console.log(' clear Clear screen\n');
}
else if (cmd === '.stats') {
const metrics = db.getMetrics();
console.log(chalk_1.default.bold('\nš Database Stats:'));
console.log(` Operations: ${metrics.database.count || 0}`);
console.log(` Avg Latency: ${(metrics.database.averageDuration || 0).toFixed(2)}ms\n`);
}
else if (cmd === '.metrics') {
const metrics = db.getMetrics();
console.log(chalk_1.default.bold('\nš Full Metrics:'));
console.log(JSON.stringify(metrics, null, 2));
console.log('');
}
else if (cmd === '.clear') {
console.clear();
}
else if (cmd.startsWith('select ')) {
const table = cmd.replace('select ', '').trim();
try {
const records = await db.select(table);
console.log(chalk_1.default.bold(`\nFound ${records.length} records in ${table}:`));
console.log(JSON.stringify(records, null, 2));
console.log('');
}
catch (err) {
console.error(chalk_1.default.red(`Error: ${err.message}\n`));
}
}
else if (cmd.startsWith('count ')) {
const table = cmd.replace('count ', '').trim();
try {
const records = await db.select(table);
console.log(chalk_1.default.bold(`\n${table}: ${records.length} records\n`));
}
catch (err) {
console.error(chalk_1.default.red(`Error: ${err.message}\n`));
}
}
else if (cmd.length > 0) {
console.log(chalk_1.default.yellow('Unknown command. Type .help for available commands\n'));
}
rl.prompt();
});
rl.on('close', () => {
db.close();
console.log(chalk_1.default.green('\nš Goodbye!\n'));
process.exit(0);
});
}
catch (error) {
console.error(chalk_1.default.red(`Error: ${error.message}`));
process.exit(1);
}
});