Skip to content

Instantly share code, notes, and snippets.

@neckro
Created November 28, 2016 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neckro/bbe28e413e43db7766dcf839d0c27a33 to your computer and use it in GitHub Desktop.
Save neckro/bbe28e413e43db7766dcf839d0c27a33 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
'use strict'
const path = require('path')
const replace = require('replace')
const semver = require('semver')
const Git = require('nodegit')
const buildCompiler = require('./build-prod.js')
const buildConfig = require('./build-config')
function buildProject () {
return new Promise((resolve, reject) => {
buildCompiler.run((err, stats) => {
if (err) throw err
console.log(stats.toString({
chunks: false,
colors: true
}))
resolve()
})
})
}
function bumpVersion (rootDir, versionString) {
// Update npm/bower files
const options = {
regex: '(\n "version": ")(.*?)(",?\n)',
replacement: '$1' + versionString + '$3',
paths: [
path.join(rootDir, 'package.json'),
path.join(rootDir, 'bower.json')
],
silent: true
}
return replace(options)
}
function tagCommit (rootDir, versionString, commitMessage, addedFiles) {
let repo
let index
let pendingIndex
let taggedCommit
let branchName
let remote = 'origin'
let tagName = commitMessage
return Git.Repository.open(rootDir)
// Open the repo before doing anything, to make sure it exists
.then(repoResult => (repo = repoResult))
// Get the name of the current branch
.then(() => repo.getCurrentBranch())
.then(branch => branch.name())
.then(branchNameResult => (branchName = branchNameResult.replace(/^refs\/heads\//, '')))
// Update the json files with the new version string
.then(() => bumpVersion(rootDir, versionString))
// Add the updated files to the index
.then(() => repo.refreshIndex())
.then(indexResult => {
index = indexResult
return Promise.all(addedFiles.map(file => index.addByPath(file)))
})
.then(() => index.write())
.then(() => index.writeTree())
.then(indexResult => (pendingIndex = indexResult))
// Get a reference to the parent commit
.then(() => Git.Reference.nameToId(repo, 'HEAD'))
.then(headId => repo.getCommit(headId))
// Do eet
.then(parentCommit => {
return repo.createCommit(
'HEAD',
repo.defaultSignature(),
repo.defaultSignature(),
commitMessage,
pendingIndex,
[parentCommit]
)
})
// Tag the commit
.then(commit => {
taggedCommit = commit
return repo.createLightweightTag(taggedCommit, tagName)
})
.then(() => repo.getCommit(taggedCommit))
.done(commit => {
console.log(`${commit.sha()} ${commit.message()}`)
console.log(`\nAll done! I'll let you do this part:`)
console.log(` git push`, remote, branchName.replace(/^refs\/heads\//, ''))
console.log(` git push`, remote, tagName)
})
}
function processArgs () {
const bumpLevel = process.argv[2]
if (!bumpLevel) {
console.log(`Usage: tag-commit.js <Level>\n`)
console.log(`Level can be one of: major, minor, patch, premajor, preminor, prepatch, or prerelease.`)
process.exit(1)
}
const currentVersion = buildConfig.version
const bumpedVersion = semver.inc(currentVersion, bumpLevel)
if (!bumpedVersion) {
console.warn(`Whoops, can't bump the version: "${buildConfig.version}"`)
process.exit(1)
}
buildProject().then(() => {
console.log(`Bumping: v${currentVersion} -> v${bumpedVersion}`)
return tagCommit(
buildConfig.repoRoot,
bumpedVersion,
`v${bumpedVersion}`,
[
'package.json',
'bower.json',
buildConfig.jsBundle,
buildConfig.cssBundle
]
)
})
}
if (require.main === module) {
processArgs()
}
module.exports = tagCommit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment