forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
57 lines (42 loc) · 1.18 KB
/
store.js
File metadata and controls
57 lines (42 loc) · 1.18 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
var fs = require('fs'),
path = require('path');
var IonicStore = function(fileName) {
this.data = {};
if(!fileName) return this;
this.fileName = fileName;
if(fileName.indexOf('.') < 0) {
this.fileName += '.data';
}
this.homeDir = process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH;
this.privateDir = path.join(this.homeDir, '.ionic');
if(!fs.existsSync(this.privateDir)) {
fs.mkdirSync(this.privateDir);
}
this.filePath = path.join(this.privateDir, this.fileName);
try {
this.data = JSON.parse(fs.readFileSync(this.filePath));
} catch(e) {}
return this;
};
IonicStore.prototype.get = function(k) {
if(k) {
return this.data[k];
}
return this.data;
};
IonicStore.prototype.set = function(k, v) {
this.data[k] = v;
};
IonicStore.prototype.remove = function(k) {
delete this.data[k];
};
IonicStore.prototype.save = function() {
try {
var dataStoredAsString = JSON.stringify(this.data, null, 2);
fs.writeFileSync(this.filePath, dataStoredAsString);
this.data = JSON.parse(dataStoredAsString);
} catch(e) {
console.error('Unable to save ionic data:', this.filePath, e);
}
};
exports.IonicStore = IonicStore;