Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

check_config.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Magic Mirror
  2. *
  3. * Check the configuration file for errors
  4. *
  5. * By Rodrigo Ramírez Norambuena https://rodrigoramirez.com
  6. * MIT Licensed.
  7. */
  8. const Linter = require("eslint").Linter;
  9. const linter = new Linter();
  10. const path = require("path");
  11. const fs = require("fs");
  12. const rootPath = path.resolve(`${__dirname}/../`);
  13. const Log = require(`${rootPath}/js/logger.js`);
  14. const Utils = require(`${rootPath}/js/utils.js`);
  15. /**
  16. * Returns a string with path of configuration file.
  17. * Check if set by environment variable MM_CONFIG_FILE
  18. *
  19. * @returns {string} path and filename of the config file
  20. */
  21. function getConfigFile() {
  22. // FIXME: This function should be in core. Do you want refactor me ;) ?, be good!
  23. return path.resolve(process.env.MM_CONFIG_FILE || `${rootPath}/config/config.js`);
  24. }
  25. /**
  26. * Checks the config file using eslint.
  27. */
  28. function checkConfigFile() {
  29. const configFileName = getConfigFile();
  30. // Check if file is present
  31. if (fs.existsSync(configFileName) === false) {
  32. Log.error(Utils.colors.error("File not found: "), configFileName);
  33. throw new Error("No config file present!");
  34. }
  35. // Check permission
  36. try {
  37. fs.accessSync(configFileName, fs.F_OK);
  38. } catch (e) {
  39. Log.error(Utils.colors.error(e));
  40. throw new Error("No permission to access config file!");
  41. }
  42. // Validate syntax of the configuration file.
  43. Log.info(Utils.colors.info("Checking file... "), configFileName);
  44. // I'm not sure if all ever is utf-8
  45. const configFile = fs.readFileSync(configFileName, "utf-8");
  46. // Explicitly tell linter that he might encounter es6 syntax ("let config = {...}")
  47. const errors = linter.verify(configFile, {
  48. env: {
  49. es6: true
  50. }
  51. });
  52. if (errors.length === 0) {
  53. Log.info(Utils.colors.pass("Your configuration file doesn't contain syntax errors :)"));
  54. } else {
  55. Log.error(Utils.colors.error("Your configuration file contains syntax errors :("));
  56. for (const error of errors) {
  57. Log.error(`Line ${error.line} column ${error.column}: ${error.message}`);
  58. }
  59. }
  60. }
  61. checkConfigFile();