@paulohenriquevn/m2js
Version:
Transform TypeScript/JavaScript code into LLM-friendly Markdown summaries + Smart Dead Code Detection + Graph-Deep Diff Analysis. Extract exported functions, classes, and JSDoc comments for better AI context with 60%+ token reduction. Intelligent dead cod
244 lines • 8.07 kB
JavaScript
;
/**
* Configuration Loader for M2JS
* Loads settings from .m2jsrc files and environment variables
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigLoader = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
/**
* Default configuration
*/
const DEFAULT_CONFIG = {
deadCode: {
enableCache: true,
maxCacheSize: 1000,
chunkSize: 50,
showProgress: true,
format: 'table',
includeMetrics: true,
includeSuggestions: true,
},
duplicateCode: {
minLines: 5,
minTokens: 50,
ignorePatterns: [
'**/*.test.*',
'**/*.spec.*',
'**/node_modules/**',
'**/dist/**',
'**/build/**',
],
includeContext: true,
includeSuggestions: true,
format: 'table',
},
extraction: {
includeComments: true,
includeUsageExamples: false,
includeBusinessContext: false,
includeArchitectureInsights: false,
includeSemanticAnalysis: false,
},
files: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
ignorePatterns: [
'**/node_modules/**',
'**/dist/**',
'**/build/**',
'**/.next/**',
'**/coverage/**',
'**/*.test.*',
'**/*.spec.*',
],
maxFileSize: 10, // 10MB
},
output: {
format: 'markdown',
preserveStructure: true,
},
};
/**
* Configuration loader class
*/
class ConfigLoader {
/**
* Load configuration from multiple sources with precedence:
* 1. Environment variables (highest priority)
* 2. .m2jsrc in current directory
* 3. .m2jsrc in home directory
* 4. Default configuration (lowest priority)
*/
static loadConfig(projectPath) {
// Return cached config if available
if (this.configCache) {
return this.configCache;
}
let config = { ...DEFAULT_CONFIG };
// Try to load from project directory
if (projectPath) {
const projectConfigPath = path_1.default.join(projectPath, '.m2jsrc');
config = this.mergeConfigs(config, this.loadConfigFile(projectConfigPath));
}
// Try to load from current directory
const localConfigPath = path_1.default.join(process.cwd(), '.m2jsrc');
config = this.mergeConfigs(config, this.loadConfigFile(localConfigPath));
// Try to load from home directory
const homeConfigPath = path_1.default.join(os_1.default.homedir(), '.m2jsrc');
config = this.mergeConfigs(config, this.loadConfigFile(homeConfigPath));
// Apply environment variable overrides
config = this.applyEnvironmentOverrides(config);
// Cache the final configuration
this.configCache = config;
return config;
}
/**
* Load configuration from a specific file
*/
static loadConfigFile(filePath) {
if (!(0, fs_1.existsSync)(filePath)) {
return null;
}
try {
const content = (0, fs_1.readFileSync)(filePath, 'utf-8');
// Support both JSON and JavaScript module formats
if (filePath.endsWith('.json') || content.trim().startsWith('{')) {
return JSON.parse(content);
}
else {
// For .m2jsrc files, expect JSON format
return JSON.parse(content);
}
}
catch (error) {
console.warn(`Warning: Failed to load config from ${filePath}: ${error.message}`);
return null;
}
}
/**
* Deep merge two configuration objects
*/
static mergeConfigs(base, override) {
if (!override)
return base;
const merged = { ...base };
// Deep merge each section
if (override.deadCode) {
merged.deadCode = { ...merged.deadCode, ...override.deadCode };
}
if (override.extraction) {
merged.extraction = { ...merged.extraction, ...override.extraction };
}
if (override.files) {
merged.files = { ...merged.files, ...override.files };
}
if (override.output) {
merged.output = { ...merged.output, ...override.output };
}
return merged;
}
/**
* Apply environment variable overrides
*/
static applyEnvironmentOverrides(config) {
const env = process.env;
const result = { ...config };
// Dead code analysis settings
if (env.M2JS_CACHE_ENABLED !== undefined) {
result.deadCode.enableCache = env.M2JS_CACHE_ENABLED === 'true';
}
if (env.M2JS_CACHE_SIZE !== undefined) {
result.deadCode.maxCacheSize =
parseInt(env.M2JS_CACHE_SIZE, 10) || result.deadCode.maxCacheSize;
}
if (env.M2JS_CHUNK_SIZE !== undefined) {
result.deadCode.chunkSize =
parseInt(env.M2JS_CHUNK_SIZE, 10) || result.deadCode.chunkSize;
}
if (env.M2JS_SHOW_PROGRESS !== undefined) {
result.deadCode.showProgress = env.M2JS_SHOW_PROGRESS === 'true';
}
if (env.M2JS_FORMAT !== undefined) {
result.deadCode.format =
env.M2JS_FORMAT || result.deadCode.format;
}
// File processing settings
if (env.M2JS_MAX_FILE_SIZE !== undefined) {
result.files.maxFileSize =
parseInt(env.M2JS_MAX_FILE_SIZE, 10) || result.files.maxFileSize;
}
if (env.M2JS_IGNORE_PATTERNS !== undefined) {
result.files.ignorePatterns = env.M2JS_IGNORE_PATTERNS.split(',').map(p => p.trim());
}
return result;
}
/**
* Convert config to performance options
*/
static toPerformanceOptions(config) {
return {
enableCache: config.deadCode.enableCache,
maxCacheSize: config.deadCode.maxCacheSize,
chunkSize: config.deadCode.chunkSize,
showProgress: config.deadCode.showProgress,
};
}
/**
* Clear cached configuration (useful for testing)
*/
static clearCache() {
this.configCache = null;
}
/**
* Generate example configuration file content
*/
static generateExampleConfig() {
return JSON.stringify({
// Dead code analysis settings
deadCode: {
enableCache: true,
maxCacheSize: 1000,
chunkSize: 50,
showProgress: true,
format: 'table',
includeMetrics: true,
includeSuggestions: true,
},
// Code extraction settings
extraction: {
includeComments: true,
includeUsageExamples: false,
includeBusinessContext: false,
includeArchitectureInsights: false,
includeSemanticAnalysis: false,
},
// File processing settings
files: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
ignorePatterns: [
'**/node_modules/**',
'**/dist/**',
'**/build/**',
'**/.next/**',
'**/coverage/**',
'**/*.test.*',
'**/*.spec.*',
],
maxFileSize: 10,
},
// Output settings
output: {
format: 'markdown',
preserveStructure: true,
},
}, null, 2);
}
}
exports.ConfigLoader = ConfigLoader;
ConfigLoader.configCache = null;
//# sourceMappingURL=config-loader.js.map