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

78 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-01-26 23:00:32 +00:00
#!/usr/bin/env node
2019-01-26 22:29:58 +00:00
const fs = require('fs')
2019-02-02 17:07:12 +00:00
const path = require('path')
2019-05-23 18:43:01 +00:00
const bent = require('bent')
const git = require('simple-git')()
const { execSync } = require('child_process')
2019-05-23 18:43:01 +00:00
const { promisify } = require('util')
2019-01-26 23:48:46 +00:00
2019-05-23 18:43:01 +00:00
const getlog = promisify(git.log.bind(git))
2019-05-23 18:43:01 +00:00
const get = bent('json', 'https://registry.npmjs.org/')
2019-05-23 18:43:01 +00:00
const event = JSON.parse(fs.readFileSync('/github/workflow/event.json').toString())
2019-02-02 17:07:12 +00:00
let pkg = require(path.join(process.cwd(), 'package.json'))
2019-05-23 18:13:42 +00:00
2019-05-23 18:43:01 +00:00
const run = async () => {
if (!process.env.NPM_AUTH_TOKEN) throw new Error('Merge-release requires NPM_AUTH_TOKEN')
2019-05-23 18:43:01 +00:00
let latest
try {
latest = await get(pkg.name + '/latest')
} catch (e) {
// unpublished
}
2019-05-23 18:13:42 +00:00
2019-05-23 18:43:01 +00:00
let messages
2019-05-23 18:13:42 +00:00
2019-05-23 18:43:01 +00:00
if (latest) {
if (latest.gitHead === process.env.GITHUB_SHA) return console.log('SHA matches latest release, skipping.')
if (latest.gitHead) {
let logs = await getlog({ from: latest.gitHead, to: process.env.GITHUB_SHA })
messages = logs.all.map(r => r.message + '\n' + r.body)
// g.log({from: 'f0002b6c9710f818b9385aafeb1bde994fe3b370', to: '53a92ca2d1ea3c55977f44d93e48e31e37d0bc69'}, (err, l) => console.log(l.all.map(r => r.message + '\n' + r.body)))
} else {
latest = null
}
}
if (!latest) {
messages = event.commits.map(commit => commit.message + '\n' + commit.body)
}
let version = 'patch'
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-09-09 19:24:33 +00:00
const run = str => process.stdout.write(execSync(str))
/* configure git */
const { GITHUB_ACTOR, GITHUB_TOKEN, GITHUB_REPOSITORY } = process.env
const remote = `https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git`
2019-09-09 21:01:58 +00:00
console.log({ remote })
run(`git remote add publish ${remote}`)
2019-09-09 19:24:33 +00:00
run(`git config user.name "Merge Release"`)
run(`git config user.email "merge-release@users.noreply.github.com"`)
2019-09-09 20:57:24 +00:00
console.log('configured')
2019-05-23 18:43:01 +00:00
let current = execSync(`npm view ${pkg.name} version`).toString()
2019-09-09 20:57:24 +00:00
run(`npm version --allow-same-version=true --git-tag-version=false ${current} `)
2019-05-23 18:43:01 +00:00
let newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString()
console.log(newVersion)
2019-09-09 20:57:24 +00:00
2019-09-09 19:24:33 +00:00
run(`git commit -a --amend --no-edit`)
2019-09-09 20:35:44 +00:00
2019-09-09 20:43:03 +00:00
run(`git fetch`)
run(`git branch tmp`)
run(`git checkout master`)
run(`git merge tmp`)
2019-09-09 20:35:44 +00:00
2019-09-09 19:24:33 +00:00
run(`npm publish --access=public`)
run(`git push publish master`)
await git.addTag(newVersion)
await git.pushTags('publish')
2019-05-23 18:43:01 +00:00
}
run()