Skip to content

Commit 8384cb1

Browse files
Siilwynboennemann
authored andcommitted
feat: save config to standard directories
The config is read to and written from standard config directories using the "env-paths" module, rather than just stuffing it into the home directory. Old config files will automatically be migrated to the new location. BREAKING CHANGE: Config is no longer stored in `~/.greenkeeperrc` Closes #3, Closes #5
1 parent b16d09d commit 8384cb1

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

index.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ var fs = require('fs')
22
var path = require('path')
33

44
var _ = require('lodash')
5+
var envPaths = require('env-paths')
56
var home = require('os-homedir')
6-
7-
var configPath = path.join(home(), '.greenkeeperrc')
7+
var mkdirp = require('mkdirp')
88

99
var config
1010

11-
try {
12-
config = JSON.parse(fs.readFileSync(configPath))
13-
} catch (e) {
14-
config = {}
15-
}
16-
1711
exports.get = function (name) {
1812
return name
1913
? config[name]
@@ -41,5 +35,28 @@ exports.merge = function (newConfig) {
4135
}
4236

4337
exports._save = function () {
44-
fs.writeFileSync(configPath, JSON.stringify(config, null, 2))
38+
fs.writeFileSync(exports.getPath(), JSON.stringify(config, null, 2))
39+
}
40+
41+
exports.getPath = _.memoize(function () {
42+
var configDir = envPaths('greenkeeper', {suffix: ''}).config
43+
mkdirp.sync(configDir)
44+
return path.join(configDir, '.greenkeeperrc')
45+
})
46+
47+
var readConfig = _.flow(fs.readFileSync, JSON.parse)
48+
try {
49+
// If config exists at the old path use and migrate it
50+
var oldPath = path.join(home(), '.greenkeeperrc')
51+
config = readConfig(oldPath)
52+
exports._save()
53+
fs.unlinkSync(oldPath)
54+
} catch (e) {
55+
try {
56+
// Use XDG environment variable and fallback to OS default
57+
config = readConfig(exports.getPath())
58+
} catch (e) {
59+
// No prior config
60+
config = {}
61+
}
4562
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"url": "https://github.com/greenkeeperio/rc/issues"
77
},
88
"dependencies": {
9+
"env-paths": "^0.3.0",
910
"lodash": "^4.11.1",
11+
"mkdirp": "^0.5.1",
1012
"os-homedir": "^1.0.1"
1113
},
1214
"devDependencies": {

0 commit comments

Comments
 (0)