settings.js 1.3 KB

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