Skip to content

Instantly share code, notes, and snippets.

@dylanjha dylanjha/setup.js
Created Jan 17, 2018

Embed
What would you like to do?
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: `git@heroku.com:${STAGING_APP_NAME}.git`
})
},
{
title: 'setup git remote - production',
task: () =>
setupGitRemote({
name: 'production',
url: `git@heroku.com:${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
You can’t perform that action at this time.