@puberty-labs/clits
Version:
CLiTS (Chrome Logging and Inspection Tool Suite) is a powerful Node.js library for AI-controlled Chrome browser automation, testing, and inspection. Features enhanced CSS selector support (:contains(), XPath), dry-run mode, element discovery tools, and co
68 lines (67 loc) • 2.11 kB
JavaScript
// BSD: Path resolution utilities for CLiTS. Handles consistent path resolution, module loading, and error reporting.
import { fileURLToPath } from 'url';
import { dirname, resolve, isAbsolute } from 'path';
import { existsSync } from 'fs';
export class PathResolver {
constructor() {
const __filename = fileURLToPath(import.meta.url);
this.baseDir = dirname(dirname(__filename)); // Go up one level from utils to src
this.projectRoot = dirname(this.baseDir); // Go up one level from src to project root
}
static getInstance() {
if (!PathResolver.instance) {
PathResolver.instance = new PathResolver();
}
return PathResolver.instance;
}
/**
* Resolves a path relative to the project root
*/
resolvePath(path) {
if (isAbsolute(path)) {
return path;
}
return resolve(this.projectRoot, path);
}
/**
* Resolves a module path and verifies it exists
*/
resolveModule(modulePath) {
const resolvedPath = this.resolvePath(modulePath);
if (!existsSync(resolvedPath)) {
throw new Error(`Module not found: ${modulePath}\nResolved to: ${resolvedPath}\nBase directory: ${this.projectRoot}`);
}
return resolvedPath;
}
/**
* Gets the project root directory
*/
getProjectRoot() {
return this.projectRoot;
}
/**
* Gets the source directory
*/
getSourceDir() {
return this.baseDir;
}
/**
* Validates if a path exists and is accessible
*/
validatePath(path) {
try {
const resolvedPath = this.resolvePath(path);
const exists = existsSync(resolvedPath);
return {
exists,
error: exists ? undefined : `Path does not exist: ${resolvedPath}`
};
}
catch (error) {
return {
exists: false,
error: `Error validating path: ${error instanceof Error ? error.message : String(error)}`
};
}
}
}