refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
202 lines (200 loc) • 7.44 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 execAsync = (0, util_1.promisify)(child_process_1.exec);
const CLAUDE_MD_PATH = path.join(__dirname, '..', 'CLAUDE.md');
const START_MARKER = '<!-- AUTO-GENERATED HELP START -->';
const END_MARKER = '<!-- AUTO-GENERATED HELP END -->';
async function getHelpOutput() {
try {
const { stdout } = await execAsync('ts-node src/cli.ts --help', {
cwd: path.join(__dirname, '..')
});
return stdout;
}
catch {
return 'Error: Could not generate help output';
}
}
function updateClaudeMd(helpOutput) {
validateClaudeMdExists();
let content = fs.readFileSync(CLAUDE_MD_PATH, 'utf8');
const autoGeneratedSection = createAutoGeneratedSection(helpOutput);
content = updateContentWithSection(content, autoGeneratedSection);
fs.writeFileSync(CLAUDE_MD_PATH, content);
console.log('✅ CLAUDE.md updated with current --help output');
}
function validateClaudeMdExists() {
if (!fs.existsSync(CLAUDE_MD_PATH)) {
console.error('CLAUDE.md not found');
process.exit(1);
}
}
function createAutoGeneratedSection(helpOutput) {
const commands = extractCommands(helpOutput);
return buildCommandsSection(commands);
}
function buildCommandsSection(commands) {
return `${START_MARKER}
## Available RefakTS Commands
\`\`\`
${commands}
\`\`\`
${END_MARKER}`;
}
function extractCommands(helpOutput) {
const lines = helpOutput.split('\n');
const commandsSection = findCommandsSection(lines);
return formatCommandsForMarkdown(commandsSection);
}
function findCommandsSection(lines) {
const commandsStartIndex = findCommandsStartIndex(lines);
if (commandsStartIndex === -1)
return [];
return extractCommandLines(lines, commandsStartIndex);
}
function findCommandsStartIndex(lines) {
return lines.findIndex(line => line.trim() === 'Commands:');
}
function extractCommandLines(lines, startIndex) {
const commandLines = [];
processLinesAfterStart(lines, startIndex, commandLines);
return commandLines;
}
function processLinesAfterStart(lines, startIndex, commandLines) {
let currentCommand = '';
currentCommand = processCommandLines(lines, startIndex, currentCommand, commandLines);
addFinalCommand(currentCommand, commandLines);
}
function processCommandLines(lines, startIndex, currentCommand, commandLines) {
for (let i = startIndex + 1; i < lines.length; i++) {
const line = lines[i].trim();
if (shouldStopExtraction(line))
break;
currentCommand = processCommandLine(line, currentCommand, commandLines);
}
return currentCommand;
}
function processCommandLine(line, currentCommand, commandLines) {
if (isRefactoringCommand(line)) {
return startNewCommand(line, currentCommand, commandLines);
}
else if (isCommandDescriptionContinuation(currentCommand, line)) {
return extendCurrentCommand(currentCommand, line);
}
return currentCommand;
}
function startNewCommand(line, currentCommand, commandLines) {
addFinalCommand(currentCommand, commandLines);
return line;
}
function extendCurrentCommand(currentCommand, line) {
return currentCommand + ' ' + line;
}
function addFinalCommand(currentCommand, commandLines) {
if (currentCommand) {
commandLines.push(currentCommand);
}
}
function shouldStopExtraction(line) {
return line === '' || line.startsWith('Available refactoring commands:') ||
line.includes('help [command]');
}
function isRefactoringCommand(line) {
return line.includes('[options]') && !line.includes('help [command]');
}
function isCommandDescriptionContinuation(currentCommand, line) {
return Boolean(currentCommand) && Boolean(line) && !line.includes('-h, --help');
}
function formatCommandsForMarkdown(commandLines) {
if (commandLines.length === 0)
return 'No refactoring commands available';
return commandLines.map(line => '- ' + line.trim()).join('\n');
}
function updateContentWithSection(content, section) {
const startIndex = content.indexOf(START_MARKER);
const endIndex = content.indexOf(END_MARKER);
if (startIndex !== -1 && endIndex !== -1) {
return replaceExistingSection(content, section, startIndex, endIndex);
}
else {
return addNewSection(content, section);
}
}
function replaceExistingSection(content, section, startIndex, endIndex) {
const before = content.substring(0, startIndex);
const after = content.substring(endIndex + END_MARKER.length);
return before + section + after;
}
function addNewSection(content, section) {
const dogFoodingIndex = content.indexOf('**CRITICAL: Dog fooding**');
if (dogFoodingIndex !== -1) {
return insertAfterDogFooding(content, section, dogFoodingIndex);
}
else {
return content + '\n\n' + section;
}
}
function insertAfterDogFooding(content, section, dogFoodingIndex) {
const afterDogFooding = content.substring(dogFoodingIndex);
const nextSectionMatch = afterDogFooding.match(/\n## /);
if (nextSectionMatch) {
return insertAtSectionBoundary(content, section, dogFoodingIndex, nextSectionMatch.index);
}
else {
return appendAtEnd(content, section);
}
}
function insertAtSectionBoundary(content, section, dogFoodingIndex, matchIndex) {
const insertIndex = dogFoodingIndex + matchIndex;
return content.substring(0, insertIndex) + '\n\n' + section + '\n' + content.substring(insertIndex);
}
function appendAtEnd(content, section) {
return content + '\n\n' + section;
}
async function main() {
const helpOutput = await getHelpOutput();
updateClaudeMd(helpOutput);
}
if (require.main === module) {
main().catch(console.error);
}
//# sourceMappingURL=update-claude-md.js.map