relaycode
Version:
A developer assistant that automates applying code changes from LLMs.
3 lines • 2.53 kB
JavaScript
import {loadConfigOrExit}from'../core/config';import {findStateFileByIdentifier,readAllStateFiles}from'../core/state';import {processPatch}from'../core/transaction';import {logger}from'../utils/logger';import'relaycode-core';import {v4}from'uuid';import {createConfirmationHandler}from'../utils/prompt';import {formatTransactionDetails}from'../utils/formatters';import o from'chalk';const M=async(v,p={},a=process.cwd(),l)=>{const y=createConfirmationHandler(p,l),d=await loadConfigOrExit(a);let i;const s=v??"1",h=/^-?\d+$/.test(s);if(h){const t=Math.abs(parseInt(s,10));if(isNaN(t)||t<=0){logger.error(`Invalid index. Please provide a positive number (e.g., ${o.cyan('"1"')} for the latest).`);return}i=t===1?"the latest transaction":`the ${o.cyan(t)}-th latest transaction`;}else i=`transaction with UUID '${o.cyan(s)}'`;logger.info(`Looking for ${i}...`);const n=await findStateFileByIdentifier(a,s,{skipReverts:!p.includeReverts});if(!n){if(logger.error(`Could not find ${i}.`),h){const t=await readAllStateFiles(a,{skipReverts:false}),f=await readAllStateFiles(a,{skipReverts:true}),u=(t?.length??0)-(f?.length??0);logger.info(`Found ${o.cyan(t?.length??0)} total transactions.`),u>0&&(logger.info(`${o.cyan(u)} of them are revert transactions, which are skipped by default.`),logger.info(`Use the ${o.cyan("--include-reverts")} flag to include them in the search.`));}return}if(logger.log(o.bold("Transaction to be reverted:")),formatTransactionDetails(n).forEach(t=>logger.log(t)),!await y(`
Are you sure you want to revert this transaction? (y/N)`)){logger.info("Revert operation cancelled.");return}const c=[],r=new Set(Object.keys(n.snapshot));for(const t of n.operations)t.type==="rename"?(r.delete(t.from),r.add(t.to)):t.type==="write"&&!r.has(t.path)?r.add(t.path):t.type==="delete"&&r.delete(t.path);for(const t of r)(!n.snapshot.hasOwnProperty(t)||n.snapshot[t]===null)&&c.push({type:"delete",path:t});for(const[t,f]of Object.entries(n.snapshot))f!==null&&c.push({type:"write",path:t,content:f,patchStrategy:"replace"});if(c.length===0){logger.warn("No operations to revert for this transaction.");return}const m=v4(),R=[`Reverting transaction ${n.uuid}.`,`Reasoning from original transaction: ${n.reasoning.join(" ")}`],w={control:{projectId:d.projectId,uuid:m},operations:c,reasoning:R};logger.info(`Creating new transaction ${o.gray(m)} to perform the revert.`),await processPatch(d,w,{cwd:a,prompter:l,yes:p.yes});};export{M as revertCommand};//# sourceMappingURL=revert.js.map
//# sourceMappingURL=revert.js.map