Skip to content

Commit

Permalink
fix: improve web url printing
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 13, 2023
1 parent 8020af8 commit 6373447
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
22 changes: 22 additions & 0 deletions src/cli.ts
Expand Up @@ -29,11 +29,14 @@ cli
.action(async (args) => {
args.token = args.token || process.env.GITHUB_TOKEN

let webUrl = ''

try {
console.log()
console.log(dim(`changelo${bold('github')} `) + dim(`v${version}`))

const { config, md, commits } = await generate(args as any)
webUrl = `https://github.com/${config.github}/releases/new?title=${encodeURIComponent(String(config.name || config.to))}&body=${encodeURIComponent(String(md))}&tag=${encodeURIComponent(String(config.to))}&prerelease=${config.prerelease}`

console.log(cyan(config.from) + dim(' -> ') + blue(config.to) + dim(` (${commits.length} commits)`))
console.log(dim('--------------'))
Expand All @@ -42,14 +45,23 @@ cli
console.log()
console.log(dim('--------------'))

function printWebUrl() {
console.log()
console.error(yellow('Using the following link to create it manually:'))
console.error(yellow(webUrl))
console.log()
}

if (config.dry) {
console.log(yellow('Dry run. Release skipped.'))
printWebUrl()
return
}

if (!config.token) {
console.error(red('No GitHub token found, specify it via GITHUB_TOKEN env. Release skipped.'))
process.exitCode = 1
printWebUrl()
return
}

Expand All @@ -62,12 +74,14 @@ cli
if (!await hasTagOnGitHub(config.to, config)) {
console.error(yellow(`Current ref "${bold(config.to)}" is not available as tags on GitHub. Release skipped.`))
process.exitCode = 1
printWebUrl()
return
}

if (!commits.length && await isRepoShallow()) {
console.error(yellow('The repo seems to be clone shallowly, which make changelog failed to generate. You might want to specify `fetch-depth: 0` in your CI config.'))
process.exitCode = 1
printWebUrl()
return
}

Expand All @@ -77,6 +91,14 @@ cli
console.error(red(String(e)))
if (e?.stack)
console.error(dim(e.stack?.split('\n').slice(1).join('\n')))

if (webUrl) {
console.log()
console.error(red('Failed to create the release. Using the following link to create it manually:'))
console.error(yellow(webUrl))
console.log()
}

process.exit(1)
}
})
Expand Down
29 changes: 8 additions & 21 deletions src/github.ts
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import { $fetch } from 'ohmyfetch'
import { cyan, green, red, yellow } from 'kolorist'
import { cyan, green } from 'kolorist'
import { notNullish } from '@antfu/utils'
import type { AuthorInfo, ChangelogOptions, Commit } from './types'

Expand Down Expand Up @@ -31,26 +31,13 @@ export async function sendRelease(
prerelease: options.prerelease,
tag_name: options.to,
}

const webUrl = `https://github.com/${options.github}/releases/new?title=${encodeURIComponent(String(body.name))}&body=${encodeURIComponent(String(body.body))}&tag=${encodeURIComponent(String(options.to))}&prerelease=${options.prerelease}`

try {
console.log(cyan(method === 'POST' ? 'Creating release notes...' : 'Updating release notes...'))
const res = await $fetch(url, {
method,
body: JSON.stringify(body),
headers,
})
console.log(green(`Released on ${res.html_url}`))
}
catch (e) {
console.log()
console.error(red('Failed to create the release. Using the following link to create it manually:'))
console.error(yellow(webUrl))
console.log()

throw e
}
console.log(cyan(method === 'POST' ? 'Creating release notes...' : 'Updating release notes...'))
const res = await $fetch(url, {
method,
body: JSON.stringify(body),
headers,
})
console.log(green(`Released on ${res.html_url}`))
}

function getHeaders(options: ChangelogOptions) {
Expand Down

0 comments on commit 6373447

Please sign in to comment.