vscode-xml/src/utils/UpdateNotifier.ts

43 lines
1.5 KiB
TypeScript
Raw Normal View History

2015-11-24 21:42:17 +00:00
'use strict';
2015-11-24 21:48:40 +00:00
import { window, commands } from 'vscode';
2015-11-24 21:42:17 +00:00
import { ExtensionManifestReader } from './ManifestUtils';
let req = require('request');
let semver = require('semver');
2015-11-24 22:06:24 +00:00
export function checkForUpdates() {
2015-11-24 21:42:17 +00:00
let manifestReader: ExtensionManifestReader = new ExtensionManifestReader('DotJoshJohnson.xml');
2015-11-24 22:06:24 +00:00
let currentVersion = '0.0.0';//manifestReader.version;
2015-11-24 21:42:17 +00:00
// use the GitHub api to determine the latest released version
let url = 'https://api.github.com/repos/DotJoshJohnson/vscode-xml/releases/latest';
let options = {
url: url,
headers: {
// the GitHub API requires a user agent header
// we are spoofing a Chrome user agent string here
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'
}
}
req(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
let release = JSON.parse(body);
2015-11-24 22:06:24 +00:00
let latestVersion = release.name.substring(1); // the release/tag is prefixed with a "v"
2015-11-24 21:42:17 +00:00
if (!release.draft && semver.gt(latestVersion, currentVersion)) {
2015-11-24 21:48:40 +00:00
let updateNowLabel = 'Update Now';
window.showInformationMessage(`Version ${latestVersion} of the ${manifestReader.displayName} extension is available.`, updateNowLabel).then((clicked) => {
if (clicked == updateNowLabel) {
commands.executeCommand('workbench.extensions.action.installExtension', manifestReader.displayName);
}
});
2015-11-24 21:42:17 +00:00
}
}
2015-11-24 22:06:24 +00:00
else {
console.log('XML Tools: Failed to get latest release information from GitHub.');
}
2015-11-24 21:42:17 +00:00
});
}