Skip to content
This repository has been archived by the owner on Feb 14, 2018. It is now read-only.

Commit

Permalink
Create utility function to parsing json from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
pquerna committed Sep 27, 2010
1 parent db5cf23 commit f8539a2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
34 changes: 9 additions & 25 deletions lib/manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var async = require('extern/async');
var sprintf = require('extern/sprintf').sprintf;

var misc = require('util/misc');
var ufs = require('util/fs');
var constants = require('manifest/constants');
var validators = require('manifest/validators');

Expand Down Expand Up @@ -166,42 +167,25 @@ exports.validate_manifest = function(manifest_path, callback) {
exports.get_manifest_data_as_object = function(manifest_path, merge_with_defaults, callback) {
path.exists(manifest_path, function(exists) {
if (!exists) {
return callback(new Error('Invalid path to the manifest file'), null);
return callback(new Error('Invalid path to the manifest file: ' + manifest_path), null);
}

var return_manifest_object = function() {
ufs.json_file(manifest_path, function(err, obj) {
var default_values, manifest_object;
var data = data_buffer.join('');

try {
manifest_object = JSON.parse(data);
}
catch (error) {
return callback(new Error('Manifest file contains invalid JSON'), null);
if (err) {
callback(err);
return;
}

if (merge_with_defaults) {
// Merge the manifest file values with the default values for this application type
default_values = constants.DEFAULT_VALUES[manifest_object.type];
manifest_object = misc.merge(default_values, manifest_object);
default_values = constants.DEFAULT_VALUES[obj.type];
obj = misc.merge(default_values, obj);
}

callback(null, manifest_object);
};

var read_stream = fs.createReadStream(manifest_path);
var data_buffer = [];

read_stream.on('data', function(chunk) {
data_buffer.push(chunk);
callback(null, obj);
});

read_stream.on('error', function(error) {
read_stream.removeListener('end', return_manifest_object);
callback(error, null);
});

read_stream.on('end', return_manifest_object);
});
};

38 changes: 38 additions & 0 deletions lib/util/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,41 @@ exports.ensure_directory = function(p, callback) {
}
});
};

/**
* Reads a JSON format file off of disk.
*
* @param {String} path The path to read from.
* @param {Function} callback The callback which takes a possible error, second parameter is the json object.
*/
exports.json_file = function(path, callback)
{
var read_stream = fs.createReadStream(path);
var data_buffer = [];

function end_callback()
{
var data = data_buffer.join('');
var obj = null;
try {
obj = JSON.parse(data);
}
catch (err) {
callback(new Error('File ' + path + ' contains invalid JSON: ' + err), null);
return;
}
callback(null, obj);
return;
}

read_stream.on('data', function(chunk) {
data_buffer.push(chunk);
});

read_stream.on('error', function(error) {
read_stream.removeListener('end', end_callback);
callback(error, null);
});

read_stream.on('end', end_callback);
};

0 comments on commit f8539a2

Please sign in to comment.