sicua
Version:
A tool for analyzing project structure and dependencies
184 lines (183 loc) • 5.78 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.PathUtils = void 0;
exports.calculateFileHash = calculateFileHash;
exports.ensureDirectoryExists = ensureDirectoryExists;
exports.readJsonFile = readJsonFile;
exports.writeJsonFile = writeJsonFile;
exports.extractPackageName = extractPackageName;
exports.isPathAlias = isPathAlias;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const crypto = __importStar(require("crypto"));
class PathUtils {
/**
* Normalizes a file path to a consistent format
*/
static normalizePath(filePath) {
return path.normalize(filePath).replace(/\\/g, "/");
}
/**
* Gets relative path between two absolute paths
*/
static getRelativePath(from, to) {
const relativePath = path.relative(path.dirname(from), to);
return this.normalizePath(relativePath);
}
/**
* Checks if a path is absolute
*/
static isAbsolutePath(filePath) {
return path.isAbsolute(filePath);
}
/**
* Gets directory name from path
*/
static getDirectory(filePath) {
return this.normalizePath(path.dirname(filePath));
}
/**
* Gets file name from path
*/
static getFileName(filePath) {
return path.basename(filePath, path.extname(filePath));
}
/**
* Gets file extension from path
*/
static getFileExtension(filePath) {
return path.extname(filePath);
}
/**
* Joins path segments
*/
static joinPaths(...paths) {
return this.normalizePath(path.join(...paths));
}
/**
* Resolves a path to its absolute form
*/
static resolvePath(...paths) {
return this.normalizePath(path.resolve(...paths));
}
/**
* Generate SHA-256 hash of a string
*/
static generateHash(input) {
return crypto.createHash("sha256").update(input).digest("hex");
}
/**
* Checks if a path is within another path
*/
static isWithinDirectory(directory, filePath) {
const normalizedDir = this.normalizePath(directory);
const normalizedPath = this.normalizePath(filePath);
return normalizedPath.startsWith(normalizedDir);
}
/**
* Checks if path is a TypeScript/JavaScript file
*/
static isSourceFile(filePath) {
const ext = this.getFileExtension(filePath).toLowerCase();
return [".ts", ".tsx", ".js", ".jsx"].includes(ext);
}
}
exports.PathUtils = PathUtils;
// File system operations
async function calculateFileHash(filePath) {
try {
const fileBuffer = await fs.readFile(filePath);
const hashSum = crypto.createHash("sha256");
hashSum.update(fileBuffer);
return hashSum.digest("hex");
}
catch (error) {
console.error(`Error calculating file hash for ${filePath}:`, error);
throw error;
}
}
async function ensureDirectoryExists(dirPath) {
try {
await fs.mkdir(dirPath, { recursive: true });
}
catch (error) {
console.error(`Error creating directory ${dirPath}:`, error);
throw error;
}
}
// JSON file operations
async function readJsonFile(filePath) {
try {
const content = await fs.readFile(filePath, "utf-8");
return JSON.parse(content);
}
catch (error) {
console.error(`Error reading JSON file ${filePath}:`, error);
throw error;
}
}
async function writeJsonFile(filePath, data) {
try {
await fs.writeFile(filePath, JSON.stringify(data, null, 2));
}
catch (error) {
console.error(`Error writing JSON file ${filePath}:`, error);
throw error;
}
}
/**
* Extracts the package name from an import path
* @example
* extractPackageName("@angular/core") // returns "@angular/core"
* extractPackageName("lodash") // returns "lodash"
* extractPackageName("./local/file") // returns null
*/
function extractPackageName(importPath) {
if (importPath.startsWith(".") || importPath.startsWith("/")) {
return null;
}
const parts = importPath.split("/");
return parts[0].startsWith("@") && parts.length > 1
? `${parts[0]}/${parts[1]}`
: parts[0];
}
/**
* Checks if a path is using an alias pattern
* @example
* isPathAlias("@/components/Button") // returns true
* isPathAlias("lodash") // returns false
*/
function isPathAlias(path) {
const aliasPatterns = [
/^@\//, // @/something
/^~\//, // ~/something
/^#\//, // #/something
/^src\//, // src/something
/^\.\//, // ./something
/^\.\.\//, // ../something
];
return aliasPatterns.some((pattern) => pattern.test(path));
}