|
| 1 | +import { ArchitecturalDecisionRecord } from "./classes"; |
| 2 | +import { adr2md, md2adr } from "./parser"; |
| 3 | +import { adr2md400, md2adr400 } from "./madr400"; |
| 4 | +import type { Adr2MdOptions, Md2AdrOptions } from "./parser"; |
| 5 | +import type { FieldKey } from "./fields"; |
| 6 | +import type { ConsequenceKind, MadrTemplateVersion } from "./types"; |
| 7 | + |
| 8 | +export interface MadrTemplateField { |
| 9 | + key: FieldKey; |
| 10 | + label: string; |
| 11 | +} |
| 12 | + |
| 13 | +export type MadrTemplatePeopleFields = "deciders" | "decisionMakersConsultedInformed"; |
| 14 | + |
| 15 | +export interface MadrTemplateAdapter { |
| 16 | + version: MadrTemplateVersion; |
| 17 | + label: string; |
| 18 | + subLabel: string; |
| 19 | + description: string; |
| 20 | + fields: readonly MadrTemplateField[]; |
| 21 | + peopleFields: MadrTemplatePeopleFields; |
| 22 | + optionArgumentKinds: readonly ConsequenceKind[]; |
| 23 | + detect(markdown: string): boolean; |
| 24 | + parse(markdown: string, options?: Md2AdrOptions): ArchitecturalDecisionRecord; |
| 25 | + serialize(adr: ArchitecturalDecisionRecord, options?: Adr2MdOptions): string; |
| 26 | + carryOverOnSwitch(record: ArchitecturalDecisionRecord, from: MadrTemplateVersion): void; |
| 27 | +} |
| 28 | + |
| 29 | +export interface MadrRoundTripOptions { |
| 30 | + parse?: Md2AdrOptions; |
| 31 | + serialize?: Adr2MdOptions; |
| 32 | + compare?: (original: string, serialized: string) => boolean; |
| 33 | +} |
| 34 | + |
| 35 | +const FIELDS_212: readonly MadrTemplateField[] = [ |
| 36 | + { key: "date", label: "Date" }, |
| 37 | + { key: "status", label: "Status" }, |
| 38 | + { key: "deciders", label: "Deciders" }, |
| 39 | + { key: "technicalStory", label: "Technical Story" }, |
| 40 | + { key: "decisionDrivers", label: "Decision Drivers" }, |
| 41 | + { key: "optionDescription", label: "Option Description" }, |
| 42 | + { key: "optionProsAndCons", label: "Option Pros & Cons" }, |
| 43 | + { key: "positiveConsequences", label: "Positive Consequences" }, |
| 44 | + { key: "negativeConsequences", label: "Negative Consequences" }, |
| 45 | + { key: "links", label: "Links" }, |
| 46 | + { key: "relevantFiles", label: "Relevant Files" } |
| 47 | +]; |
| 48 | + |
| 49 | +const FIELDS_400: readonly MadrTemplateField[] = [ |
| 50 | + { key: "date", label: "Date" }, |
| 51 | + { key: "status", label: "Status" }, |
| 52 | + { key: "deciders", label: "Decision-makers" }, |
| 53 | + { key: "consulted", label: "Consulted" }, |
| 54 | + { key: "informed", label: "Informed" }, |
| 55 | + { key: "decisionDrivers", label: "Decision Drivers" }, |
| 56 | + { key: "optionDescription", label: "Option Description" }, |
| 57 | + { key: "optionProsAndCons", label: "Option Pros & Cons" }, |
| 58 | + { key: "consequences", label: "Consequences" }, |
| 59 | + { key: "confirmation", label: "Confirmation" }, |
| 60 | + { key: "moreInformation", label: "More Information" }, |
| 61 | + { key: "relevantFiles", label: "Relevant Files" } |
| 62 | +]; |
| 63 | + |
| 64 | +function detectsMadr400(markdown: string): boolean { |
| 65 | + const normalized = markdown.replace(/\r\n/g, "\n"); |
| 66 | + return ( |
| 67 | + normalized.startsWith("---\n") || |
| 68 | + /^### Consequences$/m.test(normalized) || |
| 69 | + /^### Confirmation$/m.test(normalized) || |
| 70 | + /^## More Information$/m.test(normalized) || |
| 71 | + /^\* Neutral, because /m.test(normalized) |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +const MADR_400_ADAPTER: MadrTemplateAdapter = { |
| 76 | + version: "4.0.0", |
| 77 | + label: "MADR 4.0.0", |
| 78 | + subLabel: "latest", |
| 79 | + description: |
| 80 | + "Decision-makers / consulted / informed, combined Consequences, Confirmation, neutral arguments and a More Information section.", |
| 81 | + fields: FIELDS_400, |
| 82 | + peopleFields: "decisionMakersConsultedInformed", |
| 83 | + optionArgumentKinds: ["good", "neutral", "bad"], |
| 84 | + detect: detectsMadr400, |
| 85 | + parse: (markdown) => md2adr400(markdown), |
| 86 | + serialize: (adr) => adr2md400(adr), |
| 87 | + carryOverOnSwitch: (record, from) => { |
| 88 | + if (from === "2.1.2" && record.decisionMakers === "" && record.deciders !== "") { |
| 89 | + record.decisionMakers = record.deciders; |
| 90 | + } |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +const MADR_212_ADAPTER: MadrTemplateAdapter = { |
| 95 | + version: "2.1.2", |
| 96 | + label: "MADR 2.1.2", |
| 97 | + subLabel: "classic", |
| 98 | + description: "The classic template: deciders, Technical Story, separate Positive / Negative Consequences and Links.", |
| 99 | + fields: FIELDS_212, |
| 100 | + peopleFields: "deciders", |
| 101 | + optionArgumentKinds: ["good", "bad"], |
| 102 | + detect: () => true, |
| 103 | + parse: (markdown, options) => md2adr(markdown, options), |
| 104 | + serialize: (adr, options) => adr2md(adr, options), |
| 105 | + carryOverOnSwitch: (record, from) => { |
| 106 | + if (from !== "2.1.2" && record.deciders === "" && record.decisionMakers !== "") { |
| 107 | + record.deciders = record.decisionMakers; |
| 108 | + } |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +export const MADR_TEMPLATE_ADAPTERS = [MADR_400_ADAPTER, MADR_212_ADAPTER] as const; |
| 113 | + |
| 114 | +export function getMadrTemplateAdapter(version: MadrTemplateVersion): MadrTemplateAdapter { |
| 115 | + const adapter = MADR_TEMPLATE_ADAPTERS.find((candidate) => candidate.version === version); |
| 116 | + if (!adapter) { |
| 117 | + throw new Error(`Unsupported MADR template version: ${version}`); |
| 118 | + } |
| 119 | + return adapter; |
| 120 | +} |
| 121 | + |
| 122 | +export function hasMadrTemplateField(adapter: MadrTemplateAdapter, key: FieldKey): boolean { |
| 123 | + return adapter.fields.some((field) => field.key === key); |
| 124 | +} |
| 125 | + |
| 126 | +export function detectMadrVersion(markdown: string): MadrTemplateVersion { |
| 127 | + return MADR_TEMPLATE_ADAPTERS.find((adapter) => adapter.detect(markdown))?.version ?? "2.1.2"; |
| 128 | +} |
| 129 | + |
| 130 | +export function parseMadr( |
| 131 | + markdown: string, |
| 132 | + version: MadrTemplateVersion = detectMadrVersion(markdown), |
| 133 | + options?: Md2AdrOptions |
| 134 | +): ArchitecturalDecisionRecord { |
| 135 | + return getMadrTemplateAdapter(version).parse(markdown, options); |
| 136 | +} |
| 137 | + |
| 138 | +export function serializeMadr( |
| 139 | + adr: ArchitecturalDecisionRecord, |
| 140 | + version: MadrTemplateVersion, |
| 141 | + options?: Adr2MdOptions |
| 142 | +): string { |
| 143 | + return getMadrTemplateAdapter(version).serialize(adr, options); |
| 144 | +} |
| 145 | + |
| 146 | +export function roundTripsMadr( |
| 147 | + markdown: string, |
| 148 | + version: MadrTemplateVersion = detectMadrVersion(markdown), |
| 149 | + options: MadrRoundTripOptions = {} |
| 150 | +): boolean { |
| 151 | + const serialized = serializeMadr(parseMadr(markdown, version, options.parse), version, options.serialize); |
| 152 | + return (options.compare ?? ((original, next) => original === next))(markdown, serialized); |
| 153 | +} |
0 commit comments