Skip to content
Permalink
Browse files

fix(logging): extend shipit to provide log methods

  • Loading branch information
Marten Klitzke
Marten Klitzke committed Jun 18, 2018
1 parent 89fb2ec commit 954251249c4d44cde40014787d99562ec270bc83
Showing with 18 additions and 20 deletions.
  1. +18 −20 index.js
@@ -28,26 +28,24 @@ function extendShipit(shipit) {
shipit.currentPath = path.join(shipit.config.deployTo, 'current')
shipit.releasesPath = path.join(shipit.config.deployTo, 'releases')
shipit.deployTime = getDeployTime()
/* eslint-enable no-param-reassign */
}

/* eslint-disable func-names */
module.exports = function(shipit) {
function logInfo(message) {
shipit.logInfo = function logInfo(message) {
shipit.log(`\n\x1b[32m----->\x1b[0m ${message}`)
}

function logError(message) {
shipit.logError = function logError(message) {
shipit.log(`\n\x1b[33m ! \x1b[0m ${message}`)
}
/* eslint-enable no-param-reassign */
}

/* eslint-disable func-names */
module.exports = function(shipit) {
shipit.task('setup', async () => {
extendShipit(shipit)

logInfo('Creating deployTo directory')
shipit.logInfo('Creating deployTo directory')
await shipit.remote(`mkdir -p ${shipit.config.deployTo}`)

logInfo('Creating releases directory')
shipit.logInfo('Creating releases directory')
return shipit.remote(`mkdir -p ${shipit.releasesPath}`)
})

@@ -56,53 +54,53 @@ module.exports = function(shipit) {

const deployPath = path.join(shipit.releasesPath, shipit.deployTime)

logInfo('Installing deps & Building')
shipit.logInfo('Installing deps & Building')
await shipit.local(shipit.config.installCommand)
await shipit.local(shipit.config.buildCommand)

shipit.emit('build')

logInfo(`Creating new Release directory "${shipit.deployTime}"`)
shipit.logInfo(`Creating new Release directory "${shipit.deployTime}"`)
await shipit.remote(`mkdir -p ${deployPath}`)

logInfo('Uploading new Release')
shipit.logInfo('Uploading new Release')
await shipit.copyToRemote(`${shipit.config.dirToCopy}/`, `${deployPath}/`)

shipit.emit('uploaded')

logInfo('Updating current Symlink')
shipit.logInfo('Updating current Symlink')
await shipit.remote(`ln -nfs ${deployPath} ${shipit.currentPath}`)

logInfo(`Keeping "${shipit.config.keepReleases}" last releases, cleaning others`)
shipit.logInfo(`Keeping "${shipit.config.keepReleases}" last releases, cleaning others`)
const command = `(ls -rd ${shipit.releasesPath}/*|head -n ${shipit.config.keepReleases};ls -d ${shipit.releasesPath}/*)|sort|uniq -u|xargs rm -rf`
await shipit.remote(command)

shipit.emit('finished')

return logInfo(`Done. Deployed version ${shipit.deployTime}`)
return shipit.logInfo(`Done. Deployed version ${shipit.deployTime}`)
})

shipit.task('rollback', async () => {
extendShipit(shipit)

const current = getCurrentRelease(await shipit.remote(`readlink ${shipit.currentPath}`))
if (!current) {
logError('No Current Release - nothing to rollback')
shipit.logError('No Current Release - nothing to rollback')
return null
}

const releases = computeReleases(await shipit.remote(`ls -r1 ${shipit.releasesPath}`))
const previousRelease = releases.filter(item => item !== current)[0]
if (!previousRelease) {
logError('No Previous Release - nothing to rollback')
shipit.logError('No Previous Release - nothing to rollback')
return null
}

logInfo('Rolling back to previous Release...')
shipit.logInfo('Rolling back to previous Release...')
await shipit.remote(`ln -nfs ${shipit.releasesPath}/${previousRelease} ${shipit.currentPath}`)
await shipit.remote(`rm -rf ${shipit.releasesPath}/${current}`)

return logInfo(`Done. Rolled back to version ${previousRelease}`)
return shipit.logInfo(`Done. Rolled back to version ${previousRelease}`)
})
}
/* eslint-enable func-names */

0 comments on commit 9542512

Please sign in to comment.
You can’t perform that action at this time.