forked from sindresorhus/configstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (109 loc) · 3.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
var path = require('path');
var fs = require('graceful-fs');
var osenv = require('osenv');
var assign = require('object-assign');
var mkdirp = require('mkdirp');
var uuid = require('uuid');
var xdgBasedir = require('xdg-basedir');
var osTmpdir = require('os-tmpdir');
var writeFileAtomic = require('write-file-atomic');
var dotProp = require('dot-prop');
var user = (osenv.user() || uuid.v4()).replace(/\\/g, '');
var configDir = xdgBasedir.config || path.join(osTmpdir(), user, '.config');
var permissionError = 'You don\'t have access to this file.';
var defaultPathMode = parseInt('0700', 8);
var writeFileOptions = {mode: parseInt('0600', 8)};
function Configstore(id, defaults, opts) {
opts = opts || {};
var pathPrefix = opts.globalConfigPath ?
path.join(id, 'config.json') :
path.join('configstore', id + '.json');
this.path = opts.configFile || path.join(configDir, pathPrefix);
this.all = assign({}, defaults || {}, this.all || {});
}
Configstore.prototype = Object.create(Object.prototype, {
all: {
get: function () {
try {
return JSON.parse(fs.readFileSync(this.path, 'utf8'));
} catch (err) {
// create dir if it doesn't exist
if (err.code === 'ENOENT') {
mkdirp.sync(path.dirname(this.path), defaultPathMode);
return {};
}
// improve the message of permission errors
if (err.code === 'EACCES') {
err.message = err.message + '\n' + permissionError + '\n';
}
// empty the file if it encounters invalid JSON
if (err.name === 'SyntaxError') {
writeFileAtomic.sync(this.path, '', writeFileOptions);
return {};
}
throw err;
}
},
set: function (val) {
try {
// make sure the folder exists as it
// could have been deleted in the meantime
mkdirp.sync(path.dirname(this.path), defaultPathMode);
writeFileAtomic.sync(this.path, JSON.stringify(val, null, '\t'), writeFileOptions);
} catch (err) {
// improve the message of permission errors
if (err.code === 'EACCES') {
err.message = err.message + '\n' + permissionError + '\n';
}
throw err;
}
}
},
size: {
get: function () {
return Object.keys(this.all || {}).length;
}
}
});
Configstore.prototype.get = function (key) {
return dotProp.get(this.all, key);
};
Configstore.prototype.getPlain = function (key) {
return this.all[key];
};
Configstore.prototype.set = function (key, val) {
var config = this.all;
if (arguments.length === 1) {
Object.keys(key).forEach(function (k) {
dotProp.set(config, k, key[k]);
});
} else {
dotProp.set(config, key, val);
}
this.all = config;
return this;
};
Configstore.prototype.setPlain = function (key, val) {
var config = this.all;
if (arguments.length === 1) {
Object.keys(key).forEach(function (k) {
config[k] = key[k];
});
} else {
config[key] = val;
}
this.all = config;
return this;
};
Configstore.prototype.del = function (key) {
var config = this.all;
dotProp.delete(config, key);
this.all = config;
return this;
};
Configstore.prototype.clear = function () {
this.all = {};
return this;
};
module.exports = Configstore;