MagicMirror/js/check_config.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

/* Magic Mirror
*
* Checker configuration file
*
* By Rodrigo Ramírez Norambuena
* https://rodrigoramirez.com
*
* MIT Licensed.
*
*/
const Linter = require("eslint").Linter;
const linter = new Linter();
const path = require("path");
const fs = require("fs");
const rootPath = path.resolve(__dirname + "/../");
const config = require(rootPath + "/.eslintrc.json");
2020-05-18 09:53:34 +02:00
const Logger = require(rootPath + "/js/logger.js");
const Utils = require(rootPath + "/js/utils.js");
/* getConfigFile()
* Return string with path of configuration file
2019-06-04 10:15:50 +02:00
* Check if set by environment variable MM_CONFIG_FILE
*/
function getConfigFile() {
// FIXME: This function should be in core. Do you want refactor me ;) ?, be good!
let configFileName = path.resolve(rootPath + "/config/config.js");
if (process.env.MM_CONFIG_FILE) {
configFileName = path.resolve(process.env.MM_CONFIG_FILE);
}
return configFileName;
}
2018-01-25 20:07:51 +01:00
function checkConfigFile() {
const configFileName = getConfigFile();
2018-01-25 20:07:51 +01:00
// Check if file is present
if (fs.existsSync(configFileName) === false) {
2020-05-18 09:53:34 +02:00
Logger.error(Utils.colors.error("File not found: "), configFileName);
2018-01-25 20:07:51 +01:00
return;
}
2019-06-04 10:15:50 +02:00
// check permission
2018-01-25 20:07:51 +01:00
try {
fs.accessSync(configFileName, fs.F_OK);
} catch (e) {
2020-05-18 09:53:34 +02:00
Logger.log(Utils.colors.error(e));
2018-01-25 20:07:51 +01:00
return;
}
2018-01-25 20:07:51 +01:00
// Validate syntax of the configuration file.
2020-05-18 09:53:34 +02:00
Logger.info(Utils.colors.info("Checking file... "), configFileName);
2018-01-25 20:07:51 +01:00
// I'm not sure if all ever is utf-8
fs.readFile(configFileName, "utf-8", function (err, data) {
if (err) {
throw err;
}
const messages = linter.verify(data, config);
if (messages.length === 0) {
2020-05-18 09:53:34 +02:00
Logger.log("Your configuration file doesn't contain syntax errors :)");
2018-01-25 20:07:51 +01:00
return true;
} else {
2020-05-18 09:53:34 +02:00
// In case the there errors show messages and return
2020-05-25 18:57:15 +02:00
messages.forEach((error) => {
2020-05-18 09:53:34 +02:00
Logger.log("Line", error.line, "col", error.column, error.message);
});
}
2018-01-25 20:07:51 +01:00
});
}
checkConfigFile();