scai
Version:
> AI-powered CLI tool for commit messages **and** pull request reviews — using local models.
66 lines (65 loc) • 2.21 kB
JavaScript
// indexCmd.ts
import fg from 'fast-glob';
import path from 'path';
import { initSchema } from '../db/schema.js';
import { indexFile } from '../db/fileIndex.js';
import { detectFileType } from '../fileRules/detectFileType.js';
import { startDaemon } from './DaemonCmd.js';
import { IGNORED_FOLDER_GLOBS } from '../fileRules/ignoredPaths.js';
import { Config } from '../config.js';
import { log } from '../utils/log.js';
import lockfile from 'proper-lockfile';
import { classifyFile } from '../fileRules/classifyFile.js';
import { getDbPathForRepo } from '../db/client.js';
async function lockDb() {
try {
const lock = await lockfile.lock(getDbPathForRepo());
return lock;
}
catch (err) {
log('❌ Failed to acquire DB lock: ' + err);
throw err;
}
}
export async function runIndexCommand() {
try {
initSchema();
}
catch (err) {
console.error('❌ Failed to initialize schema:', err);
process.exit(1);
}
const indexDir = Config.getIndexDir() || process.cwd();
Config.setIndexDir(indexDir); // persist if not already saved
log(`📂 Indexing files in: ${indexDir}`);
const files = await fg('**/*.*', {
cwd: indexDir,
ignore: IGNORED_FOLDER_GLOBS,
absolute: true,
});
const countByExt = {};
let count = 0;
const release = await lockDb();
for (const file of files) {
const classification = classifyFile(file);
if (classification !== 'valid') {
log(`⏭️ Skipping (${classification}): ${file}`);
continue;
}
try {
const type = detectFileType(file);
indexFile(file, null, type);
const ext = path.extname(file);
countByExt[ext] = (countByExt[ext] || 0) + 1;
log(`📄 Indexed: ${path.relative(indexDir, file)}`);
count++;
}
catch (err) {
log(`⚠️ Skipped in indexCmd ${file}: ${err instanceof Error ? err.message : err}`);
}
}
log('📊 Indexed files by extension:', JSON.stringify(countByExt, null, 2));
log(`✅ Done. Indexed ${count} files.`);
await release();
startDaemon();
}