Sha256: 6a6d617165171e8b89a55510fa361c692fd9857ef118bb9f00a5e37878c02bcb

Contents?: true

Size: 1.26 KB

Versions: 17

Compression:

Stored size: 1.26 KB

Contents

var fs = require('fs')

var me = module.exports

me.spaces = 2

me.readFile = function(file, options, callback) {
  if (callback == undefined) {
    callback = options
    options = null
  }

  fs.readFile(file, options, function(err, data) {
    if (err) return callback(err, null)

    var obj = null
    try {
      obj = JSON.parse(data)
    } catch (err2) {
      return callback(err2, null)
    }

    callback(null, obj)
  })
}

me.readFileSync = function(file, options) {
  var noThrow = options && !options.throws

  if (!noThrow) //i.e. throw on invalid JSON
    return JSON.parse(fs.readFileSync(file, options))
  else 
    try {
      return JSON.parse(fs.readFileSync(file, options))
    } catch (err) {
      return null
    }
}

me.writeFile = function(file, obj, options, callback) {
  if (callback == undefined) {
    callback = options
    options = null
  }

  var str = ''
  try {
    str = JSON.stringify(obj, null, me.spaces) + '\n';
  } catch (err) {
    if (callback) return callback(err, null)
  }

  fs.writeFile(file, str, options, callback)
}

me.writeFileSync = function(file, obj, options) {
  var str = JSON.stringify(obj, null, me.spaces) + '\n';
  return fs.writeFileSync(file, str, options) //not sure if fs.writeFileSync returns anything, but just in case
}

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
hooch-0.4.2 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
hooch-0.4.1 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
hooch-0.4.0 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
hooch-0.3.0 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
hooch-0.2.1 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
hooch-0.2.0 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
hooch-0.1.0 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
hooch-0.0.8 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
hooch-0.0.7 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
hooch-0.0.6 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
entangled-0.0.16 spec/dummy/public/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
entangled-0.0.15 spec/dummy/public/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
entangled-0.0.14 spec/dummy/public/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
entangled-0.0.13 spec/dummy/public/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
entangled-0.0.12 spec/dummy/public/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
entangled-0.0.11 spec/dummy/public/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js
entangled-0.0.10 spec/dummy/public/node_modules/phantomjs/node_modules/fs-extra/node_modules/jsonfile/lib/jsonfile.js