FileAsync.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  6. // Not using async/await on purpose to avoid adding regenerator-runtime
  7. // to lowdb dependencies
  8. var fs = require('graceful-fs');
  9. var pify = require('pify');
  10. var steno = require('steno');
  11. var Base = require('./Base');
  12. var readFile = pify(fs.readFile);
  13. var writeFile = pify(steno.writeFile);
  14. var FileAsync = function (_Base) {
  15. _inherits(FileAsync, _Base);
  16. function FileAsync() {
  17. _classCallCheck(this, FileAsync);
  18. return _possibleConstructorReturn(this, (FileAsync.__proto__ || Object.getPrototypeOf(FileAsync)).apply(this, arguments));
  19. }
  20. _createClass(FileAsync, [{
  21. key: 'read',
  22. value: function read() {
  23. var _this2 = this;
  24. // fs.exists is deprecated but not fs.existsSync
  25. if (fs.existsSync(this.source)) {
  26. // Read database
  27. return readFile(this.source, 'utf-8').then(function (data) {
  28. // Handle blank file
  29. var trimmed = data.trim();
  30. return trimmed ? _this2.deserialize(trimmed) : _this2.defaultValue;
  31. }).catch(function (e) {
  32. if (e instanceof SyntaxError) {
  33. e.message = `Malformed JSON in file: ${_this2.source}\n${e.message}`;
  34. }
  35. throw e;
  36. });
  37. } else {
  38. // Initialize
  39. return writeFile(this.source, this.serialize(this.defaultValue)).then(function () {
  40. return _this2.defaultValue;
  41. });
  42. }
  43. }
  44. }, {
  45. key: 'write',
  46. value: function write(data) {
  47. return writeFile(this.source, this.serialize(data));
  48. }
  49. }]);
  50. return FileAsync;
  51. }(Base);
  52. module.exports = FileAsync;