Skip to content

Commit

Permalink
feat: feature parity
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Mar 12, 2024
1 parent 96b8d6e commit 3221ea7
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 16 deletions.
29 changes: 29 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions scripts/changelog/changelog_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { renderBBCode } from "./render_bbcode.ts"
export const exampleCommits = parseCommits([
"feat: asdf (#1234)",
"feat!: test (#123)",
"fix(L10n): language stuff (#415)",
"fix(L10n): `language` stuff (#415)",
])
const exampleSections = getSections(exampleCommits)

Expand All @@ -30,7 +30,7 @@ Deno.test("parseCommits() correctly parse commits", () => {
type: "fix",
scopes: "L10n",
breaking: undefined,
subject: "language stuff",
subject: "`language` stuff",
pr: "415",
},
])
Expand All @@ -55,7 +55,7 @@ Deno.test("renderBBCode() outputs identical text", () =>
[h2]Fixes[/h2]
[list]
[*] language stuff ([url=https://github.com/scarf005/marisa/pull/415]#415[/url])
[*] [i]language[/i] stuff ([url=https://github.com/scarf005/marisa/pull/415]#415[/url])
[/list]
`,
))
25 changes: 12 additions & 13 deletions scripts/changelog/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { typedRegEx } from "https://deno.land/x/typed_regex@0.2.0/mod.ts"
import type { RegExCaptureResult } from "https://deno.land/x/typed_regex@0.2.0/type_parser.ts"

import { renderBBCode } from "./render_bbcode.ts"
import { getNextVersion } from "./semver.ts"
import { renderMarkdown } from "./render_markdown.ts"
import { renderStS } from "./render_sts.ts"

export type Commit = RegExCaptureResult<typeof re>
export type Sections = Record<string, Commit[]>
Expand Down Expand Up @@ -33,17 +36,13 @@ export const getSections = (commits: Commit[]): Sections => ({
if (import.meta.main) {
const [latestTag] = await getTags()
const commits = await getCommits({ from: latestTag, to: "main" }).then(parseCommits)

const sections: Sections = getSections(commits)

console.log(latestTag)
console.log(sections)
console.log(commits)
console.log(
renderBBCode({
version: "v1.0.0",
date: new Date().toISOString().split("T")[0],
sections,
}),
)
const option = {
version: getNextVersion(latestTag, commits),
date: new Date().toISOString().split("T")[0],
sections: getSections(commits),
}

for (const render of [renderBBCode, renderMarkdown, renderStS]) {
console.log(render(option), "\n")
}
}
1 change: 1 addition & 0 deletions scripts/changelog/render_bbcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ChangelogRenderer, renderSections, SectionFormatter } from "./render.ts

const fmtCommit = (x: Commit) =>
` [*] ${x.subject} ([url=https://github.com/scarf005/marisa/pull/${x.pr}]#${x.pr}[/url])`
.replace(/`([^`]+)`/g, "[i]$1[/i]")

const fmtSection: SectionFormatter = ([section, commits]) =>
`[h2]${section}[/h2]\n` + "[list]\n" + commits.map(fmtCommit).join("\n") + "\n[/list]"
Expand Down
16 changes: 16 additions & 0 deletions scripts/changelog/render_markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { outdent } from "$dax/src/deps.ts"
import { Commit } from "./mod.ts"
import { ChangelogRenderer, renderSections, SectionFormatter } from "./render.ts"

const fmtCommit = (x: Commit) =>
`- ${x.subject} (#${x.pr})`

const fmtSection: SectionFormatter = ([section, commits]) =>
`## ${section}\n\n` + commits.map(fmtCommit).join("\n")

export const renderMarkdown: ChangelogRenderer = ({ version, date, sections }) =>
outdent`
# ${version} (${date})
${renderSections({ fmtSection, sections })}
`
20 changes: 20 additions & 0 deletions scripts/changelog/render_sts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { outdent } from "$dax/src/deps.ts"
import { Commit } from "./mod.ts"
import { ChangelogRenderer, renderSections, SectionFormatter } from "./render.ts"

const fmtCommit = (x: Commit) =>
`- ${x.subject}`
.replace(/`([^`]+)`/g, "[i]$1[/i]")
.replace(/\(#(\d+)\)/g, "")

const fmtSection: SectionFormatter = ([section, commits]) =>
`* ${section}\n\n` + commits.map(fmtCommit).join("\n")

export const renderStS: ChangelogRenderer = ({ version, date, sections }) =>
outdent`
What's new in ${version} (${date})
${renderSections({ fmtSection, sections })}
please visit https://github.com/scarf005/Marisa/releases/latest for more info
`
17 changes: 17 additions & 0 deletions scripts/changelog/semver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { SemVer } from "$std/semver/types.ts"
import type { Commit } from "./mod.ts"

import { format, increment, parse } from "$std/semver/mod.ts"

const increaseBy = (commit: Commit): "major" | "minor" | "patch" =>
commit.breaking ? "major" : commit.type === "feat" ? "minor" : "patch"

export const increaseVersion = (begin: SemVer, commits: Commit[]): SemVer =>
commits.reduce(({ ver, count }, commit) => {
const by = increaseBy(commit)
count[by]++
return (count[by] > 1) ? { ver, count } : { ver: increment(ver, increaseBy(commit)), count }
}, { ver: begin, count: { major: 0, minor: 0, patch: 0 } }).ver

export const getNextVersion = (latest: string, commits: Commit[]): string =>
"v" + format(increaseVersion(parse(latest), commits))
35 changes: 35 additions & 0 deletions scripts/changelog/semver_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { assertObjectMatch } from "$std/assert/assert_object_match.ts"
import { assertEquals } from "$std/assert/assert_equals.ts"
import { getNextVersion, increaseVersion } from "./semver.ts"

const major = { type: "fix", breaking: "!", subject: "big stuff", pr: "123", scopes: undefined }
const minor = { type: "feat", breaking: undefined, subject: "feat", pr: "124", scopes: undefined }
const patch = { type: "fix", breaking: undefined, subject: "fix", pr: "125", scopes: undefined }

Deno.test("increaseVersion() only increases biggest change", async (t) => {
await t.step("major", () => {
assertObjectMatch(
increaseVersion({ major: 1, minor: 0, patch: 0 }, [
minor,
patch,
major,
]),
{ major: 2, minor: 0, patch: 0 },
)
})

await t.step("minor", () => {
assertObjectMatch(
increaseVersion({ major: 1, minor: 0, patch: 0 }, [patch, minor]),
{ major: 1, minor: 1, patch: 0 },
)
})
})

Deno.test("nextVersion() returns correct version", () => {
assertEquals(getNextVersion("v1.0.0", [minor, patch]), "v1.1.1")
})

Deno.test("nextVersion() handles multiple patch in single release", () => {
assertEquals(getNextVersion("v1.0.0", [patch, patch, patch]), "v1.0.1")
})

0 comments on commit 3221ea7

Please sign in to comment.