storage.js 967 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. const BaseService = require('./base');
  3. const path = require('path');
  4. const _ = require('lodash');
  5. const lowdb = require('lowdb');
  6. const FileSync = require('lowdb/adapters/FileSync');
  7. const storageKey = require('../const/storageKey');
  8. const fs = require('fs');
  9. const os = require('os');
  10. const storageDir = path.normalize(os.homedir() + '/electron-egg-storage/');
  11. class StorageService extends BaseService {
  12. /*
  13. * instance
  14. */
  15. instance(file = null) {
  16. if (!file) {
  17. file = path.normalize(storageDir +'db.json');
  18. }
  19. const isExist = fs.existsSync(file);
  20. if (!isExist) {
  21. return null;
  22. }
  23. const adapter = new FileSync(file);
  24. const db = lowdb(adapter);
  25. return db;
  26. }
  27. /*
  28. * getElectronIPCPort
  29. */
  30. getElectronIPCPort() {
  31. const key = storageKey.ELECTRON_IPC + '.port';
  32. const port = this.instance()
  33. .get(key)
  34. .value();
  35. return port;
  36. }
  37. }
  38. module.exports = StorageService;