storage.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 pkg = require('../../package.json');
  11. const storageDir = path.normalize(os.userInfo().homedir + '/' + pkg.name + '/');
  12. const storageDb = 'db.json';
  13. class StorageService extends BaseService {
  14. /*
  15. * instance
  16. */
  17. instance(file = null) {
  18. if (!file) {
  19. file = path.normalize(storageDir + storageDb);
  20. }
  21. const isExist = fs.existsSync(file);
  22. if (!isExist) {
  23. return null;
  24. }
  25. const adapter = new FileSync(file);
  26. const db = lowdb(adapter);
  27. return db;
  28. }
  29. /*
  30. * getElectronIPCPort
  31. */
  32. getElectronIPCPort() {
  33. const key = storageKey.ELECTRON_IPC + '.port';
  34. const port = this.instance()
  35. .get(key)
  36. .value();
  37. return port;
  38. }
  39. /*
  40. * getStorageDir
  41. */
  42. getStorageDir() {
  43. return storageDir;
  44. }
  45. }
  46. module.exports = StorageService;