|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * 读取本地配置 |
| 5 | + */ |
| 6 | +const fs = require('fs') |
| 7 | +const path = require('path') |
| 8 | +/** |
| 9 | + * 判断配置文件是否存在 |
| 10 | + */ |
| 11 | +function isExit () { |
| 12 | + let success = true |
| 13 | + if (!(localConfig.config && typeof localConfig.config === 'object')) { |
| 14 | + success = initConfig() |
| 15 | + } |
| 16 | + return success |
| 17 | +} |
| 18 | +/** |
| 19 | + * 初始化config |
| 20 | + */ |
| 21 | +function initConfig () { |
| 22 | + try { |
| 23 | + const config = readConfig() |
| 24 | + if (config) { |
| 25 | + localConfig.config = JSON.parse(config) |
| 26 | + return true |
| 27 | + } |
| 28 | + const defalutConfig = {} |
| 29 | + const content = JSON.stringify(defalutConfig) |
| 30 | + fs.writeFileSync(localConfig.configUrl, content) |
| 31 | + localConfig.config = defalutConfig |
| 32 | + return true |
| 33 | + } catch (e) { |
| 34 | + return false |
| 35 | + } |
| 36 | +} |
| 37 | +/** |
| 38 | + * 读取文件 |
| 39 | + */ |
| 40 | +function readConfig () { |
| 41 | + try { |
| 42 | + const result = fs.readFileSync(localConfig.configUrl) |
| 43 | + return result |
| 44 | + } catch (error) { |
| 45 | + return false |
| 46 | + } |
| 47 | +} |
| 48 | +/** |
| 49 | + * 写入文件 |
| 50 | + */ |
| 51 | +function writeConfig (value) { |
| 52 | + try { |
| 53 | + const content = JSON.stringify(value) |
| 54 | + fs.writeFileSync(localConfig.configUrl, content) |
| 55 | + return true |
| 56 | + } catch (e) { |
| 57 | + return false |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const localConfig = { |
| 62 | + config: null, |
| 63 | + configUrl: path.join(__dirname, '../static/localConfig.json'), |
| 64 | + setStoragePath: (path) => { |
| 65 | + localConfig.configUrl = path |
| 66 | + }, |
| 67 | + getStoragePath: () => { |
| 68 | + return localConfig.configUrl |
| 69 | + }, |
| 70 | + getItem: (key) => { |
| 71 | + const success = isExit() |
| 72 | + if (success) { |
| 73 | + const result = localConfig.config[key] |
| 74 | + return result || '' |
| 75 | + } |
| 76 | + return null |
| 77 | + }, |
| 78 | + setItem: (key, value) => { |
| 79 | + const success = isExit() |
| 80 | + if (success) { |
| 81 | + const config = Object.assign({}, localConfig.config) |
| 82 | + config[key] = value |
| 83 | + const suc = writeConfig(config) |
| 84 | + if (suc) { |
| 85 | + localConfig.config = config |
| 86 | + return true |
| 87 | + } |
| 88 | + } |
| 89 | + return false |
| 90 | + }, |
| 91 | + getAll: () => { |
| 92 | + const success = isExit() |
| 93 | + if (success) { |
| 94 | + return localConfig.config |
| 95 | + } |
| 96 | + return null |
| 97 | + }, |
| 98 | + removeItem: (key) => { |
| 99 | + const value = localConfig.getItem(key) |
| 100 | + if (value) { |
| 101 | + const config = Object.assign({}, localConfig.config) |
| 102 | + delete config[key] |
| 103 | + const suc = writeConfig(config) |
| 104 | + if (suc) { |
| 105 | + localConfig.config = config |
| 106 | + return true |
| 107 | + } |
| 108 | + } |
| 109 | + return false |
| 110 | + }, |
| 111 | + clear: () => { |
| 112 | + const success = isExit() |
| 113 | + if (success) { |
| 114 | + const suc = writeConfig({}) |
| 115 | + if (suc) { |
| 116 | + localConfig.config = {} |
| 117 | + return true |
| 118 | + } |
| 119 | + } |
| 120 | + return false |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +module.exports = localConfig |
0 commit comments