UNPKG

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.

2 lines (1 loc) 2.31 kB
import{c as x,d as h}from"./chunk-QY2IMCJG.mjs";var j={defaultStrategy:["merge","ours"],include:["**/*.json","**/*.yaml","**/*.yml","**/*.xml","**/*.toml"],exclude:["**/node_modules/**","**/dist/**"],debug:!1,writeConflictSidecar:!1},E=async t=>{let{rules:e,byStrategy:r,defaultStrategy:n,matcher:s,plugins:i,pluginConfig:o,customStrategies:a,...b}={...j,...t},C=await L(i,o),N={...a,...C},m={exact:Object.create(null),exactFields:Object.create(null),patterns:Object.create(null),default:A(n)},z=typeof s=="string"?await h(s):s!=null?s:x,S=0;if(r)for(let[u,c]of Object.entries(r)){if(!c)continue;let{name:l,important:f}=R(u);for(let p of c){let{key:g,important:y}=T(p);w(m,g,{strategies:[{name:l,important:f||y}],order:S++,source:g})}}return e&&I(e,[],(u,c)=>{let{key:l,important:f}=T(u),p=c.map(g=>{let{name:y,important:O}=R(g);return{name:y,important:O||f}});w(m,l,{strategies:p,order:S++,source:l})}),{...b,rules:m,matcher:z,customStrategies:N}},A=t=>(Array.isArray(t)?t:[t]).map(r=>{let n=r.endsWith("!");return F(r.slice(0,-1)),{name:n?r.slice(0,-1):r,important:n}}),R=t=>{let e=t.endsWith("!"),r=e?t.slice(0,-1):t;return F(r),{name:r,important:e}},F=t=>{if(t.endsWith("!"))throw new Error(`Strategy name "${t}" must not end with "!". Use "!" on field/glob to mark rule importance.`)},T=t=>{let e=t.endsWith("!"),r=e?t.slice(0,-1):t;if(!r)throw new Error(`Invalid rule key "${t}".`);return{key:r,important:e}},w=(t,e,r)=>{if(/^\[.*\]$/.test(e)){let s=e.slice(1,-1);if(s.replace(/\\\./g,"").includes(".")||s.trim()==="")throw new Error(`Invalid bracket form "${e}". Use a single bare key like "[id]".`);let i=`**.${s}.**`;d(t.patterns,i,r);return}if(/[*?[\]]/.test(e)){d(t.patterns,e,r);return}e.includes(".")?d(t.exact,e,r):d(t.exactFields,e,r)},I=(t,e,r)=>{for(let[n,s]of Object.entries(t)){let i=[...e,n];Array.isArray(s)?r(i.join("."),s):I(s,i,r)}},d=(t,e,r)=>{var n;(n=t[e])!=null||(t[e]=[]),t[e].push(r)},L=async(t,e)=>{if(!(t!=null&&t.length))return{};let r={};for(let n of t)try{let s=await import(n),i=s.default||s,o=e==null?void 0:e[n],a=i instanceof Function?await i(o):i;if(!a.strategies)throw new Error(`Plugin "${n}" does not export strategies`);a.init&&await a.init(o),Object.assign(r,a.strategies)}catch(s){throw new Error(`Failed to load plugin "${n}": ${s}`)}return r};export{j as a,E as b};