config.js 1.7 KB
Newer Older
1 2 3 4
import path from "path";
import fs from "fs";
import { getLocalDataFile } from "./utils";
import os from "os";
T
tcsnzh 已提交
5
import { app } from "electron";
M
muwoo 已提交
6

7
const configPath = path.join(getLocalDataFile(), "./rubick-config.json");
M
muwoo 已提交
8

9 10 11 12
const defaultConfigForAnyPlatform = {
  version: 3,
  perf: {
    shortCut: {
璃白.'s avatar
璃白. 已提交
13
      showAndHidden: "Option+C",
14
      separate: "Ctrl+D",
璃白.'s avatar
璃白. 已提交
15
      quit: "Shift+Escape",
M
muwoo 已提交
16
    },
17 18 19
    common: {
      start: true,
      space: true,
T
tcsnzh 已提交
20
      // 是否失焦隐藏。默认在dev环境不隐藏,在打包后隐藏。
郭维嘉 已提交
21
      hideOnBlur: app.isPackaged
22
    },
23
    local: {
郭维嘉 已提交
24 25
      search: true
    }
26 27 28 29
  },
  superPanel: {
    baiduAPI: {
      key: "",
郭维嘉 已提交
30
      appid: ""
M
muwoo 已提交
31
    },
郭维嘉 已提交
32
    mouseDownTime: 500
33
  },
郭维嘉 已提交
34 35
  userInfo: '',
  global: []
36 37 38 39
};

let defaultConfig = {
  Darwin: {
郭维嘉 已提交
40
    ...defaultConfigForAnyPlatform
41 42
  },
  Windows_NT: {
郭维嘉 已提交
43
    ...defaultConfigForAnyPlatform
S
sovlookup 已提交
44 45
  },
  Linux: {
郭维嘉 已提交
46 47
    ...defaultConfigForAnyPlatform
  }
48
};
M
muwoo 已提交
49 50 51
global.opConfig = {
  config: null,
  get() {
52
    const platform = os.type();
M
muwoo 已提交
53 54
    try {
      if (!opConfig.config) {
55 56 57
        opConfig.config = JSON.parse(
          fs.readFileSync(configPath) || JSON.stringify(defaultConfig[platform])
        );
M
muwoo 已提交
58
      }
59
      // 重置
60 61 62 63 64 65
      if (
        !opConfig.config.version ||
        opConfig.config.version < defaultConfig[platform].version
      ) {
        opConfig.config = defaultConfig[platform];
        fs.writeFileSync(configPath, JSON.stringify(opConfig.config));
璃白.'s avatar
璃白. 已提交
66
    }
67
      return opConfig.config;
M
muwoo 已提交
68
    } catch (e) {
69 70
      opConfig.config = defaultConfig[platform];
      return opConfig.config;
M
muwoo 已提交
71 72 73
    }
  },
  set(key, value) {
74
    opConfig.config[key] = value;
75
    fs.writeFileSync(configPath, JSON.stringify(opConfig.config));
郭维嘉 已提交
76
  }
77
};