Skip to content

Commit

Permalink
show update warning
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Mar 14, 2017
1 parent 62ec57a commit 5a1e68c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ cd $WORKSPACE
yarn install --offline --no-progress --production

echo $VERSION > VERSION
echo $BRANCH > BRANCH

# create bin runner
cat << EOF > heroku
Expand Down
10 changes: 10 additions & 0 deletions lib/branch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fs = require('fs-extra')
const path = require('path')
let branch
try {
branch = fs.readFileSync(path.join(__dirname, '..', 'BRANCH'), 'utf-8').trim()
} catch (err) {
branch = 'v6'
}

module.exports = branch
38 changes: 28 additions & 10 deletions lib/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,38 @@ const {Command} = require('heroku-cli-command')
const path = require('path')
const dirs = require('../dirs')
const version = require('../version')
const currentBranch = require('../branch')
const lock = require('rwlockfile')
const config = require('../config')

class Update extends Command {
async run () {
this.fs = require('fs-extra')
this.got = require('got')
this.branch = 'v6'
this.action('heroku-cli: Updating CLI')
this.manifest = await this.fetchManifest()
if (this.flags.force || version !== this.manifest.version) {
this.manifest = await this.constructor.fetchManifest(this.args.branch || currentBranch)
if (!this.flags.force && version === this.manifest.version) {
this.action.done(`already on latest version: ${version}`)
} else if (!this.flags.force && config.updateDisabled) {
this.action.done(`not updating CLI: ${config.updateDisabled}`)
} else {
this.action(`heroku-cli: Updating CLI to ${this.color.green(this.manifest.version)}${this.branch === 'stable' ? '' : ' (' + this.color.yellow(this.branch) + ')'}`)
await this.update()
this.action.done()
} else this.action.done(`already on latest version: ${version}`)
}
this.action('heroku-cli: Updating plugins')
}

async fetchManifest () {
let url = `https://cli-assets.heroku.com/branches/${this.branch}/${process.platform}-${process.arch}`
let {body: manifest} = await this.got(url, {json: true})
static async fetchManifest (branch) {
const got = require('got')
let url = `https://cli-assets.heroku.com/branches/${branch}/${process.platform}-${process.arch}`
let {body: manifest} = await got(url, {json: true})
return manifest
}

async update () {
const got = require('got')
let url = `https://cli-assets.heroku.com/branches/${this.branch}/${this.base}.tar.gz`
let stream = await this.got.stream(url)
let stream = await got.stream(url)
let dir = path.join(dirs.data, 'jscli')
let tmp = path.join(dirs.data, 'jscli_tmp')
await this.extract(stream, tmp)
Expand Down Expand Up @@ -98,12 +104,21 @@ class Update extends Command {
}

static async autoupdate () {
if (!this.autoupdateNeeded) return
if (config.updateDisabled) return await this.warnIfUpdateAvailable()
const fs = require('fs-extra')
fs.writeFileSync(dirs.autoupdatefile, '')
const {spawn} = require('child_process')
spawn('heroku', ['update'])
}

static async warnIfUpdateAvailable () {
const manifest = await this.fetchManifest(currentBranch)
if (version !== manifest.version) {
console.error(`heroku-cli: update available from ${version} to ${manifest.version}`)
}
}

/**
* checks if there is an update running
* if there is an update running, it waits for it to complete, then restarts the CLI
Expand All @@ -116,12 +131,15 @@ class Update extends Command {
await this.restartCLI()
} else {
await lock.read(dirs.updatelockfile)
if (this.autoupdateNeeded) await this.autoupdate()
await this.autoupdate()
}
}
}

Update.topic = 'update'
Update.args = [
{name: 'branch', optional: true}
]
Update.flags = [
{name: 'force', char: 'f', hidden: true}
]
Expand Down
1 change: 1 addition & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.updateDisabled = 'running from npm'
2 changes: 1 addition & 1 deletion lib/version.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs')
const fs = require('fs-extra')
const path = require('path')
let version
try {
Expand Down

0 comments on commit 5a1e68c

Please sign in to comment.