UNPKG

domotz-remote-pawn

Version:

Domotz Agent

63 lines (52 loc) 2.21 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 26/09/2015 */ const DOMOTZ_ENV = '/etc/domotz.env'; const ENV_VAR_REGEXP = /\s*export ([\w\.\-]+)\s*=\s*["']?([^'"]*)?["]?\s*/; var fs = require('fs'); module.exports.load = function(envFile) { // Configure the environment [For OpenWRT] // N.B. It's ok also for the other architectures var env = process.env; var content = fs.readFileSync(envFile || DOMOTZ_ENV, {encoding: 'utf8'}); var lines = content.toString().split('\n'); lines.forEach(function (line) { var keyValueArr = line.match(ENV_VAR_REGEXP); if (keyValueArr !== null) { var key = keyValueArr[1], value = keyValueArr[2]; if (value) { // Remove processes if (value.charAt(0) === '`' || value.slice(0,2) === '$(') { return; } // Expand newlines in quoted values var len = value ? value.length : 0; if (len > 0 && value.charAt(0) === '\"' && value.charAt(len - 1) === '\"') { value = value.replace(/\\n/gm, '\n'); } // Replace recursive environment variables env[key] = value.replace(/\${[A-Z_]+}/g, function (str) { return process.env[(/\$[{]?([A-Z_]+)[}]?/).exec(str)[1]]; }) || env[key]; } } }); };