UNPKG

madr-tools

Version:

Node.js CLI to to create and manage Markdown Architectural Decision Records (MADR)

12 lines (8 loc) 4.55 kB
#!/usr/bin/env node var k=Object.defineProperty;var p=(o,e)=>{for(var r in e)k(o,r,{get:e[r],enumerable:!0})};import pe from"yargs/yargs";import{hideBin as fe}from"yargs/helpers";var y={};p(y,{builder:()=>E,command:()=>I,desc:()=>z,handler:()=>u});import{existsSync as L,readdirSync as P,readFileSync as A,writeFileSync as j}from"fs";import{resolve as b}from"path";import{existsSync as F,lstatSync as R,readFileSync as N,writeFileSync as T}from"fs";import{parse as O,resolve as s}from"path";var a=".madrrc.json",c="template.md";var n={madrDirectory:"docs/decisions",indexFileName:"index.md"},m=({noErrorOnMissingConfig:o=!1,configPath:e}={})=>{let r=e||M(o),i=F(r)&&R(r).isFile()?JSON.parse(N(r,{encoding:"utf-8"})):{};return{...n,...i}},w=(o,{noErrorOnMissingConfig:e=!1}={})=>{T(M(e),JSON.stringify({...m({noErrorOnMissingConfig:e}),...o},null,2),"utf-8")},l=()=>{let o=f();return s(o,m({configPath:s(o,a)}).madrDirectory)},M=(o=!1)=>o?s(process.cwd(),a):s(f(),a),f=o=>{let e=o||process.cwd();if(F(s(e,a)))return e;if(O(e).root===e)throw Error("Could not determine project root from current working directory. Are you within the correct project and did you run madr init?");return f(s(e,".."))};import d from"chalk";var t={log:(o,e)=>console.log(d.white(o,e)),info:(o,e)=>console.log(d.cyan(o,e)),ok:(o,e)=>console.log(d.greenBright(o,e)),warn:(o,e)=>console.log(d.yellow(o,e)),error:(o,e)=>console.log(d.redBright(o,e))};var D="<!-- Autogenerated content below -->",B=`# Architectural Decision Log This log lists all architectural decisions of this project. `,_="To create new MADRs, use the `madr`-Tool. Checkout the possible options by running `npx madr --help` in your console.",I="index",z="Create or update index file based on MADR files",E=o=>o,u=()=>{try{let o=l(),{indexFileName:e}=m(),r=b(o,e),i=B;if(L(r)){let g=A(r,{encoding:"utf-8"}),C=g.indexOf(D);i=C>=0?g.substring(0,C):`${g} `}j(r,`${i}${J(o)}`,{encoding:"utf-8"}),t.ok(`Successfully generated ADR Log in ${e}`)}catch(o){t.error(o),process.exit(1)}},J=o=>`${D} ${G(o).map(e=>`* ${e}`).join(` `)} ${_}`,G=o=>P(o).filter(e=>e.match(/^\d{4}/)).map(e=>`[ADR-${e.substring(0,4)}](./${e}) - ${H(b(o,e))}`),H=o=>(A(o,{encoding:"utf-8"}).split(/\r?\n/).find(e=>/^# .*$/.test(e))||"").substring(2);var h={};p(h,{builder:()=>ee,command:()=>X,desc:()=>Z,handler:()=>oe});import{copyFileSync as U,existsSync as $,mkdirSync as Y,readdirSync as q}from"fs";import{dirname as K,join as v,resolve as S}from"path";import{fileURLToPath as Q}from"url";var V=Q(import.meta.url),W=K(V),X="init [path]",Z="Initialize the MADR directory under docs/decisions or the given [path]",ee=o=>o.positional("path",{type:"string",describe:"The path relative to the current working directory where to initialize the MADR directory",default:n.madrDirectory}).option("index",{alias:"i",describe:`By default the decision log is written to a file called '${n.indexFileName}'. It is possible to change this filename by setting this option`,choices:[n.indexFileName,"readme.md"],default:n.indexFileName}),oe=({path:o,index:e})=>{try{let r=S(process.cwd(),o),i=S(W,"../assets");$(r)?$(r)&&q(r).length&&(t.error(`The specified MADR directory '${r}' already exists and is not empty!`),process.exit(1)):Y(r,{recursive:!0}),U(v(i,c),v(r,c)),w({madrDirectory:o,indexFileName:e},{noErrorOnMissingConfig:!0}),t.ok(`MADR directory '${r}' successfully initialized.`)}catch(r){t.error(r),process.exit(1)}};var x={};p(x,{builder:()=>me,command:()=>ce,desc:()=>de,handler:()=>le});import{existsSync as re,readdirSync as te,readFileSync as ie,writeFileSync as ne}from"fs";import{join as se,resolve as ae}from"path";var ce="new <title>",de="Create a new MADR with the given title based on the template.md in your MADR directory",me=o=>o.positional("title",{type:"string",describe:"The title of the new MADR",default:n.madrDirectory}),le=({title:o})=>{try{let e=l(),r=ae(e,c);re(r)||(t.error(`MADR template file '${r}' not present!`),process.exit(1));let i=`${ge(e)}-${o.toLowerCase().replace(/[^0-9a-z\s\-_]/g,"").replace(/[\s_]+/g,"-").replace(/-{2,}/g,"-")}.md`;ne(se(e,i),ie(r,{encoding:"utf-8"}).replace(/^# .*$/m,`# ${o}`),{encoding:"utf-8"}),u(),t.ok(`Successfully created MADR file ${i}. You can start editing it now.`)}catch(e){t.error(e),process.exit(1)}},ge=o=>`${te(o).filter(e=>e.match(/^\d{4}/)).map(e=>parseInt(e.substring(0,4))).reduce((e,r)=>Math.max(e,r),-1)+1}`.padStart(4,"0");pe(fe(process.argv)).scriptName("madr").command([y,h,x]).demandCommand().strict().version("1.2.5").alias({h:"help",v:"version"}).argv;