storage.js 3.1 KB

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