@kaynooo/cli-changelog 0.0.0-stage → 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +212 -2
- package/dist/cli.js +3038 -0
- package/package.json +33 -4
- package/src/changelog.ts +150 -0
- package/src/cli.ts +298 -0
- package/src/config.ts +147 -0
- package/src/discord.ts +158 -0
- package/src/flags.ts +92 -0
- package/src/generate.ts +23 -0
- package/src/git.ts +166 -0
- package/src/gitea.ts +326 -0
- package/src/highlights.ts +142 -0
- package/src/index.ts +9 -0
- package/src/markdown.ts +199 -0
- package/src/parse.ts +8 -0
- package/src/types.ts +185 -0
- package/src/ui.ts +20 -0
package/src/discord.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { DiscordWebhookPayload } from '@kaynooo/utils/types/discord'
|
|
2
|
+
import type { Highlights } from './highlights'
|
|
3
|
+
import type { AuthorInfo, Commit, ResolvedChangelogOptions } from './types'
|
|
4
|
+
import { compareUrl, foldedCounts, formatLine, formatTitle, groupCommits, releaseUrl, shortRef } from './markdown'
|
|
5
|
+
|
|
6
|
+
// Discord rejects an embed whose description runs past this many characters.
|
|
7
|
+
const descriptionLimit = 4096
|
|
8
|
+
const color = 0xFB923C
|
|
9
|
+
const breakingColor = 0xED4245
|
|
10
|
+
|
|
11
|
+
const fenceRE = /^\s*(`{3,}|~{3,})/
|
|
12
|
+
const headingRE = /^#{1,6}(?:\s(.*))?$/
|
|
13
|
+
const strongRE = /\*\*|__/g
|
|
14
|
+
|
|
15
|
+
const isCommitLine = (line: string) => line.startsWith('- ')
|
|
16
|
+
|
|
17
|
+
// Drops commit lines from the end until the description fits, saying how many went; nothing when even the head and the tail alone run over.
|
|
18
|
+
function fitCommits(head: string[], lines: string[], tail: string) {
|
|
19
|
+
const total = lines.filter(isCommitLine).length
|
|
20
|
+
|
|
21
|
+
for (let kept = lines.length; kept >= 0; kept--) {
|
|
22
|
+
const shown = lines.slice(0, kept)
|
|
23
|
+
|
|
24
|
+
while (shown.length && kept < lines.length && !isCommitLine(shown.at(-1)!))
|
|
25
|
+
shown.pop()
|
|
26
|
+
|
|
27
|
+
const more = total - shown.filter(isCommitLine).length
|
|
28
|
+
const text = [...head, ...shown, ...(more ? ['', `*…and ${more} more*`] : []), '', tail].join('\n').trim()
|
|
29
|
+
|
|
30
|
+
if (text.length <= descriptionLimit)
|
|
31
|
+
return text
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Keeps the description under Discord's limit: commit lines go first, then the highlights' lines from the end, their title and the link to the page kept, and a code block cut open closed again.
|
|
38
|
+
function fitDescription(head: string[], lines: string[], tail: string) {
|
|
39
|
+
const fitted = fitCommits(head, lines, tail)
|
|
40
|
+
|
|
41
|
+
if (fitted !== undefined || !head.length)
|
|
42
|
+
return fitted ?? tail.slice(0, descriptionLimit)
|
|
43
|
+
|
|
44
|
+
for (let kept = head.length - 2; kept >= 1; kept--) {
|
|
45
|
+
const shown = head.slice(0, kept)
|
|
46
|
+
|
|
47
|
+
const open = openFence(shown)
|
|
48
|
+
|
|
49
|
+
if (open !== undefined)
|
|
50
|
+
shown.push(open)
|
|
51
|
+
|
|
52
|
+
const text = fitCommits([...shown, '…', head.at(-1)!], lines, tail)
|
|
53
|
+
|
|
54
|
+
if (text !== undefined)
|
|
55
|
+
return text
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return tail.slice(0, descriptionLimit)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// The marker of a code block the lines leave open, which closes it again.
|
|
62
|
+
function openFence(lines: string[]) {
|
|
63
|
+
let open: string | undefined
|
|
64
|
+
|
|
65
|
+
for (const line of lines) {
|
|
66
|
+
const marker = fenceRE.exec(line)?.[1]
|
|
67
|
+
|
|
68
|
+
if (marker !== undefined)
|
|
69
|
+
open = open !== undefined && marker.startsWith(open) ? undefined : open ?? marker
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return open
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const tableRowRE = /^\s*\|.*\|\s*$/
|
|
76
|
+
const tableRuleRE = /^\s*\|(?:\s*:?-+:?\s*\|)+\s*$/
|
|
77
|
+
// A `|` inside a code span or escaped is part of a cell.
|
|
78
|
+
const cellSplitRE = /(?<!\\)\|(?=(?:[^`]*`[^`]*`)*[^`]*$)/
|
|
79
|
+
|
|
80
|
+
// Discord draws no table: each row becomes a bullet, its first cell in bold and the others after it, the header and rule rows dropped.
|
|
81
|
+
function bulletedTables(lines: string[]) {
|
|
82
|
+
let fenced = false
|
|
83
|
+
const out: string[] = []
|
|
84
|
+
|
|
85
|
+
for (const [index, line] of lines.entries()) {
|
|
86
|
+
if (fenceRE.test(line))
|
|
87
|
+
fenced = !fenced
|
|
88
|
+
|
|
89
|
+
if (fenced || !tableRowRE.test(line)) {
|
|
90
|
+
out.push(line)
|
|
91
|
+
|
|
92
|
+
continue
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The header row is the one followed by the rule row.
|
|
96
|
+
if (tableRuleRE.test(line) || tableRuleRE.test(lines[index + 1] ?? ''))
|
|
97
|
+
continue
|
|
98
|
+
|
|
99
|
+
const [first = '', ...rest] = line.trim().slice(1, -1).split(cellSplitRE).map(cell => cell.trim())
|
|
100
|
+
|
|
101
|
+
out.push(`- **${first}**${rest.length ? `: ${rest.join(' · ')}` : ''}`)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return out
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// The highlights as Discord shows them: under a bold title, their headings in bold too, as an embed draws no heading.
|
|
108
|
+
function highlightLines(highlights: Highlights, options: ResolvedChangelogOptions) {
|
|
109
|
+
let fenced = false
|
|
110
|
+
const lines = bulletedTables(highlights.lines).flatMap((line) => {
|
|
111
|
+
if (fenceRE.test(line))
|
|
112
|
+
fenced = !fenced
|
|
113
|
+
|
|
114
|
+
const heading = fenced ? null : headingRE.exec(line)
|
|
115
|
+
|
|
116
|
+
if (!heading)
|
|
117
|
+
return [line]
|
|
118
|
+
|
|
119
|
+
// Its own bold would close the one around it; an empty heading has nothing to show.
|
|
120
|
+
const text = (heading[1] ?? '').replace(strongRE, '').trim()
|
|
121
|
+
|
|
122
|
+
return text ? [`**${text}**`] : []
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
return [`**${formatTitle(options.titles.highlights!, options)}**`, ...lines, `[Read more](${highlights.link})`]
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function generateDiscordPayload(commits: Commit[], options: ResolvedChangelogOptions, contributors: AuthorInfo[] = [], highlights?: Highlights): DiscordWebhookPayload {
|
|
129
|
+
const { sections, folded } = groupCommits(commits, options)
|
|
130
|
+
const lines: string[] = []
|
|
131
|
+
|
|
132
|
+
for (const section of sections) {
|
|
133
|
+
lines.push('', `**${formatTitle(section.title, options)}**`)
|
|
134
|
+
|
|
135
|
+
for (const commit of section.commits.toReversed())
|
|
136
|
+
lines.push(`- ${formatLine(commit, options, commit.scope ? `**${commit.scope}**: ` : '', true)}`)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (!sections.length && !folded.length)
|
|
140
|
+
lines.push('*No significant changes*')
|
|
141
|
+
|
|
142
|
+
const links = `[full notes](${releaseUrl(options)}) · [diff](${compareUrl(options)})`
|
|
143
|
+
const tail = folded.length ? `${formatTitle(options.titles.maintenance!, options)} *${foldedCounts(folded, options)}* · ${links}` : links
|
|
144
|
+
const authors = contributors.map(c => c.login ?? c.name).join(', ')
|
|
145
|
+
const range = `${shortRef(options.from)} → ${shortRef(options.to)}`
|
|
146
|
+
const avatar = contributors.find(c => c.avatar !== undefined)?.avatar
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
embeds: [{
|
|
150
|
+
title: `${String(options.releaseRepo).split('/').at(-1)} ${options.name || options.to}`.slice(0, 256),
|
|
151
|
+
url: releaseUrl(options),
|
|
152
|
+
color: commits.some(c => c.isBreaking) ? breakingColor : color,
|
|
153
|
+
description: fitDescription(highlights ? highlightLines(highlights, options) : [], lines, tail),
|
|
154
|
+
footer: { text: authors ? `${authors} · ${range}` : range, ...(avatar === undefined ? {} : { icon_url: avatar }) },
|
|
155
|
+
timestamp: new Date().toISOString(),
|
|
156
|
+
}],
|
|
157
|
+
}
|
|
158
|
+
}
|
package/src/flags.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { Options, parse } from '@kaynooo/cli-kit'
|
|
2
|
+
import type { ChangelogOptions } from './types'
|
|
3
|
+
import { jsonOption } from '@kaynooo/cli-kit'
|
|
4
|
+
|
|
5
|
+
export const shared = {
|
|
6
|
+
'token': { type: 'string', description: 'The forge\'s token, defaults to GITHUB_TOKEN, then on GitHub to gh\'s login' },
|
|
7
|
+
'from': { type: 'string', description: 'From tag' },
|
|
8
|
+
'to': { type: 'string', description: 'To tag' },
|
|
9
|
+
'repo': { type: 'string', short: 'r', description: 'Repository, e.g. kaynooo/cli, origin\'s by default (--gitea is kept as an alias)' },
|
|
10
|
+
'release-repo': { type: 'string', description: 'Release repository, defaults to --repo (--release-gitea is kept as an alias)' },
|
|
11
|
+
'name': { type: 'string', description: 'Name of the release' },
|
|
12
|
+
'contributors': { type: 'boolean', description: 'Show contributors' },
|
|
13
|
+
'prerelease': { type: 'boolean', description: 'Mark release as prerelease' },
|
|
14
|
+
'draft': { type: 'boolean', short: 'd', description: 'Mark release as draft' },
|
|
15
|
+
'output': { type: 'string', description: 'Output to file instead of sending to the forge' },
|
|
16
|
+
'capitalize': { type: 'boolean', description: 'Capitalize each commit message' },
|
|
17
|
+
'emoji': { type: 'boolean', description: 'Use emojis in section titles, --no-emoji to drop them' },
|
|
18
|
+
'group': { type: 'boolean', description: 'Nest commit messages under their scopes' },
|
|
19
|
+
'first-parent': { type: 'boolean', description: 'Walk only the first parent of each merge' },
|
|
20
|
+
'discord-webhook': { type: 'string', description: 'Discord webhook to post the release to, defaults to DISCORD_WEBHOOK' },
|
|
21
|
+
'dry-run': { type: 'boolean', description: 'Dry run (--dry is kept as an alias)' },
|
|
22
|
+
'assets': { type: 'string', multiple: true, description: 'Files to upload as assets to the release, repeatable or comma separated; quote globs, e.g. --assets \'dist/*.js\'' },
|
|
23
|
+
'json': jsonOption('release: {command, status, from, to, commits, notes, url, output, assetsFailed, discord}; undo: {command, status, tag, releaseNote, remoteTag, localTag, commitReset}. Progress goes to stderr'),
|
|
24
|
+
'debug': { type: 'boolean', description: 'Print the stack of an unexpected error' },
|
|
25
|
+
} as const satisfies Options
|
|
26
|
+
|
|
27
|
+
export const undoFlags = {
|
|
28
|
+
...shared,
|
|
29
|
+
note: { type: 'boolean', description: 'Also delete the release note on the forge (skips the question)' },
|
|
30
|
+
yes: { type: 'boolean', description: 'Skip confirmation prompts' },
|
|
31
|
+
} as const satisfies Options
|
|
32
|
+
|
|
33
|
+
export type Flags = NonNullable<ReturnType<typeof parse<typeof shared>>>['values']
|
|
34
|
+
|
|
35
|
+
const ALIASES: Record<string, string> = {
|
|
36
|
+
'--dry': '--dry-run',
|
|
37
|
+
'--gitea': '--repo',
|
|
38
|
+
'--release-gitea': '--release-repo',
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Rewrites the old flag names to the current ones, up to a bare `--`.
|
|
42
|
+
export function aliasFlags(argv: string[]): string[] {
|
|
43
|
+
const end = argv.indexOf('--')
|
|
44
|
+
const head = end === -1 ? argv : argv.slice(0, end)
|
|
45
|
+
|
|
46
|
+
return [...head.map((word) => {
|
|
47
|
+
const [name = '', ...value] = word.split('=')
|
|
48
|
+
const current = ALIASES[name]
|
|
49
|
+
|
|
50
|
+
return current === undefined ? word : [current, ...value].join('=')
|
|
51
|
+
}), ...end === -1 ? [] : argv.slice(end)]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Keeps only the keys given a value.
|
|
55
|
+
function defined<T extends object>(values: T): { [K in keyof T]?: Exclude<T[K], undefined> } {
|
|
56
|
+
return Object.fromEntries(Object.entries(values).filter(([, value]) => value !== undefined)) as { [K in keyof T]?: Exclude<T[K], undefined> }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The flags as config options; an absent flag is left out so the config file keeps the say.
|
|
60
|
+
export function toOptions(args: Flags): ChangelogOptions {
|
|
61
|
+
return defined({
|
|
62
|
+
token: args.token,
|
|
63
|
+
from: args.from,
|
|
64
|
+
to: args.to,
|
|
65
|
+
repo: args.repo,
|
|
66
|
+
releaseRepo: args['release-repo'],
|
|
67
|
+
name: args.name,
|
|
68
|
+
contributors: args.contributors,
|
|
69
|
+
prerelease: args.prerelease,
|
|
70
|
+
draft: args.draft,
|
|
71
|
+
output: args.output,
|
|
72
|
+
capitalize: args.capitalize,
|
|
73
|
+
emoji: args.emoji,
|
|
74
|
+
group: args.group,
|
|
75
|
+
firstParent: args['first-parent'],
|
|
76
|
+
discordWebhook: args['discord-webhook'],
|
|
77
|
+
dry: args['dry-run'],
|
|
78
|
+
assets: args.assets,
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// The token goes to the release's forge, so only --token and GITHUB_TOKEN (which Forgejo Actions and GitHub Actions set) are read here, and gh's login only for GitHub (resolveConfig).
|
|
83
|
+
export function withToken(options: ChangelogOptions): ChangelogOptions {
|
|
84
|
+
const token = [options.token, process.env.GITHUB_TOKEN].find(value => value !== undefined && value !== '')
|
|
85
|
+
|
|
86
|
+
return token === undefined ? options : { ...options, token }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// The release note is only asked about when nothing answers or skips the question.
|
|
90
|
+
export function shouldAskNote({ release, note, yes, dry }: { release: unknown, note: boolean | undefined, yes: boolean | undefined, dry: boolean | undefined }): boolean {
|
|
91
|
+
return Boolean(release) && note !== true && yes !== true && dry !== true
|
|
92
|
+
}
|
package/src/generate.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AuthorInfo, ChangelogOptions } from './types'
|
|
2
|
+
import { getGitDiff } from './changelog'
|
|
3
|
+
import { resolveConfig } from './config'
|
|
4
|
+
import { generateDiscordPayload } from './discord'
|
|
5
|
+
import { resolveAuthors } from './gitea'
|
|
6
|
+
import { readHighlights } from './highlights'
|
|
7
|
+
import { generateMarkdown } from './markdown'
|
|
8
|
+
import { parseCommits } from './parse'
|
|
9
|
+
|
|
10
|
+
export async function generate(options: ChangelogOptions) {
|
|
11
|
+
const resolved = await resolveConfig(options)
|
|
12
|
+
|
|
13
|
+
const rawCommits = await getGitDiff(resolved.fromRoot === true ? undefined : resolved.from, resolved.to, resolved.firstParent)
|
|
14
|
+
const commits = parseCommits(rawCommits, resolved)
|
|
15
|
+
const contributors: AuthorInfo[] = resolved.contributors ? await resolveAuthors(commits, resolved) : []
|
|
16
|
+
|
|
17
|
+
const highlights = await readHighlights(resolved)
|
|
18
|
+
|
|
19
|
+
const md = generateMarkdown(commits, resolved, contributors, highlights)
|
|
20
|
+
const discord = generateDiscordPayload(commits, resolved, contributors, highlights)
|
|
21
|
+
|
|
22
|
+
return { config: resolved, md, discord, commits, contributors }
|
|
23
|
+
}
|
package/src/git.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type { ChangelogOptions } from './types'
|
|
2
|
+
import { isSemver, parseSemver } from '@kaynooo/utils/semver'
|
|
3
|
+
import { $ } from 'bun'
|
|
4
|
+
|
|
5
|
+
const gitSuffixRE = /\.git$/
|
|
6
|
+
const sshRegex = /(?:ssh:\/\/[^/]+\/|^(?:[^@/\s]+@)?[^:/\s]+:)(\w[\w.-]*)\/(\w[\w.-]*)$/i
|
|
7
|
+
const regexSpecialRE = /[.*+?^${}()|[\]\\]/g
|
|
8
|
+
const httpsRegex = /https?:\/\/[^/]+\/(\w[\w.-]*)\/(\w[\w.-]*)$/i
|
|
9
|
+
const releaseVersionRE = /^[^.]*(?:\.[\d.]*|\d)$/
|
|
10
|
+
const TAG_DECORATION = /tag: ([^,\n]+)/g
|
|
11
|
+
const ORIGIN_HOST = /^(?:[a-z][a-z\d+.-]*:\/\/)?(?:[^@/]+@)?([^:/]+)/i
|
|
12
|
+
|
|
13
|
+
// The host of origin's URL, whether it is an scp address, ssh:// or https://; undefined with no origin.
|
|
14
|
+
export async function getOriginHost(): Promise<string | undefined> {
|
|
15
|
+
const url = (await $`git config --get remote.origin.url`.quiet().nothrow().text()).trim()
|
|
16
|
+
|
|
17
|
+
return ORIGIN_HOST.exec(url)?.[1]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function getGiteaRepo() {
|
|
21
|
+
const url = await execCommand('git', ['config', '--get', 'remote.origin.url'])
|
|
22
|
+
const cleanUrl = url.replace(gitSuffixRE, '')
|
|
23
|
+
|
|
24
|
+
let match = sshRegex.exec(cleanUrl)
|
|
25
|
+
|
|
26
|
+
match ||= httpsRegex.exec(cleanUrl)
|
|
27
|
+
|
|
28
|
+
if (!match)
|
|
29
|
+
throw new Error(`Can not parse Gitea repo from url ${url}`)
|
|
30
|
+
|
|
31
|
+
return `${match[1]}/${match[2]}`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function getCurrentGitBranch() {
|
|
35
|
+
const [tag] = (await execCommand('git', ['tag', '--points-at', 'HEAD', '--sort=-v:refname'])).split('\n')
|
|
36
|
+
|
|
37
|
+
return tag !== undefined && tag !== '' ? tag : execCommand('git', ['rev-parse', '--abbrev-ref', 'HEAD'])
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function isRepoShallow() {
|
|
41
|
+
return (await execCommand('git', ['rev-parse', '--is-shallow-repository'])).trim() === 'true'
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getSafeTagTemplate(template: string) {
|
|
45
|
+
return template.includes('%s') ? template : `${template}%s`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getVersionString(template: string, tag: string): string {
|
|
49
|
+
const pattern = template.split('%s').map(part => part.replace(regexSpecialRE, '\\$&')).join('(.+)')
|
|
50
|
+
const regex = new RegExp(`^${pattern}$`)
|
|
51
|
+
const match = regex.exec(tag)
|
|
52
|
+
|
|
53
|
+
return match?.[1] ?? tag
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Where a release ends: `to` when it names a commit, else HEAD, as the notes of a version not tagged yet run up to HEAD.
|
|
57
|
+
async function tipOf(to: string): Promise<string> {
|
|
58
|
+
return (await $`git rev-parse --verify -q ${`${to}^{commit}`}`.quiet().nothrow()).exitCode === 0 ? to : 'HEAD'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// The tags reachable from `to`, newest first, so a release's start is always one of its ancestors; every commit is walked, as --simplify-by-decoration hides a tag on an empty commit.
|
|
62
|
+
export async function getGitTags(firstParent = false, to = 'HEAD') {
|
|
63
|
+
const output = await execCommand('git', ['log', ...firstParent ? ['--first-parent'] : [], '--decorate-refs=refs/tags/', '--format=%D', await tipOf(to), '--'])
|
|
64
|
+
|
|
65
|
+
return [...output.matchAll(TAG_DECORATION)].map(match => match[1]?.trim() ?? '').filter(tag => tag !== '')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function getLastMatchingTag(
|
|
69
|
+
inputTag: string,
|
|
70
|
+
tagFilter: (tag: string) => boolean,
|
|
71
|
+
tagTemplate: string,
|
|
72
|
+
rollup: NonNullable<ChangelogOptions['rollup']> = {},
|
|
73
|
+
firstParent = false,
|
|
74
|
+
) {
|
|
75
|
+
const inputVersionString = getVersionString(tagTemplate, inputTag)
|
|
76
|
+
// `isSemver` only accepts stable `x.y.z` versions, so it doubles as a
|
|
77
|
+
// "is a stable (non-prerelease) version" check.
|
|
78
|
+
const isStableVersion = isSemver(inputVersionString)
|
|
79
|
+
const tags = await getGitTags(firstParent, inputTag)
|
|
80
|
+
const filteredTags = tags.filter(tagFilter)
|
|
81
|
+
|
|
82
|
+
let tag: string | undefined
|
|
83
|
+
|
|
84
|
+
// Doing a stable release, find the last stable release to compare with
|
|
85
|
+
if (isStableVersion) {
|
|
86
|
+
const input = parseSemver(inputVersionString)
|
|
87
|
+
const isMajor = input.minor === 0 && input.patch === 0
|
|
88
|
+
const isMinor = !isMajor && input.patch === 0
|
|
89
|
+
|
|
90
|
+
// On a rolled-up minor/major release, walk back to the previous minor/major
|
|
91
|
+
// tag so the range absorbs every intermediate patch (and minor) release.
|
|
92
|
+
let boundary: ((v: { minor: number, patch: number }) => boolean) | undefined
|
|
93
|
+
|
|
94
|
+
if (isMajor && rollup.major)
|
|
95
|
+
boundary = v => v.minor === 0 && v.patch === 0
|
|
96
|
+
else if (isMinor && rollup.minor)
|
|
97
|
+
boundary = v => v.patch === 0
|
|
98
|
+
|
|
99
|
+
tag = filteredTags.find((tag) => {
|
|
100
|
+
const versionString = getVersionString(tagTemplate, tag)
|
|
101
|
+
|
|
102
|
+
if (versionString === inputVersionString || !isSemver(versionString))
|
|
103
|
+
return false
|
|
104
|
+
|
|
105
|
+
return boundary ? boundary(parseSemver(versionString)) : true
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Fallback to the last tag, that are not the input tag
|
|
110
|
+
tag ||= filteredTags.find(tag => tag !== inputTag)
|
|
111
|
+
|
|
112
|
+
return tag
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export async function isRefGitTag(to: string) {
|
|
116
|
+
const ref = `refs/tags/${to}`
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
await $`git show-ref --verify ${ref}`.quiet()
|
|
120
|
+
|
|
121
|
+
return true
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return false
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export async function getFirstGitCommit(firstParent = false, to = 'HEAD') {
|
|
129
|
+
return (await execCommand('git', ['rev-list', '--max-parents=0', ...firstParent ? ['--first-parent'] : [], await tipOf(to), '--'])).split('\n').at(-1) ?? ''
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export async function deleteLocalTag(tag: string) {
|
|
133
|
+
await $`git tag -d ${tag}`.quiet()
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export async function hasRemoteGitTag(tag: string, remote = 'origin') {
|
|
137
|
+
return (await execCommand('git', ['ls-remote', '--tags', remote, `refs/tags/${tag}`])).length > 0
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export async function deleteRemoteGitTag(tag: string, remote = 'origin') {
|
|
141
|
+
await $`git push ${remote} --delete ${tag}`.quiet()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function getTagCommit(tag: string) {
|
|
145
|
+
return execCommand('git', ['rev-list', '-n', '1', tag])
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export async function getHeadCommit() {
|
|
149
|
+
return execCommand('git', ['rev-parse', 'HEAD'])
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export async function getCommitSubject(ref: string) {
|
|
153
|
+
return execCommand('git', ['log', '-1', '--format=%s', ref])
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export async function softResetLastCommit() {
|
|
157
|
+
await $`git reset --soft HEAD~1`.quiet()
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function isPrerelease(version: string) {
|
|
161
|
+
return !releaseVersionRE.test(version)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function execCommand(cmd: string, args: string[]) {
|
|
165
|
+
return (await $`${cmd} ${args}`.quiet().text()).trim()
|
|
166
|
+
}
|