storage.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.userInfo().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. * getStorageDir
  39. */
  40. getStorageDir() {
  41. return storageDir;
  42. }
  43. }
  44. module.exports = StorageService;