json-merge-resolver
Version:
A rules-based JSON conflict resolver that parses Git conflict markers, reconstructs ours/theirs, and merges with deterministic strategies — beyond line-based merges.
4 lines (3 loc) • 1.69 kB
JavaScript
import{c as z}from"./chunk-PRRU4E6A.mjs";import{b as F}from"./chunk-XH3FBGBA.mjs";import{a as C}from"./chunk-7UWGXGKY.mjs";import{a as S}from"./chunk-IOL4YJZP.mjs";import{j as y}from"./chunk-HB6Q755Y.mjs";import{b as d,c as w}from"./chunk-54J6PE6E.mjs";import{a as b}from"./chunk-3PFH3U2T.mjs";import{execSync as k}from"child_process";import n from"fs/promises";var N=new Map,I=async({ours:o,theirs:s,base:f,format:t,filePath:e,config:a,normalizedConfig:m,logger:g,autoStage:u=!1})=>{var i;let r=[],[l]=await Promise.all([z({ours:o,theirs:s,base:f,filePath:e,conflicts:r,path:"",ctx:{config:m,strategies:(i=m.customStrategies)!=null?i:{},_strategyCache:N},logger:g}),y(e,a.backupDir)]);if(g.debug(e,JSON.stringify({merged:l,conflicts:r},null,2)),r.length===0){let c=await b(t,l);if(await n.writeFile(e,c,"utf8"),u)try{k(`git add ${e}`)}catch(p){g.warn(e,`Failed to stage file: ${p}`)}return{success:!0,conflicts:[]}}else{let c=await S(l,o,s,t);return await Promise.all([n.writeFile(e,`${c}
`,"utf8"),a.writeConflictSidecar?n.writeFile(`${e}.conflict.json`,`${JSON.stringify(r,null,2)}
`):null]),{success:!1,conflicts:r}}},_=async(o,s,f,t={})=>{let e=await C(t.loggerConfig,t.debug),a=await F(t);e.debug("git-merge",`Merging files: ours=${o}, base=${s}, theirs=${f}`);let[m,g,u]=await Promise.all([n.readFile(o,"utf8"),n.readFile(s,"utf8").catch(()=>"{}"),n.readFile(f,"utf8")]),r=d({...a,filename:""}),[l,i]=await w(m,r),[c,p]=await Promise.all([g,u].map(M=>w(M,[i]).then(([T])=>T))),$=typeof i=="string"?i:i.name,{success:x}=await I({ours:l,theirs:p,base:c,format:$,filePath:o,config:t,normalizedConfig:a,logger:e,autoStage:!1});await e.flush(),process.exit(x?0:1)};export{I as a,_ as b};