storage.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. const _ = require('lodash');
  13. /**
  14. * 安装模块
  15. */
  16. exports.setup = function () {
  17. console.log('[electron-lib-storage] [setup]');
  18. const storageDir = this.getStorageDir();
  19. if (!fs.existsSync(storageDir)) {
  20. utils.mkdir(storageDir);
  21. utils.chmodPath(storageDir, '777');
  22. }
  23. const file = storageDir + storageDb;
  24. const adapter = new FileSync(file);
  25. const db = lowdb(adapter);
  26. const eggConfigKey = storageKey.EGG_CONFIG;
  27. if (!db.has(eggConfigKey).value()) {
  28. db.set(eggConfigKey, {}).write();
  29. }
  30. return true;
  31. };
  32. exports.instance = function (file = null) {
  33. if (!file) {
  34. const storageDir = this.getStorageDir();
  35. file = path.normalize(storageDir + storageDb);
  36. }
  37. const isExist = fs.existsSync(file);
  38. if (!isExist) {
  39. return null;
  40. }
  41. const adapter = new FileSync(file);
  42. const db = lowdb(adapter);
  43. return db;
  44. };
  45. exports.getEggConfig = function () {
  46. const key = storageKey.EGG_CONFIG;
  47. const res = this.instance()
  48. .get(key)
  49. .value();
  50. return res;
  51. };
  52. exports.setDynamicPort = async function () {
  53. // const eggConfig = config.get('egg');
  54. // console.log('setDynamicPort eggConfig:', eggConfig);
  55. // const dynamicPort = await getPort({port: eggConfig.port})
  56. const dynamicPort = await getPort();
  57. const key = storageKey.EGG_CONFIG + '.port';
  58. const res = this.instance()
  59. .set(key, dynamicPort)
  60. .write();
  61. return res;
  62. };
  63. exports.setIpcDynamicPort = async function () {
  64. const key = storageKey.ELECTRON_IPC + '.port';
  65. const dynamicPort = await getPort();
  66. this.instance()
  67. .set(key, dynamicPort)
  68. .write();
  69. return dynamicPort;
  70. };
  71. exports.getStorageDir = function () {
  72. const userHomeDir = os.userInfo().homedir;
  73. const storageDir = path.normalize(userHomeDir + '/' + pkg.name + '/');
  74. return storageDir;
  75. }
  76. exports.iniPreferences = function () {
  77. const key = storageKey.PREFERENCES;
  78. if (!this.instance().has(key).value()) {
  79. this.instance().set(key, {}).write();
  80. }
  81. const res = this.instance()
  82. .get(key)
  83. .value();
  84. return res;
  85. };
  86. exports.setShortcuts = function (data) {
  87. const key = storageKey.PREFERENCES + '.shortcuts';
  88. if (!this.instance().has(key).value()) {
  89. this.instance().set(key, []).write();
  90. }
  91. const item = this.instance().get(key).find({id: data.id}).value();
  92. if (_.isEmpty(item)) {
  93. this.instance()
  94. .get(key)
  95. .push(data)
  96. .write();
  97. } else {
  98. this.instance()
  99. .get(key)
  100. .find({id: data.id})
  101. .assign(data)
  102. .write();
  103. }
  104. return true;
  105. };
  106. exports = module.exports;