UNPKG

domotz-remote-pawn

Version:

Domotz Agent

55 lines (43 loc) 2.05 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. * * Created by Tommaso Latini <tommaso@domotz.com> on 17/12/15. */ const LOG_EVENT = 'HANDLER - (updateAgent)'; const AGENT_INFO_URL = 'https://portal.domotz.com/assets/domotz-npm-agent.json'; var updateAgent = function (resourceLocator) { if (process.env.DPLATFORM === 'openwrt') { console.warn('%s: Update option is disabled for openwrt platform', LOG_EVENT); return; } var registerRequest = resourceLocator.registerRequest; var pkgJson = resourceLocator.pkg.json; var pkgUpdate = resourceLocator.pkg.update; function updateIfObsolete(minReq) { var hasToBeUpdated = (minReq.name !== pkgJson.name || minReq.version > pkgJson.version); console.info('%s: Check last version of agent.' + 'Previous: [NAME: %s, VERSION: %s]' + ' Current: [NAME: %s, VERSION: %s]. %s', LOG_EVENT, minReq.name, minReq.version, pkgJson.name, pkgJson.version, hasToBeUpdated ? 'OBSOLETE VERSION => Start update procedure' : ''); if (hasToBeUpdated) { pkgUpdate.updateAgent(minReq.registry, minReq.name, minReq.version); } } registerRequest.sendWithPromise(process.env.AGENT_INFO_URL || AGENT_INFO_URL, 'GET', {}, null, true).then(updateIfObsolete); }; module.exports.handler = updateAgent;