-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
151 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,2 @@ | ||
#!/usr/bin/env node | ||
import minimist from 'minimist' | ||
import changelogithub from './dist/index.mjs' | ||
|
||
const args = minimist(process.argv.slice(2), { | ||
boolean: [ | ||
'draft', | ||
'prerelease', | ||
'dry', | ||
], | ||
string: [ | ||
'token', | ||
'from', | ||
'to', | ||
'name', | ||
], | ||
alias: { | ||
draft: 'd', | ||
}, | ||
}) | ||
|
||
args.token = args.token || process.env.GITHUB_TOKEN | ||
|
||
changelogithub(args) | ||
import './dist/cli.mjs' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env node | ||
import minimist from 'minimist' | ||
import { blue, bold, cyan, dim, red, yellow } from 'kolorist' | ||
import { version } from '../package.json' | ||
import { generate, sendRelease } from './index' | ||
|
||
const args = minimist(process.argv.slice(2), { | ||
boolean: [ | ||
'draft', | ||
'dry', | ||
], | ||
string: [ | ||
'token', | ||
'from', | ||
'to', | ||
'name', | ||
], | ||
default: { | ||
prerelease: undefined, | ||
}, | ||
alias: { | ||
draft: 'd', | ||
}, | ||
}) | ||
|
||
args.token = args.token || process.env.GITHUB_TOKEN | ||
|
||
async function run() { | ||
const { config, md } = await generate(args as any) | ||
|
||
console.log() | ||
console.log(dim(`changelo${bold('github')} `) + dim(`v${version}`)) | ||
console.log(bold(config.github)) | ||
console.log(cyan(config.from) + dim(' -> ') + blue(config.to)) | ||
console.log(dim('--------------')) | ||
console.log() | ||
console.log(md.replaceAll(' ', '')) | ||
console.log() | ||
console.log(dim('--------------')) | ||
|
||
if (config.dry) | ||
return console.log(yellow('Dry run, skipped.')) | ||
|
||
if (!config.to.startsWith('v')) { | ||
console.log(yellow('Release version must starts with `v`, skipped.')) | ||
process.exitCode = 1 | ||
return | ||
} | ||
|
||
await sendRelease(config, md) | ||
} | ||
|
||
run() | ||
.catch((e) => { | ||
console.error(red(String(e))) | ||
console.error(dim(e.stack?.split('\n').slice(1).join('\n'))) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { getGitDiff, parseCommits } from 'changelogen' | ||
import type { ChangelogOptions } from './types' | ||
import { getCurrentGitBranch, getGitHubRepo, getLastGitTag, isPrerelease } from './git' | ||
import { generateMarkdown } from './markdown' | ||
|
||
export async function generate(options: ChangelogOptions) { | ||
const config: ChangelogOptions = { | ||
scopeMap: {}, | ||
types: { | ||
feat: { title: '🚀 Features' }, | ||
fix: { title: '🐞 Bug Fixes' }, | ||
perf: { title: '🏎 Performance' }, | ||
}, | ||
breakingChangeMessage: '🚨 Breaking Changes', | ||
...options as any, | ||
} | ||
|
||
config.from = config.from || await getLastGitTag() | ||
config.to = config.to || await getCurrentGitBranch() | ||
config.github = config.github || await getGitHubRepo() | ||
config.prerelease = config.prerelease ?? isPrerelease(config.to) | ||
|
||
if (config.to === config.from) | ||
config.from = await getLastGitTag(-2) | ||
|
||
const rawCommits = await getGitDiff(config.from, config.to) | ||
const commits = parseCommits(rawCommits, config) | ||
const md = generateMarkdown(commits, config) | ||
|
||
return { config, md, commits } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-disable no-console */ | ||
import { $fetch } from 'ohmyfetch' | ||
import { cyan, green } from 'kolorist' | ||
import type { ChangelogOptions } from './types' | ||
|
||
export async function sendRelease( | ||
options: ChangelogOptions, | ||
content: string, | ||
) { | ||
const headers = { | ||
accept: 'application/vnd.github.v3+json', | ||
authorization: `token ${options.token}`, | ||
} | ||
let url = `https://api.github.com/repos/${options.github}/releases` | ||
let method = 'POST' | ||
try { | ||
const exists = await $fetch(`https://api.github.com/repos/${options.github}/releases/tags/${options.to}`, { | ||
headers, | ||
}) | ||
if (exists.url) { | ||
url = exists.url | ||
method = 'PATCH' | ||
} | ||
} | ||
catch (e) { | ||
} | ||
|
||
const body = { | ||
body: content, | ||
draft: options.draft || false, | ||
name: options.name || options.to, | ||
prerelease: options.prerelease, | ||
tag_name: options.to, | ||
} | ||
|
||
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}`)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,5 @@ | ||
/* eslint-disable no-console */ | ||
import { $fetch } from 'ohmyfetch' | ||
import { getGitDiff, parseCommits } from 'changelogen' | ||
import semver from 'semver' | ||
import { blue, bold, cyan, dim, green } from 'kolorist' | ||
import type { ChangelogOptions } from './types' | ||
import { getCurrentGitBranch, getGitHubRepo, getLastGitTag } from './git' | ||
import { generateMarkdown } from './markdown' | ||
|
||
export default async function changelogithub( | ||
options: ChangelogOptions, | ||
) { | ||
const config: ChangelogOptions = { | ||
scopeMap: {}, | ||
types: { | ||
feat: { title: '🚀 Features' }, | ||
fix: { title: '🐞 Bug Fixes' }, | ||
perf: { title: '🏎 Performance' }, | ||
}, | ||
breakingChangeMessage: '🚨 Breaking Changes', | ||
...options as any, | ||
} | ||
|
||
config.from = config.from || await getLastGitTag() | ||
config.to = config.to || await getCurrentGitBranch() | ||
config.github = config.github || await getGitHubRepo() | ||
|
||
if (config.to === config.from) | ||
config.from = await getLastGitTag(-2) | ||
|
||
const rawCommits = await getGitDiff(config.from, config.to) | ||
const commits = parseCommits(rawCommits, config) | ||
|
||
if (!commits.length) { | ||
console.log('No commits') | ||
process.exitCode = 1 | ||
return | ||
} | ||
|
||
const md = generateMarkdown(commits, config) | ||
|
||
console.log(bold(config.github)) | ||
console.log(cyan(config.from) + dim(' -> ') + blue(config.to)) | ||
console.log(dim('--------------')) | ||
console.log() | ||
console.log(md) | ||
console.log() | ||
console.log(dim('--------------')) | ||
|
||
if (config.dry) | ||
return | ||
|
||
await sendRelease(config, md) | ||
} | ||
|
||
async function sendRelease( | ||
options: ChangelogOptions, | ||
content: string, | ||
) { | ||
const headers = { | ||
accept: 'application/vnd.github.v3+json', | ||
authorization: `token ${options.token}`, | ||
} | ||
let url = `https://api.github.com/repos/${options.github}/releases` | ||
let method = 'POST' | ||
try { | ||
const exists = await $fetch(`https://api.github.com/repos/${options.github}/releases/tags/${options.to}`, { | ||
headers, | ||
}) | ||
if (exists.url) { | ||
url = exists.url | ||
method = 'PATCH' | ||
} | ||
} | ||
catch (e) { | ||
} | ||
|
||
const version = options.from.startsWith('v') ? options.to.slice(1) : options.to | ||
const body = { | ||
body: content, | ||
draft: options.draft || false, | ||
name: options.name || options.to, | ||
prerelease: options.prerelease || semver.parse(version)!.prerelease.length > 0, | ||
tag_name: options.to, | ||
} | ||
|
||
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(`Release: ${res.html_url}`)) | ||
} | ||
export * from './types' | ||
export * from './github' | ||
export * from './git' | ||
export * from './markdown' | ||
export * from './generate' |