diff options
Diffstat (limited to 'node_modules/utile/lib/file.js')
-rw-r--r-- | node_modules/utile/lib/file.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/node_modules/utile/lib/file.js b/node_modules/utile/lib/file.js new file mode 100644 index 0000000..ee98758 --- /dev/null +++ b/node_modules/utile/lib/file.js @@ -0,0 +1,33 @@ +/* + * file.js: Simple utilities for working with the file system. + * + * (C) 2011, Charlie Robbins & the Contributors + * MIT LICENSE + * + */ + +var fs = require('fs'); + +exports.readJson = exports.readJSON = function (file, callback) { + if (typeof callback !== 'function') { + throw new Error('utile.file.readJson needs a callback'); + } + + fs.readFile(file, 'utf-8', function (err, data) { + if (err) { + return callback(err); + } + + try { + var json = JSON.parse(data); + callback(null, json); + } + catch (err) { + return callback(err); + } + }); +}; + +exports.readJsonSync = exports.readJSONSync = function (file) { + return JSON.parse(fs.readFileSync(file, 'utf-8')); +}; |