Skip to content

Commit

Permalink
feat: handle new version before generating changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 20, 2022
1 parent 858c13c commit fd56f6b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
21 changes: 13 additions & 8 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ async function main () {
const config = await loadChangelogConfig(cwd, {
from: args.from,
to: args.to,
output: args.output
output: args.output,
newVersion: args.r
})

const logger = consola.create({ stdout: process.stderr })
Expand All @@ -31,6 +32,15 @@ async function main () {
c.scope !== 'deps'
)

// Bump version optionally
if (args.bump || args.release) {
const newVersion = await bumpVersion(commits, config)
if (!newVersion) {
throw new Error('Unable to bump version based on changes.')
}
config.newVersion = newVersion
}

// Generate markdown
const markdown = generateMarkDown(commits, config)

Expand Down Expand Up @@ -65,18 +75,13 @@ async function main () {
await fsp.writeFile(config.output, changelogMD)
}

// Bump version optionally
let newVersion
if (args.bump || args.release) {
newVersion = await bumpVersion(commits, config)
}
// Commit and tag changes for release mode
if (args.release) {
if (args.commit !== false) {
await execa('git', ['commit', '-am', `chore(release): ${newVersion}`], { cwd })
await execa('git', ['commit', '-am', `chore(release): v${config.newVersion}`], { cwd })
}
if (args.tag !== false) {
await execa('git', ['tag', '-am', 'v' + newVersion, 'v' + newVersion], { cwd })
await execa('git', ['tag', '-am', 'v' + config.newVersion, 'v' + config.newVersion], { cwd })
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ChangelogConfig {
github: string
from: string
to: string
newVersion?: string
output: string | false
}

Expand Down
2 changes: 1 addition & 1 deletion src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function generateMarkDown (commits: GitCommit[], config: ChangelogConfig)
// Version Title
const compareLink = config.github ? `https://github.com/${config.github}/compare/${config.from}...${config.to}` : ''
markdown.push('',
'## ' + (compareLink ? `[${config.to}](${compareLink})` : `${config.to} (${config.from}..${config.to})`)
'## ' + (compareLink ? `[${config.newVersion || config.to}](${compareLink})` : `${config.to} (${config.from}..${config.to})`)
, '')

for (const type in config.types) {
Expand Down
15 changes: 10 additions & 5 deletions src/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function determineSemverChange (commits: GitCommit[], config: ChangelogCo
return hasMajor ? 'major' : (hasMinor ? 'minor' : (hasPatch ? 'patch' : null))
}

export async function bumpVersion (commits: GitCommit[], config: ChangelogConfig) {
export async function bumpVersion (commits: GitCommit[], config: ChangelogConfig): Promise<string | false> {
let type = determineSemverChange(commits, config)
const originalType = type

Expand All @@ -39,15 +39,20 @@ export async function bumpVersion (commits: GitCommit[], config: ChangelogConfig
}
}

if (type) {
if (config.newVersion) {
pkg.version = config.newVersion
} else if (type) {
// eslint-disable-next-line import/no-named-as-default-member
pkg.version = semver.inc(currentVersion, type)
config.newVersion = pkg.version
}

if (pkg.version !== currentVersion) {
consola.info(`Bumping version from ${currentVersion} to ${pkg.version} (${originalType})`)
await fsp.writeFile(pkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf8')
if (pkg.version === currentVersion) {
return false
}

consola.info(`Bumping version from ${currentVersion} to ${pkg.version} (${originalType})`)
await fsp.writeFile(pkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf8')

return pkg.version
}

0 comments on commit fd56f6b

Please sign in to comment.