smart-renamer
Version:
🚀 Intelligent file and code naming suggestions based on project-specific naming conventions. Interactive CLI tool with AST-based code analysis for variables, functions, components, and more.
139 lines • 4.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigManager = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
class ConfigManager {
configPath;
defaultConfig = {
convention: 'camelCase',
files: ['*.ts', '*.js'],
folders: 'kebab-case',
exceptions: ['index', 'main', 'app'],
code: {
variables: 'camelCase',
functions: 'camelCase',
components: 'PascalCase',
constants: 'UPPER_SNAKE_CASE',
classes: 'PascalCase',
interfaces: 'PascalCase',
types: 'PascalCase',
enums: 'PascalCase'
}
};
constructor(projectRoot = process.cwd()) {
this.configPath = (0, path_1.join)(projectRoot, 'naming.config');
}
loadConfig() {
if (!(0, fs_1.existsSync)(this.configPath)) {
return this.defaultConfig;
}
try {
const content = (0, fs_1.readFileSync)(this.configPath, 'utf-8');
return this.parseConfig(content);
}
catch (error) {
console.warn('Failed to load naming.config, using defaults:', error);
return this.defaultConfig;
}
}
saveConfig(config) {
try {
const content = this.stringifyConfig(config);
(0, fs_1.writeFileSync)(this.configPath, content, 'utf-8');
}
catch (error) {
throw new Error(`Failed to save naming.config: ${error}`);
}
}
parseConfig(content) {
const config = {};
const codeConfig = {};
const lines = content.split('\n');
let currentSection = '';
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.startsWith('[') && trimmed.endsWith(']')) {
currentSection = trimmed.slice(1, -1);
continue;
}
if (!trimmed.includes('='))
continue;
const [key, value] = trimmed.split('=').map(s => s.trim());
if (currentSection === 'naming') {
switch (key) {
case 'convention':
if (value && this.isValidConvention(value)) {
config.convention = value;
}
break;
case 'files':
if (value) {
config.files = value.split(',').map(s => s.trim());
}
break;
case 'folders':
if (value && this.isValidConvention(value)) {
config.folders = value;
}
break;
case 'exceptions':
if (value) {
config.exceptions = value.split(',').map(s => s.trim());
}
break;
}
}
else if (currentSection === 'code') {
const validCodeKeys = [
'variables', 'functions', 'components', 'constants',
'classes', 'interfaces', 'types', 'enums'
];
if (validCodeKeys.includes(key) &&
value && this.isValidConvention(value)) {
codeConfig[key] = value;
}
}
}
if (Object.keys(codeConfig).length > 0) {
config.code = { ...this.defaultConfig.code, ...codeConfig };
}
return { ...this.defaultConfig, ...config };
}
stringifyConfig(config) {
let content = `[naming]
convention=${config.convention}
files=${config.files.join(',')}
folders=${config.folders || 'kebab-case'}
exceptions=${config.exceptions?.join(',') || ''}
`;
if (config.code) {
content += `
[code]
variables=${config.code.variables}
functions=${config.code.functions}
components=${config.code.components}
constants=${config.code.constants}
classes=${config.code.classes}
interfaces=${config.code.interfaces}
types=${config.code.types}
enums=${config.code.enums}
`;
}
return content;
}
isValidConvention(value) {
const validConventions = [
'camelCase', 'snake_case', 'kebab-case', 'PascalCase', 'UPPER_SNAKE_CASE'
];
return validConventions.includes(value);
}
configExists() {
return (0, fs_1.existsSync)(this.configPath);
}
createDefaultConfig() {
this.saveConfig(this.defaultConfig);
}
}
exports.ConfigManager = ConfigManager;
//# sourceMappingURL=config-manager.js.map