Files
MagicMirror/clientonly/index.js
T

127 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-06-25 12:18:59 +02:00
"use strict";
2019-06-04 09:33:53 +02:00
// Use separate scope to prevent global scope pollution
2017-06-25 12:18:59 +02:00
(function () {
2021-04-18 15:36:00 +02:00
const config = {};
2017-06-25 12:18:59 +02:00
/**
* Helper function to get server address/hostname from either the commandline or env
*/
2017-06-29 21:32:48 +02:00
function getServerAddress() {
/**
2020-07-29 22:13:19 +02:00
* Get command line parameters
* Assumes that a cmdline parameter is defined with `--key [value]`
*
2020-07-29 22:13:19 +02:00
* @param {string} key key to look for at the command line
* @param {string} defaultValue value if no key is given at the command line
* @returns {string} the value of the parameter
*/
2017-06-29 21:32:48 +02:00
function getCommandLineParameter(key, defaultValue = undefined) {
2021-04-18 15:36:00 +02:00
const index = process.argv.indexOf(`--${key}`);
const value = index > -1 ? process.argv[index + 1] : undefined;
2017-06-29 21:32:48 +02:00
return value !== undefined ? String(value) : defaultValue;
2017-06-25 12:18:59 +02:00
}
2017-06-29 21:32:48 +02:00
// Prefer command line arguments over environment variables
["address", "port"].forEach((key) => {
config[key] = getCommandLineParameter(key, process.env[key.toUpperCase()]);
2019-06-05 10:23:58 +02:00
});
2020-03-28 12:37:50 +01:00
// determine if "--use-tls"-flag was provided
2020-04-14 14:10:29 +02:00
config["tls"] = process.argv.indexOf("--use-tls") > 0;
2017-06-25 12:18:59 +02:00
}
/**
2020-07-29 22:13:19 +02:00
* Gets the config from the specified server url
*
2020-08-01 17:06:56 +02:00
* @param {string} url location where the server is running.
2020-07-29 22:13:19 +02:00
* @returns {Promise} the config
*/
2017-06-25 12:18:59 +02:00
function getServerConfig(url) {
// Return new pending promise
return new Promise((resolve, reject) => {
2020-07-29 22:13:19 +02:00
// Select http or https module, depending on requested url
2017-06-25 12:18:59 +02:00
const lib = url.startsWith("https") ? require("https") : require("http");
const request = lib.get(url, (response) => {
2021-04-18 15:36:00 +02:00
let configData = "";
2017-06-29 21:22:00 +02:00
2019-06-04 09:33:53 +02:00
// Gather incoming data
2020-05-11 22:22:32 +02:00
response.on("data", function (chunk) {
2017-06-29 21:22:00 +02:00
configData += chunk;
});
// Resolve promise at the end of the HTTP/HTTPS stream
2020-05-11 22:22:32 +02:00
response.on("end", function () {
2017-06-29 21:22:00 +02:00
resolve(JSON.parse(configData));
});
});
2020-05-11 22:22:32 +02:00
request.on("error", function (error) {
2017-06-29 21:22:00 +02:00
reject(new Error(`Unable to read config from server (${url} (${error.message}`));
2017-06-25 12:18:59 +02:00
});
2019-06-05 10:23:58 +02:00
});
2019-06-04 09:33:53 +02:00
}
2017-06-25 12:18:59 +02:00
/**
2020-07-29 22:13:19 +02:00
* Print a message to the console in case of errors
*
2021-01-06 13:17:39 +01:00
* @param {string} message error message to print
2020-07-29 22:13:19 +02:00
* @param {number} code error code for the exit call
*/
2017-06-29 21:32:48 +02:00
function fail(message, code = 1) {
if (message !== undefined && typeof message === "string") {
console.log(message);
} else {
2020-03-28 12:37:50 +01:00
console.log("Usage: 'node clientonly --address 192.168.1.10 --port 8080 [--use-tls]'");
2017-06-29 21:32:48 +02:00
}
process.exit(code);
}
getServerAddress();
(config.address && config.port) || fail();
2021-04-18 15:36:00 +02:00
const prefix = config.tls ? "https://" : "http://";
2017-06-29 21:32:48 +02:00
2017-06-25 12:18:59 +02:00
// Only start the client if a non-local server was provided
if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].indexOf(config.address) === -1) {
2020-03-28 12:37:50 +01:00
getServerConfig(`${prefix}${config.address}:${config.port}/config/`)
.then(function (configReturn) {
2017-06-25 12:18:59 +02:00
// Pass along the server config via an environment variable
2021-04-18 15:36:00 +02:00
const env = Object.create(process.env);
const options = { env: env };
configReturn.address = config.address;
configReturn.port = config.port;
2020-03-28 12:37:50 +01:00
configReturn.tls = config.tls;
env.config = JSON.stringify(configReturn);
2017-06-25 12:18:59 +02:00
// Spawn electron application
const electron = require("electron");
2017-06-29 21:22:00 +02:00
const child = require("child_process").spawn(electron, ["js/electron.js"], options);
2017-06-25 12:18:59 +02:00
// Pipe all child process output to current stdout
child.stdout.on("data", function (buf) {
process.stdout.write(`Client: ${buf}`);
});
// Pipe all child process errors to current stderr
child.stderr.on("data", function (buf) {
process.stderr.write(`Client: ${buf}`);
});
child.on("error", function (err) {
process.stdout.write(`Client: ${err}`);
});
2017-07-25 17:52:44 -04:00
2018-05-26 19:36:46 +01:00
child.on("close", (code) => {
2019-06-05 08:48:22 +02:00
if (code !== 0) {
2017-07-25 17:52:44 -04:00
console.log(`There something wrong. The clientonly is not running code ${code}`);
}
});
2017-06-25 12:18:59 +02:00
})
.catch(function (reason) {
fail(`Unable to connect to server: (${reason})`);
});
} else {
fail();
}
2020-05-11 22:22:32 +02:00
})();