-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathConfigurationService.js
More file actions
65 lines (57 loc) · 1.43 KB
/
ConfigurationService.js
File metadata and controls
65 lines (57 loc) · 1.43 KB
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
const fs = require('fs');
/**
* Default file path where credentials will be stored.
* @type {string}
*/
const configFilePath = __dirname + '/.awscredentials.json';
/**
* Encoding used while reading persistent storage.
* @type {string}
*/
const defaultEncoding = 'utf-8';
/**
* Initial Configuration.
* @type {{accessKey: string, secretKey: string, bucket: string, ACL: string, storageClass: string,
* encryption: boolean}}
*/
const configuration = {
accessKey: '',
secretKey: '',
bucket: '',
folder: '',
ACL: '',
storageClass: '',
encryption: false,
};
/**
* Merges old config with provided one. Saves changes to persistent storage.
* @param newConfig
*/
const updateConfig = (newConfig) => {
Object.assign(configuration, newConfig);
fs.writeFile(configFilePath, JSON.stringify(configuration), {}, (err) => {
if (err) throw new Error(err);
});
};
/**
* Loads configuration from persistent storage if it's possible.
*/
const loadConfig = () => new Promise((resolve, reject) => {
fs.readFile(configFilePath, defaultEncoding, (err, data) => {
if (err) {
return reject(err);
}
Object.assign(configuration, JSON.parse(data));
return resolve(configuration);
});
});
/**
* Returns value for given key from configuration directory. Fast, in-memory function (fs not used).
* @param key
*/
const getItem = (key) => configuration[key];
module.exports = {
getItem,
updateConfig,
loadConfig,
};