[refactor]: Cleaned up error handling on File.loadSync and File.load

[refactor]: Using path module to determine if file exists instead of throwing error
[api]: File.load and File.loadSync will now automatically create the requested JSON file path if no file is found.
This commit is contained in:
Marak Squires 2011-06-23 18:06:26 -07:00
parent d8627a9475
commit c43685160d

View file

@ -6,6 +6,7 @@
*/ */
var fs = require('fs'), var fs = require('fs'),
path = require('path'),
util = require('util'), util = require('util'),
Memory = require('./memory').Memory; Memory = require('./memory').Memory;
@ -60,27 +61,15 @@ File.prototype.save = function (value, callback) {
// Saves the current configuration object to disk at `this.file` // Saves the current configuration object to disk at `this.file`
// using the format specified by `this.format` synchronously. // using the format specified by `this.format` synchronously.
// //
File.prototype.saveSync = function (value, callback) { File.prototype.saveSync = function (value) {
if (!callback) {
callback = value;
value = null;
}
var err;
try { try {
fs.writeFileSync(this.file, this.format.stringify(this.store)); fs.writeFileSync(this.file, this.format.stringify(this.store));
} }
catch (ex) { catch (ex) {
err = ex; throw(ex);
} }
if (callback) {
return callback(err);
}
if (err) {
throw err;
}
}; };
// //
@ -90,21 +79,37 @@ File.prototype.saveSync = function (value, callback) {
// //
File.prototype.load = function (callback) { File.prototype.load = function (callback) {
var self = this; var self = this;
fs.readFile(this.file, function (err, data) {
path.exists(self.file, function(exists){
if (!exists) {
// If the path we are attempting to load doesn't exist, create it
self.save({}, function(err){
self.store = {};
return callback(err, self.store);
});
}
else {
// Else, the path exists, read it from disk
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) {
self.store = {}; return callback(new Error("Error parsing your JSON configuration file."));
return callback(ex);
} }
callback(null, self.store); callback(null, self.store);
}); });
}
});
}; };
// //
@ -112,24 +117,28 @@ File.prototype.load = function (callback) {
// #### @callback {function} **Optional** Continuation to respond to when complete. // #### @callback {function} **Optional** Continuation to respond to when complete.
// Attempts to load the data stored in `this.file` synchronously and responds appropriately. // Attempts to load the data stored in `this.file` synchronously and responds appropriately.
// //
File.prototype.loadSync = function (callback) { File.prototype.loadSync = function () {
var err, data; var data, self = this;
if (!path.existsSync(self.file)) {
// If the path we are attempting to load doesn't exist, create it
self.saveSync({});
self.store = {};
data = {};
}
else {
// 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);
} }
catch (ex) { catch (ex) {
err = ex; throw new Error("Error parsing your JSON configuration file.")
} }
if (callback) {
return callback(err, data);
}
if (err) {
err.message = "Error parsing your JSON configuration file."
throw err;
} }
return data; return data;