UNPKG

5.95 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const fs_1 = __importDefault(require("fs"));
7const path_1 = __importDefault(require("path"));
8// 这里开始考虑cluster的模式方便部署
9class TeConfigMgr {
10 constructor() {
11 this._tmpValue = {};
12 this._configValue = {};
13 this._defaultValue = {};
14 this._filePath = '';
15 this._filePath = path_1.default.join(process.cwd(), 'config.json');
16 fs_1.default.watchFile(this._filePath, this._watchFile.bind(this));
17 this._loadFile();
18 }
19 static get inst() {
20 if (!this._inst)
21 this._inst = new TeConfigMgr();
22 return this._inst;
23 }
24 static get(v, int = false) {
25 if (int) {
26 return parseInt(this.inst.get(v));
27 }
28 return this.inst.get(v);
29 }
30 /**
31 * 零时的调整配置,重启后失效
32 * @param k
33 * @param v
34 */
35 static set(k, v) {
36 this.inst._tmpValue[k] = v;
37 this.inst.set(k, v);
38 }
39 static has(v) {
40 return this.inst.has(v);
41 }
42 static default(d) {
43 return this.inst.registDefault(d);
44 }
45 changeFile(file) {
46 fs_1.default.unwatchFile(this._filePath);
47 this._filePath = file;
48 fs_1.default.watchFile(this._filePath, this._watchFile.bind(this));
49 this._configValue = {};
50 this._loadFile();
51 }
52 _loadFile() {
53 try {
54 var jt = fs_1.default.readFileSync(this._filePath);
55 this._configValue = JSON.parse(jt.toString());
56 // 这里需要把特殊的配置项目设置进入
57 for (let key in this._tmpValue) {
58 this.set(key, this._tmpValue[key]);
59 }
60 }
61 catch (e) {
62 }
63 }
64 _watchFile(curr, prev) {
65 if (prev.ctime.getTime() == 0 && curr.ctime.getTime() != 0) {
66 //console.log('文件被创建!');
67 this._loadFile();
68 }
69 else if (curr.ctime.getTime() == 0) {
70 // console.log('文件被删除!');
71 // 文件删除了,但是配置先不要响应删除操作了
72 }
73 else if (curr.mtime.getTime() != prev.mtime.getTime()) {
74 // console.log('文件有修改');
75 this._loadFile();
76 }
77 }
78 registDefault(def) {
79 this._defaultValue = Object.assign(this._defaultValue, def);
80 }
81 _get(srcObj, key) {
82 var arr = key.split('.');
83 var tObj = srcObj;
84 for (var i = 0; i < arr.length; i++) {
85 if (!tObj)
86 return null;
87 tObj = tObj[arr[i]];
88 }
89 return tObj;
90 }
91 get(key) {
92 var ret = this._get(this._configValue, key);
93 if (ret == null)
94 ret = this._get(this._defaultValue, key);
95 if (ret == null)
96 ret = 0;
97 return ret;
98 }
99 set(key, v) {
100 if (this._set(this._configValue, key, v, false)) {
101 return;
102 }
103 this._set(this._defaultValue, key, v, true);
104 }
105 // 设置新的属性上去,如果没有就不设置了
106 _set(srcObj, key, v, force) {
107 var arr = key.split('.');
108 var tObj = srcObj;
109 for (var i = 0; i < arr.length; i++) {
110 if (i == arr.length - 1) {
111 tObj[arr[i]] = v;
112 }
113 else {
114 if (tObj[arr[i]] == undefined) {
115 if (force == false)
116 return false;
117 else {
118 tObj[arr[i]] = {};
119 }
120 }
121 tObj = tObj[arr[i]];
122 }
123 }
124 return true;
125 }
126 _has(srcObj, key) {
127 var arr = key.split('.');
128 var tObj = srcObj;
129 for (var i = 0; i < arr.length; i++) {
130 if (!tObj || !tObj.hasOwnProperty(arr[i]))
131 return false;
132 tObj = tObj[arr[i]];
133 }
134 return true;
135 }
136 has(key) {
137 // this._use_key_log(key);
138 // 先找动态的
139 // 再找配置的
140 // 最后找默认的
141 var ret = false;
142 if (ret == false)
143 ret = this._has(this._configValue, key);
144 if (ret == false)
145 ret = this._has(this._defaultValue, key);
146 if (ret == false)
147 ret = false;
148 return ret;
149 }
150 /**
151 * 制作json配置用的模板文件,或者说把配置用的js文件转成json文件,主要是json是不能有注释的
152 */
153 createTemplateJson() {
154 // 这里把defalut的生成一个json的就可以了
155 var jsonFile = JSON.stringify(this._defaultValue, null, 4);
156 fs_1.default.writeFileSync(path_1.default.join(process.cwd(), 'config.templete'), jsonFile);
157 }
158}
159var instance = new TeConfigMgr();
160exports.ConfigMgr = {
161 name: 'ConfigMgr',
162 /**
163 * 获取配置项 支持 a.b.c
164 * @param {string} v
165 * @param {boolean} int
166 */
167 get: function (v, int = false) {
168 return (int ? parseInt(instance.get(v)) : instance.get(v));
169 },
170 set: function (k, value) {
171 return instance.set(k, value);
172 },
173 /**
174 * 检查配置项是否存在 支持 a.b.c
175 * @param v
176 */
177 has: function (v) {
178 return instance.has(v);
179 },
180 /**
181 * 设置默认配置项
182 * @param d
183 */
184 default: function (d) {
185 return instance.registDefault(d);
186 },
187 /**
188 * 切换配置文件
189 * @param path
190 */
191 changeFile: function (path) {
192 return instance.changeFile(path);
193 }
194};
195exports.default = exports.ConfigMgr;