storage.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. const path = require('path');
  3. const lowdb = require('lowdb');
  4. const FileSync = require('lowdb/adapters/FileSync');
  5. const fs = require('fs');
  6. const getPort = require('get-port');
  7. const utils = require('../../app/utils/utils');
  8. const storageKey = require('../../app/const/storageKey');
  9. const os = require('os');
  10. const pkg = require('../../package.json');
  11. const storageDb = 'db.json';
  12. exports.setup = function () {
  13. const storageDir = this.getStorageDir();
  14. if (!fs.existsSync(storageDir)) {
  15. utils.mkdir(storageDir);
  16. utils.chmodPath(storageDir, '777');
  17. }
  18. const file = storageDir + storageDb;
  19. const adapter = new FileSync(file);
  20. const db = lowdb(adapter);
  21. const eggConfigKey = storageKey.EGG_CONFIG;
  22. if (!db.has(eggConfigKey).value()) {
  23. db.set(eggConfigKey, {}).write();
  24. }
  25. return true;
  26. };
  27. exports.instance = function (file = null) {
  28. if (!file) {
  29. const storageDir = this.getStorageDir();
  30. file = path.normalize(storageDir + storageDb);
  31. }
  32. const isExist = fs.existsSync(file);
  33. if (!isExist) {
  34. return null;
  35. }
  36. const adapter = new FileSync(file);
  37. const db = lowdb(adapter);
  38. return db;
  39. };
  40. exports.getEggConfig = function () {
  41. const key = storageKey.EGG_CONFIG;
  42. const res = this.instance()
  43. .get(key)
  44. .value();
  45. return res;
  46. };
  47. exports.setDynamicPort = async function () {
  48. // const eggConfig = config.get('egg');
  49. // console.log('setDynamicPort eggConfig:', eggConfig);
  50. // const dynamicPort = await getPort({port: eggConfig.port})
  51. const dynamicPort = await getPort();
  52. const key = storageKey.EGG_CONFIG + '.port';
  53. const res = this.instance()
  54. .set(key, dynamicPort)
  55. .write();
  56. return res;
  57. };
  58. exports.setIpcDynamicPort = async function () {
  59. const key = storageKey.ELECTRON_IPC + '.port';
  60. const dynamicPort = await getPort();
  61. this.instance()
  62. .set(key, dynamicPort)
  63. .write();
  64. return dynamicPort;
  65. };
  66. exports.getStorageDir = function () {
  67. const userHomeDir = os.userInfo().homedir;
  68. const storageDir = path.normalize(userHomeDir + '/' + pkg.name + '/');
  69. return storageDir;
  70. }
  71. exports.getPreferences = function () {
  72. const key = storageKey.PREFERENCES;
  73. if (!this.instance().has(key).value()) {
  74. this.instance().set(key, {}).write();
  75. }
  76. const res = this.instance()
  77. .get(key)
  78. .value();
  79. return res;
  80. };
  81. exports.setShortcuts = function (data) {
  82. const key = storageKey.PREFERENCES + '.shortcuts';
  83. const res = this.instance()
  84. .set(key, data)
  85. .write();
  86. return res;
  87. };
  88. exports = module.exports;