giga-code
Version:
A personal AI CLI assistant powered by Grok for local development.
137 lines • 5.29 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceFileQuery = exports.getFilteredItems = exports.extractFileQuery = exports.getAllFiles = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
/**
* Get all files in the current directory and subdirectories
*/
function getAllFiles(rootDir = process.cwd(), maxDepth = 3) {
const files = [];
function walkDirectory(dir, currentDepth = 0) {
if (currentDepth > maxDepth)
return;
try {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
// Skip hidden files and common ignore patterns
if (entry.name.startsWith('.') ||
entry.name === 'node_modules' ||
entry.name === 'dist' ||
entry.name === 'build' ||
entry.name === '.git') {
continue;
}
const fullPath = path.join(dir, entry.name);
const relativePath = path.relative(rootDir, fullPath);
if (entry.isDirectory()) {
files.push({
name: entry.name,
relativePath,
isDirectory: true
});
walkDirectory(fullPath, currentDepth + 1);
}
else {
files.push({
name: entry.name,
relativePath,
isDirectory: false
});
}
}
}
catch (error) {
// Ignore permission errors and continue
}
}
walkDirectory(rootDir);
return files;
}
exports.getAllFiles = getAllFiles;
/**
* Extract file search query from input text after @ symbol
* Only returns a result if @ is the last "word" (no spaces after @)
*/
function extractFileQuery(input) {
const atIndex = input.lastIndexOf('@');
if (atIndex === -1)
return null;
// Check if there are any spaces after the @ symbol
const afterAt = input.substring(atIndex + 1);
if (afterAt.includes(' ')) {
// There's a space after @, so this is not an active file query
return null;
}
const beforeAt = input.substring(0, atIndex);
const query = afterAt; // Everything after @ until end of string
const isDirectory = query.endsWith('/');
// Only show file finder if there's at least the @ symbol
// Allow empty query to show all files/directories
return { beforeAt, query, afterAt: '', isDirectory };
}
exports.extractFileQuery = extractFileQuery;
/**
* Filter files to get only files or only directories based on query
*/
function getFilteredItems(files, query, isDirectory) {
// Remove trailing slash for directory search
const searchQuery = isDirectory ? query.slice(0, -1) : query;
if (isDirectory) {
// Filter for directories only
let results = files
.filter(file => file.isDirectory)
.map(file => file.relativePath + '/');
// If there's a search query, filter by it
if (searchQuery) {
results = results.filter(path => path.toLowerCase().includes(searchQuery.toLowerCase()));
}
return results.sort().slice(0, 10); // Limit results
}
else {
// Filter for files only
let results = files
.filter(file => !file.isDirectory)
.map(file => file.relativePath);
// If there's a search query, filter by it
if (query) {
results = results.filter(path => path.toLowerCase().includes(query.toLowerCase()));
}
return results.sort().slice(0, 10); // Limit results
}
}
exports.getFilteredItems = getFilteredItems;
/**
* Replace the file query in input with the selected file path
*/
function replaceFileQuery(input, selectedFile) {
const queryInfo = extractFileQuery(input);
if (!queryInfo)
return input;
return queryInfo.beforeAt + selectedFile + queryInfo.afterAt;
}
exports.replaceFileQuery = replaceFileQuery;
//# sourceMappingURL=file-finder.js.map