refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
198 lines • 7.36 kB
JavaScript
;
/* eslint-disable no-console */
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
const util_1 = require("util");
const plugin_loader_1 = require("./quality-tools/plugin-loader");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
const README_PATH = path.join(__dirname, '..', 'README.md');
async function getHelpOutput() {
try {
const { stdout } = await execAsync('ts-node src/cli.ts --help', {
cwd: path.join(__dirname, '..')
});
return extractCommands(stdout);
}
catch (_error) {
return 'Error: Could not generate help output';
}
}
function getQualityChecksContent() {
const qualityChecks = (0, plugin_loader_1.loadQualityChecks)();
const descriptions = new Set();
for (const check of qualityChecks) {
addCheckDescriptions(check, descriptions);
}
return formatDescriptions(descriptions);
}
function addCheckDescriptions(check, descriptions) {
const groupKeys = getValidGroupKeys(check);
for (const groupKey of groupKeys) {
const groupDef = check.getGroupDefinition?.(groupKey);
if (groupDef) {
descriptions.add(`- **${groupDef.title}** (${groupDef.description})`);
}
}
}
function formatDescriptions(descriptions) {
return descriptions.size > 0
? Array.from(descriptions).join('\n')
: '- No quality checks configured';
}
function getValidGroupKeys(check) {
const possibleKeys = buildPossibleKeys(check);
return possibleKeys.filter(key => isValidKey(check, key));
}
function buildPossibleKeys(check) {
return [
check.name,
`${check.name}Functions`,
`critical${check.name.charAt(0).toUpperCase() + check.name.slice(1)}`,
`large${check.name.charAt(0).toUpperCase() + check.name.slice(1)}`,
'changeFrequency',
'cohesiveChange'
];
}
function isValidKey(check, key) {
try {
return Boolean(check.getGroupDefinition?.(key));
}
catch {
return false;
}
}
async function updateReadme() {
validateReadmeExists();
let content = fs.readFileSync(README_PATH, 'utf8');
content = await updateBothSections(content);
fs.writeFileSync(README_PATH, content);
console.log('✅ README.md updated with current help and quality checks');
}
function validateReadmeExists() {
if (!fs.existsSync(README_PATH)) {
console.error('README.md not found');
process.exit(1);
}
}
async function updateBothSections(content) {
const helpCommands = await getHelpOutput();
const qualityChecks = getQualityChecksContent();
content = updateHelpSection(content, helpCommands);
content = updateQualitySection(content, qualityChecks);
return content;
}
function updateHelpSection(content, helpCommands) {
return replaceSection(content, '<!-- AUTO-GENERATED HELP START -->', '<!-- AUTO-GENERATED HELP END -->', `## Available Commands\n\n\`\`\`\n${helpCommands}\n\`\`\`\n`);
}
function updateQualitySection(content, qualityChecks) {
return replaceSection(content, '<!-- AUTO-GENERATED QUALITY-CHECKS START -->', '<!-- AUTO-GENERATED QUALITY-CHECKS END -->', `**Quality Checks Include:**\n\`\`\`\`\n${qualityChecks}\n\`\`\`\``);
}
function replaceSection(content, startMarker, endMarker, newContent) {
const markerPositions = findMarkerPositions(content, startMarker, endMarker);
if (markerPositions.startIndex === -1 || markerPositions.endIndex === -1) {
console.warn(`Markers ${startMarker}/${endMarker} not found in README.md`);
return content;
}
return buildReplacementContent(content, markerPositions, startMarker, newContent);
}
function findMarkerPositions(content, startMarker, endMarker) {
return {
startIndex: content.indexOf(startMarker),
endIndex: content.indexOf(endMarker)
};
}
function buildReplacementContent(content, positions, startMarker, newContent) {
return content.substring(0, positions.startIndex) +
startMarker + '\n' + newContent + '\n' +
content.substring(positions.endIndex);
}
function extractCommands(helpOutput) {
const lines = helpOutput.split('\n');
const commandsStartIndex = findCommandsStart(lines);
if (commandsStartIndex === -1)
return 'No refactoring commands available';
const commands = parseCommandLines(lines, commandsStartIndex);
return formatCommands(commands);
}
function findCommandsStart(lines) {
return lines.findIndex(line => line.trim() === 'Commands:');
}
function parseCommandLines(lines, startIndex) {
const commands = [];
const currentCommand = processLines(lines, startIndex, commands);
if (currentCommand)
commands.push(currentCommand);
return commands;
}
function processLines(lines, startIndex, commands) {
let currentCommand = '';
for (let i = startIndex + 1; i < lines.length; i++) {
const line = lines[i].trim();
if (shouldStop(line))
break;
currentCommand = processLine(line, currentCommand, commands);
}
return currentCommand;
}
function shouldStop(line) {
return !line || line.includes('help [command]');
}
function processLine(line, currentCommand, commands) {
if (line.includes('[options]')) {
if (currentCommand)
commands.push(currentCommand);
return line;
}
else if (currentCommand && line && !line.includes('-h, --help')) {
return currentCommand + ' ' + line;
}
return currentCommand;
}
function formatCommands(commands) {
return commands.length > 0
? commands.map(cmd => '- ' + cmd).join('\n')
: 'No refactoring commands available';
}
async function main() {
await updateReadme();
}
if (require.main === module) {
main().catch(console.error);
}
//# sourceMappingURL=update-readme.js.map