@gati-framework/cli
Version:
CLI tool for Gati framework - create, develop, build and deploy cloud-native applications
93 lines • 3.25 kB
JavaScript
/**
* @module cli/analyzer/simple-analyzer
* @description Simple file-based analyzer without ts-morph
*/
import { readFileSync } from 'fs';
import { relative } from 'path';
export function analyzeFile(filePath, srcRoot) {
try {
const content = readFileSync(filePath, 'utf-8');
const relativePath = relative(srcRoot, filePath);
if (filePath.includes('/handlers/') || filePath.includes('\\handlers\\')) {
return analyzeHandler(content, filePath, relativePath);
}
else if (filePath.includes('/modules/') || filePath.includes('\\modules\\')) {
return analyzeModule(content, filePath);
}
return null;
}
catch (error) {
console.warn(`Failed to analyze ${filePath}:`, error);
return null;
}
}
function analyzeHandler(content, filePath, relativePath) {
// Extract METHOD export
const methodMatch = content.match(/export\s+const\s+METHOD\s*=\s*['"`]([^'"`]+)['"`]/);
const method = methodMatch?.[1] || 'GET';
// Extract ROUTE export
const routeMatch = content.match(/export\s+const\s+ROUTE\s*=\s*['"`]([^'"`]+)['"`]/);
const customRoute = routeMatch?.[1] || null;
// Extract handler export
const handlerMatch = content.match(/export\s+const\s+(\w*[Hh]andler\w*)/);
if (!handlerMatch?.[1])
return null;
const exportName = handlerMatch[1];
const route = customRoute ? buildFullRoute(relativePath, customRoute) : pathToRoute(relativePath);
return {
filePath,
relativePath,
route,
method,
exportName,
exportType: 'named'
};
}
function analyzeModule(content, filePath) {
// Extract module export
const moduleMatch = content.match(/export\s+const\s+(\w+)/);
if (!moduleMatch?.[1])
return null;
const exportName = moduleMatch[1];
// Extract method names (simple regex)
const methods = [...content.matchAll(/(\w+):\s*(?:async\s+)?\(/g)]
.map(match => match[1])
.filter((name) => name !== undefined && !['export', 'const', 'function'].includes(name));
return {
filePath,
exportName,
exportType: 'named',
methods
};
}
function pathToRoute(relativePath) {
let route = relativePath
.replace(/\\/g, '/') // Normalize to forward slashes
.replace(/^handlers\//, '')
.replace(/\.ts$/, '')
.replace(/\.js$/, '')
.replace(/\/index$/, '');
route = route.replace(/\[([^\]]+)\]/g, ':$1');
if (!route.startsWith('/')) {
route = '/' + route;
}
return route === '/' ? '/' : route;
}
function buildFullRoute(relativePath, customRoute) {
let parentPath = relativePath
.replace(/\\/g, '/') // Normalize to forward slashes
.replace(/^handlers\//, '');
// Remove filename (everything after last slash, or entire string if no slash)
const lastSlash = parentPath.lastIndexOf('/');
if (lastSlash >= 0) {
parentPath = parentPath.substring(0, lastSlash);
}
else {
parentPath = ''; // No parent directory
}
if (!parentPath) {
return customRoute;
}
return `/${parentPath}${customRoute}`;
}
//# sourceMappingURL=simple-analyzer.js.map