Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request rdkcentral#91 from rdkcentral/fix/auto-update
Browse files Browse the repository at this point in the history
Fix/auto update
  • Loading branch information
michielvandergeest authored Oct 30, 2020
2 parents 253b754 + 64b841d commit 4e2a466
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 50 deletions.
7 changes: 7 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ program
updateCheck(true).then(() => uploadAction())
})

program
.command('update')
.description(['🔄', ' '.repeat(3), 'Update the Lightning-CLI to the latest version'].join(''))
.action(() => {
updateCheck(true).then(() => process.exit(1))
})

program.on('command:*', () => {
const suggestion = didYouMean(
program.args[0] || '',
Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
- [Docs](commands/docs.md)
- [Dist](commands/dist.md)
- [Upload](commands/upload.md)
- [Update](commands/update.md)
- [Environment variables](environmentvariables.md)
13 changes: 13 additions & 0 deletions docs/commands/update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Update command

_Update the Lightning-CLI to the latest version_

```bash
lng update
```

The Lightning-CLI has an auto-update mechanism. It check periodically if you are still on the latest version,
and updates automatically if you are not. This way you can be sure that you are always compatible with the latest
changes (either on the Lightning-SDK side, or the Metro App store side).

When you don't want to wait, but force an update to the latest version manually you can use the `lng update` command.
70 changes: 70 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"http-server": "^0.12.3",
"inquirer": "^7.3.3",
"is-online": "^8.4.0",
"latest-version": "^5.1.0",
"ora": "^5.1.0",
"replace-in-file": "^6.1.0",
"rollup": "^2.28.2",
Expand Down
68 changes: 18 additions & 50 deletions src/helpers/uptodate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,28 @@

const fs = require('fs')
const path = require('path')
const https = require('https')
const semver = require('semver')
const execa = require('execa')
const isOnline = require('is-online')
const latestVersion = require('latest-version')

const spinner = require('./spinner.js')
const exit = require('./exit.js')
const packageJson = require('../../package.json')
const ask = require('../helpers/ask')

const fetchLatestVersion = () => {
const gitBranch = getGitBranch()
const url = gitBranch
? 'https://raw.githubusercontent.com/rdkcentral/Lightning-CLI/' + gitBranch + '/package.json'
: false

return new Promise((resolve, reject) => {
if (!url) reject('Skipping auto update.')
https
.get(url, res => {
let body = ''

res.on('data', chunk => {
body += chunk
})

res.on('end', () => {
try {
let json = JSON.parse(body)
resolve(json.version)
} catch (error) {
reject('Unable to get CLI version from ' + url)
}
})
})
.on('error', error => {
reject(error)
})
// if a git folder exists, we are probably working of a clone,
// so likely we don't want to do any auto updates
const gitFolder = path.join(__dirname, '../../.git')
if (fs.existsSync(gitFolder)) {
resolve(false)
} else {
return latestVersion(packageJson.name)
.then(resolve)
.catch(reject)
}
})
}

Expand Down Expand Up @@ -93,15 +77,17 @@ const testConnection = async () => {
const checkForUpdate = () => {
spinner.start('Verifying if your installation of Lightning-CLI is up to date.')
return fetchLatestVersion()
.then(latestVersion => {
.then(version => {
if (version === false) {
spinner.succeed()
return Promise.resolve()
}
if (
semver.lt(packageJson.version, latestVersion) ||
semver.lt(packageJson.version, version) ||
packageJson.name === 'wpe-lightning-cli' // always update when old package name
) {
spinner.fail()
spinner.start(
'Attempting to update Lightning-CLI to the latest version (' + latestVersion + ')'
)
spinner.start('Attempting to update Lightning-CLI to the latest version (' + version + ')')

const options = ['install', '-g', '@lightningjs/cli']

Expand Down Expand Up @@ -138,22 +124,4 @@ const checkForUpdate = () => {
})
}

const getGitBranch = () => {
// if a git folder exists, we are probably working of a clone,
// so likely we don't want to do any auto updates
const gitFolder = path.join(__dirname, '../../.git')
if (fs.existsSync(gitFolder)) {
return false
}
// otherwise base on the package.json
else {
const packageJson = require(path.join(__dirname, '../../package.json'))
if (packageJson._requested && 'gitCommittish' in packageJson._requested) {
return packageJson._requested.gitCommittish || 'master' // gitCommittish is null for master
} else {
return false
}
}
}

module.exports = upToDate

0 comments on commit 4e2a466

Please sign in to comment.