-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.js
More file actions
151 lines (133 loc) · 3.81 KB
/
settings.js
File metadata and controls
151 lines (133 loc) · 3.81 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
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
const os = require('os');
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const log = require('./log');
const Settings = class Settings {
constructor () {
this.settings = null;
this.passPath = path.join(os.homedir(), '.pass');
this.passSettingsPath = path.join(this.passPath, 'settings.json');
this.defaultStores = path.join(this.passPath, 'stores');
}
/**
* loads the current settings if any
* @function
*/
load () {
return new Promise((resolve, reject) => {
// check if the Pass settings are present
if (!fs.existsSync(this.passPath)) {
try {
fs.mkdirSync(this.passPath);
fs.writeFileSync(this.passSettingsPath, '{}');
} catch (error) {
reject(error);
}
}
// check if the default stores folder exists
if (!fs.existsSync(this.defaultStores)) {
try {
fs.mkdirSync(this.defaultStores);
} catch (error) {
reject(error);
}
}
try {
this.settings = require(this.passSettingsPath);
resolve(this.settings);
} catch (error) {
reject(error);
}
});
}
/**
* sets a setting
* @param {string} key
* @param {*} value
* @function
* @returns {Settings}
*/
set (key, value) {
this.settings[key] = value;
this.save();
return this;
}
/**
* returns a setings value
* @param {string} key to return
* @returns {*} returned value
*/
get (key, defaultValue) {
if (typeof this.settings[key] !== 'undefined') return this.settings[key];
return defaultValue;
}
remove (key) {
if (typeof this.settings[key] !== 'undefined') {
delete this.settings[key];
this.save();
}
return this;
}
/**
* append a value to a Array type key
* @param {string} key
* @param {*} value
* @function
* @returns Settings
*/
append (key, value) {
let list = this.get(key);
if (typeof list === 'undefined') list = [];
if (list.constructor === Array) {
list.push(value);
this.set(key, list);
} else {
log.error(`Error while appending settings value in key '${key}'! The selected key is not of type 'Array'`);
log.nl();
}
return this;
}
/**
* check if the settings are loaded
* @function
* @returns {boolean} returns true or false if the settings exist and loaded
*/
loaded () {
return this.loaded;
}
/**
* ask user for the master password
* @function
*/
askPassword () {
return new Promise((resolve, reject) => {
inquirer.prompt([{
type: 'password',
name: 'password',
message: 'Enter master password'
}]).then(answers => {
this.masterPassword = answers.password;
}).catch(error => {
log.error('There was a problem while reading the inputed Master Password', error);
log.nl();
reject();
});
});
}
/**
* save the settings
* @function
* @returns {Settings}
*/
save () {
try {
fs.writeFileSync(this.passSettingsPath, JSON.stringify(this.settings, undefined, 4));
} catch (error) {
log.error('There was a problem while saving the settings', error);
log.nl();
}
return this;
}
}
module.exports = Settings;