Skip to content

Instantly share code, notes, and snippets.

@dylanjha
Created January 17, 2018 16:13
Show Gist options
  • Save dylanjha/1d3c5b5045de19f34c668ffc7df43f33 to your computer and use it in GitHub Desktop.
Save dylanjha/1d3c5b5045de19f34c668ffc7df43f33 to your computer and use it in GitHub Desktop.
const execa = require('execa')
const Listr = require('listr')
const PROD_APP_NAME = '<replace with: heroku name of prod app>'
const STAGING_APP_NAME = '<replace with: heroku name of staging app>'
const tasks = new Listr([
{
title: 'setup git remote - staging',
task: () =>
setupGitRemote({
name: 'staging',
url: `[email protected]:${STAGING_APP_NAME}.git`
})
},
{
title: 'setup git remote - production',
task: () =>
setupGitRemote({
name: 'production',
url: `[email protected]:${PROD_APP_NAME}.git`
})
},
{
title: 'Install package dependencies with Yarn',
task: (ctx, task) =>
execa.shell('yarn').catch(() => {
throw new Error(
'Yarn not available, install it via `npm install -g yarn`'
)
})
},
{
title: 'restoring local db from staging',
task: () =>
execa.shell('yarn restore').catch(() => {
throw new Error('Error restoring mongo db from staging')
})
}
])
function setupGitRemote ({ name, url }) {
return execa.shell('git remote -v').then(result => {
const remotes = result.stdout
// This remote already exists, remove it and re-add it
// This ensures that the client machine will always have the most
// up-to-date value for the remote repository
if (remotes.match(new RegExp(name))) {
return execa.shell(`git remote rm ${name}`).then(() => {
return execa.shell(`git remote add ${name} ${url}`)
})
} else {
return execa.shell(`git remote add ${name} ${url}`)
}
})
}
tasks.run().catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment