gh-action-bump-version/gh-action-bump-version-run.js

47 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-10-26 17:35:58 +00:00
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')
2019-10-26 18:01:14 +00:00
const event = JSON.parse(fs.readFileSync('/github/workflow/event.json').toString())
2019-10-26 17:35:58 +00:00
let pkg = require(path.join(process.cwd(), 'package.json'))
const run = async () => {
2019-10-26 18:11:31 +00:00
console.log('event:', event)
2019-10-26 18:01:14 +00:00
let messages = event.commits.map(commit => commit.message + '\n' + commit.body)
2019-10-26 18:44:21 +00:00
const commitMessage = 'version bump to'
2019-10-26 18:01:14 +00:00
const isVersionBump = messages.map(message => message.toLowerCase().includes(commitMessage)).includes(true)
if (isVersionBump) {
return
}
2019-10-26 17:35:58 +00:00
let version = 'patch'
2019-10-26 18:01:14 +00:00
if (messages.map(message => message.includes('BREAKING CHANGE')).includes(true)) {
version = 'major'
} else if (messages.map(message => message.toLowerCase().startsWith('feat')).includes(true)) {
version = 'minor'
}
2019-10-26 17:35:58 +00:00
2019-10-26 17:46:53 +00:00
const exec = str => {
return process.stdout.write(execSync(str))
}
2019-10-26 17:35:58 +00:00
2019-10-26 17:46:53 +00:00
let current = pkg.version.toString()
2019-10-26 18:11:31 +00:00
exec(`git checkout master`)
2019-10-26 17:35:58 +00:00
exec(`npm version --allow-same-version=true --git-tag-version=false ${current} `)
console.log('current:', current, '/', 'version:', version)
let newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString()
console.log('new version:', newVersion)
2019-10-26 18:01:14 +00:00
exec(`git commit -a -m 'ci: ${commitMessage} ${newVersion}'`)
2019-10-26 18:28:45 +00:00
const remoteRepo = `https://${process.env.GITHUB_ACTOR}:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git`
console.log('remoteRepo:', remoteRepo)
2019-10-26 17:35:58 +00:00
exec(`git tag ${newVersion}`)
2019-10-26 18:28:45 +00:00
exec(`git push "${remoteRepo}" --follow-tags`)
2019-10-26 18:30:39 +00:00
exec(`git push "${remoteRepo}" --tags`)
2019-10-26 17:35:58 +00:00
}
run()