UNPKG

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