|
| 1 | +import { Matcher } from "./matcher"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Built-in merge strategies. |
| 5 | + * ⚠️ Reserved names — plugin authors should avoid reusing these. |
| 6 | + */ |
| 7 | +export type BasicMergeStrategies = "merge" | "ours" | "theirs" | "skip" | "drop"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Result contract for a strategy function. |
| 11 | + * - `ok: true` → merge produced a value |
| 12 | + * - `ok: false` → merge failed (with optional reason) |
| 13 | + */ |
| 14 | +export type StrategyResult = { ok: true; value: unknown } | { ok: false; reason?: string }; |
| 15 | + |
| 16 | +/** |
| 17 | + * Strategy function signature. |
| 18 | + * Custom strategies receive both sides, optional base, file path, and context. |
| 19 | + */ |
| 20 | +export type StrategyFn<TContext = unknown> = (args: { |
| 21 | + ours: unknown; |
| 22 | + theirs: unknown; |
| 23 | + base?: unknown; |
| 24 | + path: string; |
| 25 | + filePath?: string; |
| 26 | + context?: TContext; |
| 27 | +}) => StrategyResult | Promise<StrategyResult>; |
| 28 | + |
| 29 | +/** |
| 30 | + * Utility type: forbids strategy names ending with "!". |
| 31 | + * (Reserved suffix for internal overrides.) |
| 32 | + */ |
| 33 | +type ForbidBangEnd<T extends string> = T extends `${string}!` ? never : T; |
| 34 | + |
| 35 | +/** |
| 36 | + * Rules tree: maps field globs → strategy names or nested rules. |
| 37 | + * - Keys: glob patterns (matcher configurable) |
| 38 | + * - Values: one or more strategies, or nested RuleTree |
| 39 | + */ |
| 40 | +export type RuleTree<T extends string = BasicMergeStrategies> = { |
| 41 | + [fieldGlob: string]: ForbidBangEnd<T>[] | RuleTree<ForbidBangEnd<T>>; |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * High-level config object for conflict resolution. |
| 46 | + */ |
| 47 | +export interface Config<T extends string = BasicMergeStrategies, TContext = unknown> { |
| 48 | + /** Fallback strategy when no rule matches */ |
| 49 | + defaultStrategy?: ForbidBangEnd<T> | ForbidBangEnd<T>[]; |
| 50 | + |
| 51 | + /** Rule tree mapping globs → strategies */ |
| 52 | + rules?: RuleTree<T>; |
| 53 | + |
| 54 | + /** Strategy → list of fields to apply it to */ |
| 55 | + byStrategy?: Partial<Record<ForbidBangEnd<T>, string[]>>; |
| 56 | + |
| 57 | + /** Custom strategies (excluding built-in names) */ |
| 58 | + customStrategies?: Record<Exclude<ForbidBangEnd<T>, BasicMergeStrategies>, StrategyFn<TContext>>; |
| 59 | + |
| 60 | + /** File inclusion globs */ |
| 61 | + include?: string[]; |
| 62 | + |
| 63 | + /** File exclusion globs */ |
| 64 | + exclude?: string[]; |
| 65 | + |
| 66 | + /** Glob matcher: `"micromatch"`, `"picomatch"`, or custom implementation */ |
| 67 | + matcher?: "micromatch" | "picomatch" | Matcher; |
| 68 | +} |
| 69 | + |
| 70 | +export type { Matcher }; |
0 commit comments