create-ai-chat-context-experimental
Version:
Phase 2: TypeScript rewrite - AI Chat Context & Memory System with conversation extraction and AICF format support (powered by aicf-core v2.1.0).
117 lines • 3.37 kB
JavaScript
/**
* This file is part of create-ai-chat-context-experimental.
* Licensed under the GNU Affero General Public License v3.0 or later (AGPL-3.0-or-later).
* See LICENSE file for details.
*/
/**
* File System Utilities
* Common file operations for watchers
* October 2025
*/
import { existsSync, readdirSync, readFileSync, statSync } from 'fs';
import { join } from 'path';
import { Ok, Err } from '../types/index.js';
/**
* Get project path
*/
export function getProjectPath(basePath, projectName) {
return join(basePath, projectName);
}
/**
* List files in directory
*/
export function listFiles(dirPath) {
try {
if (!existsSync(dirPath)) {
return Ok([]);
}
const files = readdirSync(dirPath);
return Ok(files);
}
catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error';
return Err(new Error(`Failed to list files: ${message}`));
}
}
/**
* List files with specific extension
*/
export function listFilesByExtension(dirPath, extension) {
try {
const result = listFiles(dirPath);
if (!result.ok) {
return result;
}
const filtered = result.value.filter((f) => f.endsWith(extension));
return Ok(filtered);
}
catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error';
return Err(new Error(`Failed to list files by extension: ${message}`));
}
}
/**
* Read file content
*/
export function readFile(filePath) {
try {
if (!existsSync(filePath)) {
return Err(new Error(`File not found: ${filePath}`));
}
const content = readFileSync(filePath, 'utf-8');
return Ok(content);
}
catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error';
return Err(new Error(`Failed to read file: ${message}`));
}
}
/**
* Get latest file (by modification time)
*/
export function getLatestFile(dirPath, extension) {
try {
const filesResult = extension ? listFilesByExtension(dirPath, extension) : listFiles(dirPath);
if (!filesResult.ok) {
return filesResult;
}
const files = filesResult.value;
if (files.length === 0) {
return Ok(null);
}
let latestFile = files[0] ?? null;
let latestTime = 0;
for (const file of files) {
const filePath = join(dirPath, file);
try {
const stat = statSync(filePath);
if (stat.mtimeMs > latestTime) {
latestTime = stat.mtimeMs;
latestFile = file;
}
}
catch {
// Skip files that can't be stat'd
continue;
}
}
return Ok(latestFile ?? null);
}
catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error';
return Err(new Error(`Failed to get latest file: ${message}`));
}
}
/**
* Filter files by extension
*/
export function filterByExtension(files, extension) {
return files.filter((f) => f.endsWith(extension));
}
/**
* Check if path exists
*/
export function pathExists(path) {
return existsSync(path);
}
//# sourceMappingURL=FileSystemUtils.js.map