Skip to content

Commit

Permalink
feature: fetch metadata for latest server version
Browse files Browse the repository at this point in the history
  • Loading branch information
tkurki committed Sep 21, 2020
1 parent 77019da commit 307f70a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "signalk-server",
"version": "1.34.0",
"version": "1.33.0",
"description": "An implementation of a [Signal K](http://signalk.org) server for boats.",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 6 additions & 5 deletions src/interfaces/appstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const compareVersions = require('compare-versions')
const { installModule, removeModule } = require('../modules')
const {
findModulesWithKeyword,
getLatestServerVersion,
getLatestServerVersionInfo,
getAuthor
} = require('../modules')

Expand Down Expand Up @@ -118,9 +118,9 @@ module.exports = function(app) {
app.get('/appstore/available/', (req, res) => {
findPluginsAndWebapps()
.then(([plugins, webapps]) => {
getLatestServerVersion(app.config.version)
.then(serverVersion => {
const result = getAllModuleInfo(plugins, webapps, serverVersion)
getLatestServerVersionInfo(app.config.version)
.then(({version}) => {
const result = getAllModuleInfo(plugins, webapps, version)
res.send(JSON.stringify(result))
})
.catch(err => {
Expand Down Expand Up @@ -167,7 +167,8 @@ module.exports = function(app) {
updates: [],
installing: [],
storeAvailable: true,
isInDocker: process.env.IS_IN_DOCKER === 'true'
isInDocker: process.env.IS_IN_DOCKER === 'true',
nodeVersion: {}
}
}

Expand Down
37 changes: 29 additions & 8 deletions src/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,25 +264,46 @@ function doFetchDistTags() {
return fetch('http://registry.npmjs.org/-/package/signalk-server/dist-tags')
}

function getLatestServerVersion(
export interface ServerVersionInfo {
version: string
disttag: string
minimumNodeVersion: string
}

function getLatestServerVersionInfo(
currentVersion: string,
distTags = doFetchDistTags
): Promise<string> {
): Promise<ServerVersionInfo> {
return new Promise((resolve, reject) => {
distTags()
.then(npmjsResults => npmjsResults.json())
.then(npmjsParsed => {
const prereleaseData = semver.prerelease(currentVersion)
if (prereleaseData) {
if (semver.satisfies(npmjsParsed.latest, `>${currentVersion}`)) {
resolve(npmjsParsed.latest)
return [npmjsParsed.latest, 'latest']
} else {
resolve(npmjsParsed[prereleaseData[0]])
return [npmjsParsed[prereleaseData[0]], 'beta']
}
} else {
resolve(npmjsParsed.latest)
return([npmjsParsed.latest, 'latest'])
}
})
.then(([version, disttag]) => {
return fetch('https://registry.npmjs.org/signalk-server', {
headers: {
Accept: 'application/vnd.npm.install-v1+json'
}
})
.then(res => res.json())
.then(moduleMetadata => {
resolve({
version,
disttag,
minimumNodeVersion: `${moduleMetadata.versions[version].engines}`
})
})
})
.catch(reject)
})
}
Expand All @@ -295,10 +316,10 @@ function checkForNewServerVersion(
) => any,
getLatestServerVersionP: (
version: string
) => Promise<string> = getLatestServerVersion
) => Promise<ServerVersionInfo> = getLatestServerVersionInfo
) {
getLatestServerVersionP(currentVersion)
.then((version: string) => {
.then(({version}) => {
if (semver.satisfies(new SemVer(version), `>${currentVersion}`)) {
serverUpgradeIsAvailable(undefined, version)
}
Expand Down Expand Up @@ -326,7 +347,7 @@ module.exports = {
installModule,
removeModule,
findModulesWithKeyword,
getLatestServerVersion,
getLatestServerVersionInfo,
checkForNewServerVersion,
getAuthor,
restoreModules
Expand Down

0 comments on commit 307f70a

Please sign in to comment.