Skip to content

Commit

Permalink
Merge pull request #31 from KELiON/feature/fix-broken-config-files
Browse files Browse the repository at this point in the history
Use default config when config file is corrupted
  • Loading branch information
KELiON authored Jan 24, 2017
2 parents 34b74ad + 18c4f72 commit fa2c351
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ const defaultSettings = memoize(() => {
}
})

const readConfig = () => {
try {
return JSON.parse(fs.readFileSync(CONFIG_FILE).toString())
} catch (err) {
return defaultSettings()
}
}

/**
* Get a value from global configuration
* @param {String} key
Expand All @@ -36,7 +44,7 @@ const get = (key) => {
config = defaultSettings()
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config))
} else {
config = JSON.parse(fs.readFileSync(CONFIG_FILE).toString())
config = readConfig()
}
return config[key]
}
Expand All @@ -53,7 +61,7 @@ const set = (key, value) => {
// If after app update some keys were added to config
// we use default values for that keys
...defaultSettings(),
...JSON.parse(fs.readFileSync(CONFIG_FILE).toString())
...readConfig()
}
config[key] = value
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config))
Expand Down

0 comments on commit fa2c351

Please sign in to comment.