forked from sindresorhus/conf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (134 loc) · 4.23 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const assert = require('assert');
const EventEmitter = require('events');
const dotProp = require('dot-prop');
const makeDir = require('make-dir');
const pkgUp = require('pkg-up');
const envPaths = require('env-paths');
const writeFileAtomic = require('write-file-atomic');
const plainObject = () => Object.create(null);
// Prevent caching of this module so module.parent is always accurate
delete require.cache[__filename];
const parentDir = path.dirname((module.parent && module.parent.filename) || '.');
class Conf {
constructor(options) {
const pkgPath = pkgUp.sync(parentDir);
options = Object.assign({
// Can't use `require` because of Webpack being annoying:
// https://github.com/webpack/webpack/issues/196
projectName: pkgPath && JSON.parse(fs.readFileSync(pkgPath, 'utf8')).name
}, options);
if (!options.projectName && !options.cwd) {
throw new Error('Project name could not be inferred. Please specify the `projectName` option.');
}
options = Object.assign({
configName: 'config'
}, options);
if (!options.cwd) {
options.cwd = envPaths(options.projectName).config;
}
this.events = new EventEmitter();
this.encryptionKey = options.encryptionKey;
this.path = path.resolve(options.cwd, `${options.configName}.json`);
this.store = Object.assign(plainObject(), options.defaults, this.store);
}
get(key, defaultValue) {
return dotProp.get(this.store, key, defaultValue);
}
set(key, value) {
if (typeof key !== 'string' && typeof key !== 'object') {
throw new TypeError(`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`);
}
if (typeof key !== 'object' && value === undefined) {
throw new TypeError('Use `delete()` to clear values');
}
const {store} = this;
if (typeof key === 'object') {
for (const k of Object.keys(key)) {
dotProp.set(store, k, key[k]);
}
} else {
dotProp.set(store, key, value);
}
this.store = store;
}
has(key) {
return dotProp.has(this.store, key);
}
delete(key) {
const {store} = this;
dotProp.delete(store, key);
this.store = store;
}
clear() {
this.store = plainObject();
}
onDidChange(key, callback) {
if (typeof key !== 'string') {
throw new TypeError(`Expected \`key\` to be of type \`string\`, got ${typeof key}`);
}
if (typeof callback !== 'function') {
throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`);
}
let currentValue = this.get(key);
const onChange = () => {
const oldValue = currentValue;
const newValue = this.get(key);
try {
// TODO: Use `util.isDeepStrictEqual` when targeting Node.js 10
assert.deepEqual(newValue, oldValue);
} catch (_) {
currentValue = newValue;
callback.call(this, newValue, oldValue);
}
};
this.events.on('change', onChange);
return () => this.events.removeListener('change', onChange);
}
get size() {
return Object.keys(this.store).length;
}
get store() {
try {
let data = fs.readFileSync(this.path, this.encryptionKey ? null : 'utf8');
if (this.encryptionKey) {
try {
const decipher = crypto.createDecipher('aes-256-cbc', this.encryptionKey);
data = Buffer.concat([decipher.update(data), decipher.final()]);
} catch (_) {}
}
return Object.assign(plainObject(), JSON.parse(data));
} catch (error) {
if (error.code === 'ENOENT') {
makeDir.sync(path.dirname(this.path));
return plainObject();
}
if (error.name === 'SyntaxError') {
return plainObject();
}
throw error;
}
}
set store(value) {
// Ensure the directory exists as it could have been deleted in the meantime
makeDir.sync(path.dirname(this.path));
let data = JSON.stringify(value, null, '\t');
if (this.encryptionKey) {
const cipher = crypto.createCipher('aes-256-cbc', this.encryptionKey);
data = Buffer.concat([cipher.update(Buffer.from(data)), cipher.final()]);
}
writeFileAtomic.sync(this.path, data);
this.events.emit('change');
}
// TODO: Use `Object.entries()` when targeting Node.js 8
* [Symbol.iterator]() {
const {store} = this;
for (const key of Object.keys(store)) {
yield [key, store[key]];
}
}
}
module.exports = Conf;