rosie-cli
Version:
AI-powered command-line interface tool that uses OpenAI's API to help you interact with your computer through natural language
112 lines (111 loc) • 4.88 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findEverythingCli = findEverythingCli;
exports.searchEverything = searchEverything;
const child_process_1 = require("child_process");
const util_1 = require("util");
const fs_1 = require("fs");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
/**
* Finds the Everything CLI executable
* @returns Path to Everything CLI or null if not found
*/
function findEverythingCli() {
return __awaiter(this, void 0, void 0, function* () {
// Default paths to check for the Everything CLI
const possiblePaths = [
'C:\\Program Files\\Everything\\es.exe',
'C:\\Program Files (x86)\\Everything\\es.exe',
];
// Check if any of the default paths exist
for (const path of possiblePaths) {
if ((0, fs_1.existsSync)(path)) {
return path;
}
}
// Try to find it in PATH
try {
const { stdout } = yield execAsync('where es.exe');
const paths = stdout.trim().split('\n');
if (paths.length > 0 && paths[0]) {
return paths[0];
}
}
catch (error) {
// Not found in PATH, continue
}
return null;
});
}
/**
* Queries VoidTools' Everything search engine via CLI
*
* @param query - The search query string
* @param options - Search options
* @returns Promise resolving to an array of search results
* @throws Error if Everything CLI is not found or search fails
*/
function searchEverything(query_1) {
return __awaiter(this, arguments, void 0, function* (query, options = {}) {
var _a;
try {
// Find the Everything CLI executable
const everythingPath = options.everythingPath || (yield findEverythingCli());
if (!everythingPath) {
throw new Error('Everything CLI (es.exe) not found. Please install Everything search tool or provide a custom path.');
}
// Build command-line arguments
const args = ['-csv']; // CSV output format
// Add search options
if (options.regex)
args.push('-regex');
if (options.matchCase)
args.push('-case');
if (options.matchPath)
args.push('-path-match');
if (options.matchWholeWord)
args.push('-whole-word');
args.push(`-max-results ${(_a = options.maxResults) !== null && _a !== void 0 ? _a : 100}`);
// Add the query (wrap in quotes to handle spaces)
args.push(`"${query.replace(/"/g, '\\"')}"`);
// Execute the command with increased maxBuffer size (50MB)
const { stdout, stderr } = yield execAsync(`"${everythingPath}" ${args.join(' ')}`, { maxBuffer: 50 * 1024 * 1024 });
if (stderr) {
console.error('Everything CLI error:', stderr);
}
// Parse CSV output
const results = [];
if (stdout) {
const lines = stdout.trim().split('\n');
// Check if the first line is the header
const hasHeader = lines[0] === 'Filename';
const startLine = hasHeader ? 1 : 0;
for (let i = startLine; i < lines.length; i++) {
// The output format appears to be just the full path without CSV columns
const path = lines[i].replace(/^"(.*)"$/, '$1'); // Remove surrounding quotes if present
// Extract the filename from the path
const name = path.split('\\').pop() || '';
results.push({
name: name,
path: path,
isFolder: name.endsWith('\\') || !name.includes('.')
});
}
}
return results;
}
catch (error) {
console.error('Error executing Everything search:', error);
throw new Error(`Everything search failed: ${error.message}`);
}
});
}