refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
138 lines • 5.52 kB
JavaScript
;
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 commander_1 = require("commander");
const command_registry_1 = require("./command-registry");
const usage_tracker_1 = require("./usage-tracker");
const location_parser_1 = require("./core/location-parser");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const program = new commander_1.Command();
const commandRegistry = new command_registry_1.CommandRegistry();
program
.name('refakts')
.description('TypeScript refactoring tool based on ts-morph')
.version('1.0.0');
for (const command of commandRegistry.getAllCommands()) {
const warningText = !command.complete ? ' (warning: incomplete)' : '';
const cmd = program
.command(command.name)
.description(command.description + warningText)
.argument('<target>', 'TypeScript file or location format [file.ts line:col-line:col]');
addCommandOptions(cmd, command);
cmd
.addHelpText('after', command.getHelpText())
.action(async (target, options) => {
usage_tracker_1.UsageTracker.logUsage(command.name, process.argv.slice(2));
await executeRefactoringCommand(command, target, options);
});
}
function addCommandOptions(cmd, command) {
const options = loadCommandOptions(command.name);
for (const option of options) {
cmd.option(option.flags, option.description);
}
}
function loadCommandOptions(commandName) {
const optionsPath = getOptionsPath(commandName);
return readOptionsFile(optionsPath, commandName);
}
function getOptionsPath(commandName) {
return path.join(__dirname, 'commands', `${commandName}-options.json`);
}
function readOptionsFile(optionsPath, commandName) {
try {
const optionsData = fs.readFileSync(optionsPath, 'utf8');
return JSON.parse(optionsData);
}
catch (error) {
process.stderr.write(`Failed to load options for command ${commandName}: ${error}\n`);
return [];
}
}
async function executeRefactoringCommand(command, target, options) {
try {
await executeCommandWithTarget(command, target, options);
}
catch (error) {
handleCommandError(error);
}
}
async function executeCommandWithTarget(command, target, options) {
if (location_parser_1.LocationParser.isLocationFormat(target)) {
await executeWithLocationTarget(command, target, options);
}
else {
await executeWithFileTarget(command, target, options);
}
}
async function executeWithLocationTarget(command, target, options) {
const location = location_parser_1.LocationParser.parseLocation(target);
const optionsWithLocation = { ...options, location };
command.validateOptions(optionsWithLocation);
await command.execute(location.file, optionsWithLocation);
}
async function executeWithFileTarget(command, target, options) {
command.validateOptions(options);
await command.execute(target, options);
}
function handleCommandError(error) {
process.stderr.write(`Error: ${error.message}\n`);
process.exit(1);
}
function reorderHelpCommandArguments() {
const args = process.argv.slice(2);
const helpIndex = args.findIndex(arg => arg === '--help');
const commandIndex = findCommandArgumentIndex(args);
if (shouldReorderArguments(helpIndex, commandIndex)) {
const reorderedArgs = createReorderedArguments(args, helpIndex, commandIndex);
process.argv = ['node', 'cli.ts', ...reorderedArgs];
}
}
function findCommandArgumentIndex(args) {
return args.findIndex(arg => commandRegistry.getAllCommands().some(cmd => cmd.name === arg));
}
function shouldReorderArguments(helpIndex, commandIndex) {
return helpIndex !== -1 && commandIndex !== -1 && helpIndex < commandIndex;
}
function createReorderedArguments(args, helpIndex, commandIndex) {
const command = args[commandIndex];
const filteredArgs = args.filter((_, i) => i !== helpIndex && i !== commandIndex);
return [command, '--help', ...filteredArgs];
}
reorderHelpCommandArguments();
program.parse();
//# sourceMappingURL=cli.js.map