MagicMirror/tests/utils/weather_mocker.js
Kristjan ESPERANTO d276a7ddb9
Use template literals instead of string concatenation (#3066)
We have used it inconsistently till now. Template literals are more
modern and easier to maintain in my opinion.

Because that's a large amount of changes, here's a way to reproduce it:
I added the rule `"prefer-template": "error"` to the `.eslintrc.json`
and did an autofix. Since this caused a new problem in line 409 of
`newsfeed.js`, I reversed it in that line and also removed the rule from
the eslint config file.

The rule is described here:
https://eslint.org/docs/latest/rules/prefer-template

Note: I've played around with some other linter rules as well, and some
seem to point to some specific, non-cosmetic, issues. But before I dive
even deeper and then introduce even bigger and hardly understandable
changes at once, I thought I'd start with this simple cosmetic rule.
2023-03-19 14:32:23 +01:00

44 lines
1.4 KiB
JavaScript

const _ = require("lodash");
const fs = require("fs");
const path = require("path");
/**
* @param {string} type what data to read, can be "current" "forecast" or "hourly
* @param {object} extendedData extra data to add to the default mock data
* @returns {string} mocked current weather data
*/
const readMockData = (type, extendedData = {}) => {
let fileName;
switch (type) {
case "forecast":
fileName = "weather_forecast.json";
break;
case "hourly":
fileName = "weather_hourly.json";
break;
case "current":
default:
fileName = "weather_current.json";
break;
}
return JSON.stringify(_.merge({}, JSON.parse(fs.readFileSync(path.resolve(`${__dirname}/../mocks/${fileName}`)).toString()), extendedData));
};
const injectMockData = (configFileName, extendedData = {}) => {
let mockWeather;
if (configFileName.includes("forecast")) {
mockWeather = readMockData("forecast", extendedData);
} else if (configFileName.includes("hourly")) {
mockWeather = readMockData("hourly", extendedData);
} else {
mockWeather = readMockData("current", extendedData);
}
let content = fs.readFileSync(path.resolve(`${__dirname}../../../${configFileName}`)).toString();
content = content.replace("#####WEATHERDATA#####", mockWeather);
fs.writeFileSync(path.resolve(`${__dirname}../../../config/config.js`), content);
};
module.exports = { injectMockData };