1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const fs_1 = __importDefault(require("fs"));
|
7 | const path_1 = __importDefault(require("path"));
|
8 |
|
9 | class 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 |
|
33 |
|
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 |
|
67 | this._loadFile();
|
68 | }
|
69 | else if (curr.ctime.getTime() == 0) {
|
70 |
|
71 |
|
72 | }
|
73 | else if (curr.mtime.getTime() != prev.mtime.getTime()) {
|
74 |
|
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 |
|
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 |
|
152 |
|
153 | createTemplateJson() {
|
154 |
|
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 | }
|
159 | var instance = new TeConfigMgr();
|
160 | exports.ConfigMgr = {
|
161 | name: 'ConfigMgr',
|
162 | |
163 |
|
164 |
|
165 |
|
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 |
|
175 |
|
176 |
|
177 | has: function (v) {
|
178 | return instance.has(v);
|
179 | },
|
180 | |
181 |
|
182 |
|
183 |
|
184 | default: function (d) {
|
185 | return instance.registDefault(d);
|
186 | },
|
187 | |
188 |
|
189 |
|
190 |
|
191 | changeFile: function (path) {
|
192 | return instance.changeFile(path);
|
193 | }
|
194 | };
|
195 | exports.default = exports.ConfigMgr;
|