settings.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. var path = require('path')
  2. , util = require('util')
  3. , fs = require('fs')
  4. , utl = require('./utl')
  5. , home = process.env.HOME
  6. , settings;
  7. function getSettings (home_) {
  8. if (settings) return settings;
  9. try {
  10. settingsJson = fs.readFileSync(path.join(home_ || home, '.cardinalrc'), 'utf-8');
  11. } catch (_) {
  12. // no .cardinalrc found - not a problem
  13. return undefined;
  14. }
  15. try {
  16. return JSON.parse(settingsJson);
  17. } catch (e) {
  18. // Have a .cardinalrc, but something about it is wrong - warn the user
  19. // Coudn't parse the contained JSON
  20. console.error(e);
  21. return undefined;
  22. }
  23. }
  24. // home_ mainly to be used during tests
  25. // Resolves the preferred theme from the .cardinalrc found in the HOME directory
  26. // If it couldn't be resolved, undefined is returned
  27. function resolveTheme (home_) {
  28. var themePath
  29. , settings = getSettings(home_);
  30. if (!settings || !settings.theme) return undefined;
  31. try {
  32. // allow specifying just the name of a built-in theme or a full path to a custom theme
  33. themePath = utl.isPath(settings.theme) ? settings.theme : path.join(__dirname, 'themes', settings.theme);
  34. return require(themePath);
  35. } catch (e) {
  36. // Specified theme path is invalid
  37. console.error(e);
  38. return undefined;
  39. }
  40. }
  41. module.exports = {
  42. resolveTheme: resolveTheme
  43. , getSettings: getSettings
  44. };