var Promise = require('promise') var requestPromise = require('./helpers/requestPromise.js') var BasePlugin = require('./base.js') const logger = require('../logger') class Kernel extends BasePlugin { constructor (config) { super(config) this.name = 'Kernel' } getKernelData (queryText) { var urlArgs = {} var url = 'https://www.kernel.org/releases.json' var promise = requestPromise(url, urlArgs) return promise } help () { // | Command | Description return '.kernel Hakee viimeisimmän kernelin version' } test (input) { // Kernel version if (input.match(/^\.kernel( (.+))?/i)) { return true } return false } result (input) { var that = this var resultPromise = new Promise(function (resolve, reject) { var res = input.match(/^\.kernel( (.+))?/i) if (res !== null) { var version = res[2] logger.info('Search kernel version ' + res[2]) that.getKernelData().then(function (data) { var text = 'kernel: ' // Store current versions! var releaseToVersion = {} data.releases.forEach(function (val) { var regex = /([0-9]+?)(\.([0-9]+))?(\.([0-9]+))?/ var result = val.version.match(regex) var major = result[1] var minor = result[3] if (typeof major !== 'undefined' && typeof minor !== 'undefined') { var releaseWithVersion = val.moniker + '-' + major + '-' + minor var release = val.moniker + '' if (typeof releaseToVersion[releaseWithVersion] !== 'undefined') { if (releaseToVersion[releaseWithVersion] !== val.version) { // new release } } if (typeof releaseToVersion[release] === 'undefined') { releaseToVersion[release] = val.version } releaseToVersion[releaseWithVersion] = val.version } }, function (error) { reject(error) }) if (typeof releaseToVersion[version] !== 'undefined') { text += 'Viimeisin versio on ' + releaseToVersion[version] + ' (' + version + ')' } else { text += 'Viimeisin vakaa versio on ' + data.latest_stable.version } resolve(text) }, function (error) { reject(error) }) } }) return resultPromise } } module.exports = Kernel