Skip to content

Instantly share code, notes, and snippets.

@justingreenberg
Last active January 11, 2017 07:26
Show Gist options
  • Save justingreenberg/b14f72d82342198dc5e05b8cf56c6b4b to your computer and use it in GitHub Desktop.
Save justingreenberg/b14f72d82342198dc5e05b8cf56c6b4b to your computer and use it in GitHub Desktop.
import { exec } from 'child_process'
import debug from 'debug'
export default {
check: cwd => new Promise((resolve, reject) => {
let resolveObj = {
behind: false,
amount: 0,
branch: 'origin/stable'
}
const git = exec('git fetch && git status', { cwd })
const checkOutput = data => {
const isBehind = data.match(/Your branch is behind '(.+)' by (\d) commit/)
const isCurrent = data.match(/Your branch is up-to-date with '(.+)'/)
if (isBehind && isBehind.length > 1) {
const [,branch,amount] = isBehind
resolveObj = { behind: true, branch, amount }
}
if (isCurrent && isCurrent.length > 1) {
const [,branch] = isCurrent
resolveObj = { ...resolveObj, branch }
}
}
git.stdout.on('data', checkOutput)
git.stderr.on('data', checkOutput)
git.on('exit', code => {
if (code === 0 || code === '0') {
debug.log('Done checking for updates')
return resolve(resolveObj)
} else {
debug.log('Checking for updates failed')
return reject(new Error(`git exited with status code ${code}`)
}
})
}),
update: cwd => new Promise((resolve, reject) => {
const git = exec('git pull', { cwd })
const log = data => debug.log(data)
git.stdout.on('data', log)
git.stderr.on('data', log)
git.on('exit', code => {
if (code === 0 || code === '0') {
debug.log('Update successful')
return resolve()
} else {
debug.log('Update failed')
return reject(new Error(`git exited with status code ${code}`))
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment