Skip to content

Commit

Permalink
fix(semantic-workflow): filter out README sections that exists "close…
Browse files Browse the repository at this point in the history
… enough"
  • Loading branch information
snorrees committed Oct 31, 2022
1 parent 572ecdb commit 04a8b8a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
46 changes: 38 additions & 8 deletions src/presets/semver-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ async function updateReadme(options: InjectOptions) {
const readmePath = path.join(basePath, 'README.md')
const readme = (await readFile(readmePath, 'utf8').catch(errorToUndefined)) ?? ''

const {v3Banner, installUsage, developFooter} = await readmeSnippets(options)
const updatedReadme = [v3Banner, installUsage, readme, developFooter].filter(Boolean).join('\n\n')
await writeFile(readmePath, updatedReadme, {encoding: 'utf8'})
log.info('Updated README. Please review the changes.')
const {v3Banner, install, usage, developFooter} = await readmeSnippets(options)

const prependSections = missingSections(readme, [v3Banner, install, usage])
const appendSections = missingSections(readme, [developFooter])

if (prependSections.length || appendSections.length) {
const updatedReadme = [...prependSections, readme, ...appendSections]
.filter(Boolean)
.join('\n\n')
await writeFile(readmePath, updatedReadme, {encoding: 'utf8'})
log.info('Updated README. Please review the changes.')
}
}

async function readmeSnippets(options: InjectOptions) {
Expand All @@ -90,7 +98,7 @@ async function readmeSnippets(options: InjectOptions) {
> For the v2 version, please refer to the [v2-branch](${bestEffortUrl}).
`

const installUsage = outdent`
const install = outdent`
## Installation
\`\`\`
Expand All @@ -102,10 +110,10 @@ async function readmeSnippets(options: InjectOptions) {
\`\`\`
yarn add ${pkg.name}@studio-v3
\`\`\`
`

const usage = outdent`
## Usage
<TODO: Show usage here>
`

const developFooter = outdent`
Expand All @@ -131,11 +139,33 @@ async function readmeSnippets(options: InjectOptions) {

return {
v3Banner,
installUsage,
install,
usage,
developFooter,
}
}

/**
* Returns sections that does not exists "close enough" in readme
*/
export function missingSections(readme: string, sections: string[]) {
return sections.filter((section) => !closeEnough(section, readme))
}

/**
* a and b are considered "close enough" if > 50% of a lines exist in b lines
* @param a
* @param b
*/
function closeEnough(a: string, b: string) {
const aLines = a.split('\n')
const bLines = b.split('\n')

const matchingLines = aLines.filter((line) => bLines.find((bLine) => bLine === line)).length
const isCloseEnough = matchingLines > aLines.length * 0.5
return isCloseEnough
}

function semverWorkflowFiles(): FromTo[] {
return [
{from: ['.github', 'workflows', 'main.yml'], to: ['.github', 'workflows', 'main.yml']},
Expand Down
40 changes: 39 additions & 1 deletion test/semver-workflow.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import tap from 'tap'
import {readmeBaseurl} from '../src/presets/semver-workflow'
import {missingSections, readmeBaseurl} from '../src/presets/semver-workflow'
import {PackageJson} from '../src/actions/verify/types'
import outdent from 'outdent'

tap.test('readmeBaseUrl', async (t) => {
const testCases: {pkg: PackageJson; expectedUrl: string}[] = [
Expand Down Expand Up @@ -28,3 +29,40 @@ tap.test('readmeBaseUrl', async (t) => {

testCases.forEach(({pkg, expectedUrl}) => t.equal(readmeBaseurl(pkg), expectedUrl))
})

tap.test('missingSections', async (t) => {
const exactMatch = outdent`
This a
matches b
exactly c
`
const over50PercentMatch = outdent`
This x
matches y
enough z
`
const only50PercentMatch = outdent`
This
does
not
match
`
const sections = [exactMatch, over50PercentMatch, only50PercentMatch]

const readme = outdent`
This a
matches b
exactly c
This x
matches y
enough zzzzzz
This
does
miss
`

const missing = missingSections(readme, sections)
t.same(missing, [only50PercentMatch])
})

0 comments on commit 04a8b8a

Please sign in to comment.