@lenne.tech/cli
Version:
lenne.Tech CLI: lt
118 lines (117 loc) • 3.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.History = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const MAX_HISTORY_ENTRIES = 100;
/**
* Command history management
*/
class History {
constructor(toolbox) {
this.toolbox = toolbox;
const homeDir = toolbox.filesystem.homedir();
const ltDir = (0, path_1.join)(homeDir, `.${toolbox.runtime.brand}`);
// Ensure ~/.lt directory exists
if (!(0, fs_1.existsSync)(ltDir)) {
(0, fs_1.mkdirSync)(ltDir, { recursive: true });
}
this.historyFile = (0, path_1.join)(ltDir, 'history.json');
}
/**
* Get all history entries
*/
getHistory() {
try {
if ((0, fs_1.existsSync)(this.historyFile)) {
const content = (0, fs_1.readFileSync)(this.historyFile, 'utf-8');
return JSON.parse(content);
}
}
catch (_a) {
// Return empty if file doesn't exist or is corrupted
}
return [];
}
/**
* Add a command to history
*/
addEntry(command, args) {
// Skip history-related commands
if (command === 'history' || args.includes('--no-history')) {
return;
}
const history = this.getHistory();
const entry = {
args,
command,
cwd: this.toolbox.filesystem.cwd(),
timestamp: new Date().toISOString(),
};
history.push(entry);
// Limit history size
const trimmed = history.slice(-MAX_HISTORY_ENTRIES);
try {
(0, fs_1.writeFileSync)(this.historyFile, JSON.stringify(trimmed, null, 2));
}
catch (_a) {
// Ignore write errors
}
}
/**
* Clear history
*/
clear() {
try {
(0, fs_1.writeFileSync)(this.historyFile, '[]');
}
catch (_a) {
// Ignore errors
}
}
/**
* Get last N entries
*/
getLast(count) {
const history = this.getHistory();
return history.slice(-count);
}
/**
* Get entry by index (1-based, negative for reverse)
*/
getEntry(index) {
const history = this.getHistory();
if (index < 0) {
// Negative index: -1 = last, -2 = second last, etc.
const actualIndex = history.length + index;
return history[actualIndex] || null;
}
// Positive index: 1-based
return history[index - 1] || null;
}
/**
* Search history by command pattern
*/
search(pattern) {
const history = this.getHistory();
const regex = new RegExp(pattern, 'i');
return history.filter((entry) => regex.test(entry.command) || entry.args.some((arg) => regex.test(arg)));
}
/**
* Format entry for display
*/
formatEntry(entry, index) {
const date = new Date(entry.timestamp);
const dateStr = date.toLocaleDateString();
const timeStr = date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const fullCommand = [entry.command, ...entry.args].join(' ');
return `${index.toString().padStart(4)} ${dateStr} ${timeStr} lt ${fullCommand}`;
}
}
exports.History = History;
/**
* Extend toolbox
*/
exports.default = (toolbox) => {
toolbox.history = new History(toolbox);
};