Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 909B

123456789101112131415161718192021222324252627282930313233343536373839
  1. var fs = require( 'graceful-fs' );
  2. var write = require( 'write' );
  3. var circularJson = require( 'circular-json' );
  4. module.exports = {
  5. tryParse: function ( filePath, defaultValue ) {
  6. var result;
  7. try {
  8. result = this.readJSON( filePath );
  9. } catch (ex) {
  10. result = defaultValue;
  11. }
  12. return result;
  13. },
  14. /**
  15. * Read json file synchronously using circular-json
  16. *
  17. * @method readJSON
  18. * @param {String} filePath Json filepath
  19. * @returns {*} parse result
  20. */
  21. readJSON: function ( filePath ) {
  22. return circularJson.parse( fs.readFileSync( filePath ).toString() );
  23. },
  24. /**
  25. * Write json file synchronously using circular-json
  26. *
  27. * @method writeJSON
  28. * @param {String} filePath Json filepath
  29. * @param {*} data Object to serialize
  30. */
  31. writeJSON: function ( filePath, data ) {
  32. write.sync( filePath, circularJson.stringify( data ) );
  33. }
  34. };