[minor] Small style updates to the File store

This commit is contained in:
indexzero 2011-06-24 03:31:26 -04:00
parent c43685160d
commit d8b5a80280

View file

@ -62,14 +62,12 @@ File.prototype.save = function (value, callback) {
// using the format specified by `this.format` synchronously. // using the format specified by `this.format` synchronously.
// //
File.prototype.saveSync = function (value) { File.prototype.saveSync = function (value) {
try { try {
fs.writeFileSync(this.file, this.format.stringify(this.store)); fs.writeFileSync(this.file, this.format.stringify(this.store));
} }
catch (ex) { catch (ex) {
throw(ex); throw(ex);
} }
}; };
// //
@ -80,36 +78,36 @@ File.prototype.saveSync = function (value) {
File.prototype.load = function (callback) { File.prototype.load = function (callback) {
var self = this; var self = this;
path.exists(self.file, function(exists){ path.exists(self.file, function (exists) {
if (!exists) { if (!exists) {
//
// If the path we are attempting to load doesn't exist, create it // If the path we are attempting to load doesn't exist, create it
self.save({}, function(err){ //
self.save({}, function (err) {
self.store = {}; self.store = {};
return callback(err, self.store); return callback(err, self.store);
}); });
} }
else { else {
//
// Else, the path exists, read it from disk // Else, the path exists, read it from disk
//
fs.readFile(self.file, function (err, data) { fs.readFile(self.file, function (err, data) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
try { try {
self.store = self.format.parse(data.toString()); self.store = self.format.parse(data.toString());
} }
catch (ex) { catch (ex) {
return callback(new Error("Error parsing your JSON configuration file.")); return callback(new Error("Error parsing your JSON configuration file."));
} }
callback(null, self.store); callback(null, self.store);
}); });
} }
}); });
}; };
// //
@ -121,16 +119,17 @@ File.prototype.loadSync = function () {
var data, self = this; var data, self = this;
if (!path.existsSync(self.file)) { if (!path.existsSync(self.file)) {
//
// If the path we are attempting to load doesn't exist, create it // If the path we are attempting to load doesn't exist, create it
//
self.saveSync({}); self.saveSync({});
self.store = {}; self.store = {};
data = {}; data = {};
} }
else { else {
//
// Else, the path exists, read it from disk // Else, the path exists, read it from disk
//
try { try {
data = fs.readFileSync(this.file, 'utf8'); data = fs.readFileSync(this.file, 'utf8');
this.store = this.format.parse(data); this.store = this.format.parse(data);
@ -138,7 +137,6 @@ File.prototype.loadSync = function () {
catch (ex) { catch (ex) {
throw new Error("Error parsing your JSON configuration file.") throw new Error("Error parsing your JSON configuration file.")
} }
} }
return data; return data;