const { syncAll } = require('./update-nested-deps')
const path = require('path')
const git = require('simple-git')(path.resolve(__dirname, '..'))

const gitOutputHandler = (command, stdout, stderr) => {
  stdout.pipe(process.stdout)
  stderr.pipe(process.stderr)
}

const GH_TOKEN = process.env.GH_TOKEN
const BRANCH = process.env.TRAVIS_PULL_REQUEST_BRANCH || process.env.TRAVIS_BRANCH || ''
const REPO = process.env.TRAVIS_REPO_SLUG

console.log('Current branch is:', BRANCH || '(empty)')
if (!BRANCH.startsWith('greenkeeper/')) {
  console.log('Not a Greenkeeper PR, exiting.')
  process.exit(0)
}

const changedPackages = syncAll()

git
  .outputHandler(gitOutputHandler)
  .addConfig('user.email', 'travis@travis-ci.org')
  .addConfig('user.name', 'Travis CI')
  .checkout(BRANCH)
  .add(changedPackages.map(p => path.join(p.folder, 'package.json')))
  .commit('chore(package): update nested deps')
  .addRemote('gh-repo', `https://${GH_TOKEN}@github.com/${REPO}.git`)
  .push('gh-repo', BRANCH, ['--set-upstream'])

@@ -429,16 +429,86 @@
 O_SLUG%0A%0A
+const exit = message =%3E %7B%0A  console.log(message)%0A  process.exit(0)%0A%7D%0A%0A
 console.
@@ -594,31 +594,20 @@
 per/')) 
-%7B%0A  console.log
+exit
 ('Not a 
@@ -632,36 +632,16 @@
 iting.')
-%0A  process.exit(0)%0A%7D
 %0A%0Aconst 
@@ -667,16 +667,94 @@
 yncAll()
+%0Aif (!changedPackages.length) exit('Nested packages are up-to-date, exiting.')
 %0A%0Agit%0A  

exit when up-to-date