Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit ff35819

Browse files
authored
Info speed up (#130)
* feat(): rewrite info to be async. * fix(): rewrite get info to make it async.
1 parent b49fda4 commit ff35819

File tree

6 files changed

+185
-263
lines changed

6 files changed

+185
-263
lines changed

lib/config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var fs = require('fs');
22
var path = require('path');
3+
var Utils = require('./utils');
4+
var Q = require('q');
35

46
var home = process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH;
57

@@ -14,6 +16,16 @@ module.exports = {
1416
}
1517
return this;
1618
},
19+
promiseLoad: function() {
20+
var self = this;
21+
self.file = self.CONFIG_FILE;
22+
var readFile = Utils.promisify(fs.readFile);
23+
24+
return readFile(path.join(home, self.file)).then(function(jsonContents) {
25+
self.data = jsonContents.toString();
26+
return self;
27+
});
28+
},
1729
save: function() {
1830
if (!this.data) {
1931
return;
@@ -30,6 +42,24 @@ module.exports = {
3042
console.error('Unable to save settings file: ' + e);
3143
}
3244
},
45+
promiseSave: function() {
46+
if (!this.data) {
47+
return Q.resolve();
48+
}
49+
var dirPath = path.join(home, path.dirname(this.CONFIG_FILE));
50+
var filepath = path.join(dirPath, this.CONFIG_FILE);
51+
var statFile = Utils.promisify(fs.stat);
52+
var mkDir = Utils.promisify(fs.mkdir);
53+
var jsonString = JSON.stringify(this.data, null, 2);
54+
55+
return statFile(dirPath).then(function(stat) {
56+
return Utils.writeJsonContents(filepath, jsonString);
57+
}, function() {
58+
return mkDir(dirPath).then(function() {
59+
return Utils.writeJsonContents(filepath, jsonString);
60+
});
61+
});
62+
},
3363
get: function(k) {
3464
return this.data[k];
3565
},

0 commit comments

Comments
 (0)