From a326be721107cd9eba78d71a529fbed95a99f3e2 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 10 May 2023 15:33:16 -0400 Subject: [PATCH] add more CR padding --- src/command/print.js | 198 ++++++++++++++++++++++--------------------- 1 file changed, 101 insertions(+), 97 deletions(-) diff --git a/src/command/print.js b/src/command/print.js index f741203..69afd08 100644 --- a/src/command/print.js +++ b/src/command/print.js @@ -1,131 +1,135 @@ -import * as R from 'remeda'; -import fs from 'fs-extra'; -import yaml from 'yaml'; -import mkd from 'markdown-utils'; +import * as R from "remeda"; +import fs from "fs-extra"; +import yaml from "yaml"; +import mkd from "markdown-utils"; -const render_header = (source ) => { - let header = '# Changelog'; - - let name = source.project?.name +const render_header = (source) => { + let header = "# Changelog"; - const links = {}; + let name = source.project?.name; - if( source?.project?.homepage) { - links.homepage = source?.project?.homepage; - name = `[${name}][homepage]`; - } + const links = {}; - if(name) header += ` for ${name}`; + if (source?.project?.homepage) { + links.homepage = source?.project?.homepage; + name = `[${name}][homepage]`; + } - return { header, links } + if (name) header += ` for ${name}`; + return { header, links }; }; function render_links(links) { - - return Object.entries(links).map( - ([name,url]) => ` [${name}]: ${url}` - ).join( "\n") - + return Object.entries(links) + .map(([name, url]) => ` [${name}]: ${url}`) + .join("\n"); } -const render_change = (config,source)=>(change) => { - let body = change.desc; - let links = {}; +const render_change = (config, source) => (change) => { + let body = change.desc; + let links = {}; - return { - body, links - } + return { + body, + links, + }; }; -export const render_release = (config,source) => ( release ) => { - let links = {}; +export const render_release = (config, source) => (release) => { + let links = {}; - if( typeof release === 'string') return { - body: release, - links + if (typeof release === "string") + return { + body: release, + links, }; - if(release.version === 'NEXT' && !config.next) return { - body: '', - links, + if (release.version === "NEXT" && !config.next) + return { + body: "", + links, }; - let body = "## " + release.version; + let body = "## " + release.version; - if( release.date) { - body += " " + release.date; + if (release.date) { + body += " " + release.date; + } + + body += "\n\n"; + + const type_map = Object.fromEntries( + source.change_types.flatMap((entry) => + entry.keywords.map((k) => [k, entry]) + ) + ); + + const changes = release.changes.map((change) => + typeof change === "string" ? { desc: change, type: "" } : change + ); + + const unknown = R.uniq( + changes + .map(R.prop("type")) + .map((t) => t ?? "") + .filter((type) => !type_map[type]) + ); + + if (unknown.length) { + throw new Error( + "unknown change types encountered: " + + unknown.map((t) => `'${t}'`).join(", ") + ); + } + + for (const change_type of source.change_types) { + const type_changes = changes.filter(({ type }) => + change_type.keywords.includes(type) + ); + + if (type_changes.length === 0) continue; + + if (change_type.title) body += "\n" + mkd.h3(change_type.title) + "\n\n"; + + for (const c of type_changes) { + const res = render_change(config, source)(c); + body += mkd.li(res.body, 1) + "\n"; + links = { ...links, ...res.links }; } + } - body += "\n\n"; - - const type_map = Object.fromEntries(source.change_types.flatMap( - entry => entry.keywords.map( k => [ k, entry ]) - )); - - const changes = release.changes.map( change => typeof change === 'string'? { desc: change, type: '' } : change ); - - const unknown = R.uniq( changes.map(R.prop('type')).map( - t => t ?? '' - ).filter( type => !type_map[type])); - - if( unknown.length ) { - throw new Error( - "unknown change types encountered: " + unknown.map(t => `'${t}'`).join(', ') - ); - } - - for ( const change_type of source.change_types ) { - const type_changes = changes.filter( - ({type}) => change_type.keywords.includes(type) - ); - - if( type_changes.length === 0) continue; - - if( change_type.title ) - body += mkd.h2( change_type.title) + "\n\n"; - - for( const c of type_changes ) { - const res = render_change(config,source)(c); - body += mkd.li(res.body,1)+"\n"; - links = { ...links, ...res.links}; - } - } - - return { body, links }; + return { body, links }; }; const handler = async (config) => { - const source = await fs.readFile(config.source,'utf-8').then(yaml.parse); + const source = await fs.readFile(config.source, "utf-8").then(yaml.parse); - let output = ''; + let output = ""; - let {header,links} = render_header(source); + let { header, links } = render_header(source); - output += header + "\n\n"; + output += header + "\n\n"; - for ( const res of source.releases.map( render_release(config,source) )) { - output += res.body + "\n\n"; - links = {...links,...res.links}; - } - - output += "\n\n\n\n" + render_links(links); - - config.consola.raw(output); + for (const res of source.releases.map(render_release(config, source))) { + output += res.body + "\n\n"; + links = { ...links, ...res.links }; + } + output += "\n\n\n\n" + render_links(links); + config.consola.raw(output); }; export default { - command: 'print', - desc : 'render the changelog', - builder: (yargs) => { - yargs - //.boolean('json') - .boolean('next') -// .default('json',false) - .default('next',true); - }, - handler, -} - + command: "print", + desc: "render the changelog", + builder: (yargs) => { + yargs + //.boolean('json') + .boolean("next") + // .default('json',false) + .default("next", true); + }, + handler, +};