mm_config 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/base.js +8 -11
  2. package/index.js +29 -40
  3. package/package.json +2 -3
package/base.js CHANGED
@@ -1,9 +1,3 @@
1
- const JSON5 = require('json5')
2
- const {
3
- writeFileSync,
4
- readFileSync
5
- } = require('fs');
6
-
7
1
  /**
8
2
  * 重够配置,同步保存
9
3
  * @param {Object} _config 配置对象
@@ -14,18 +8,21 @@ module.exports = function(_config, _jsonFile) {
14
8
  _jsonFile = this.filename;
15
9
  }
16
10
  if (!_config) {
17
- var str = readFileSync(_jsonFile, 'utf-8');
18
- if (!str) {
11
+ try {
12
+ _config = _jsonFile.loadJson();
13
+ } catch (error) {
19
14
  _config = {};
20
- } else {
21
- _config = JSON5.parse(str);
22
15
  }
23
16
  }
24
17
  return new Proxy(_config, {
25
18
  set: function(obj, prop, value) {
26
19
  obj[prop] = value;
27
20
  if (_jsonFile) {
28
- writeFileSync(_jsonFile, JSON.stringify(obj, null, 4), 'utf-8');
21
+ try {
22
+ _jsonFile.saveJson(obj); // 使用saveJson原型函数,参数为对象和缩进空格数
23
+ } catch (error) {
24
+ console.error('保存配置失败:', error);
25
+ }
29
26
  }
30
27
  return true
31
28
  }
package/index.js CHANGED
@@ -1,7 +1,4 @@
1
- const JSON5 = require('json5');
2
- const { writeFileSync, readFileSync, writeFile } = require('fs');
3
- const { promisify } = require('util');
4
- const writeFileAsync = promisify(writeFile);
1
+ require('mm_logs');
5
2
 
6
3
  /**
7
4
  * 配置管理器类,提供同步和异步API
@@ -31,12 +28,9 @@ class ConfigManager {
31
28
  // 如果没有提供配置且有文件路径,则从文件加载
32
29
  if (!_config && _jsonFile) {
33
30
  try {
34
- const content = readFileSync(_jsonFile, 'utf-8');
35
- if (content) {
36
- this.config = this.options.format === 'json5' ? JSON5.parse(content) : JSON.parse(content);
37
- }
38
- } catch (error) {
39
- console.error(`Error loading config from ${_jsonFile}:`, error.message);
31
+ this.config = _jsonFile.loadJson();
32
+ } catch(error) {
33
+ $.log.error(`Error loading config from ${_jsonFile}:`, error.message);
40
34
  this.config = {};
41
35
  }
42
36
  }
@@ -46,16 +40,12 @@ class ConfigManager {
46
40
  * 保存配置到文件(同步)
47
41
  */
48
42
  saveSync() {
49
- if (!this.filePath) return;
50
-
43
+ if (!this.filePath) return false;
51
44
  try {
52
- const content = this.options.pretty
53
- ? JSON.stringify(this.config, null, 4)
54
- : JSON.stringify(this.config);
55
- writeFileSync(this.filePath, content, 'utf-8');
45
+ this.filePath.saveJson(this.config);
56
46
  return true;
57
- } catch (error) {
58
- console.error(`Error saving config to ${this.filePath}:`, error.message);
47
+ } catch(error) {
48
+ $.log.error(`Error saving config to ${this.filePath}:`, error.message);
59
49
  return false;
60
50
  }
61
51
  }
@@ -66,21 +56,20 @@ class ConfigManager {
66
56
  */
67
57
  async saveAsync() {
68
58
  if (!this.filePath) return Promise.resolve(true);
69
-
59
+
70
60
  // 确保之前的写操作完成
71
61
  await this.writePromise;
72
-
73
- const content = this.options.pretty
74
- ? JSON.stringify(this.config, null, 4)
75
- : JSON.stringify(this.config);
76
-
77
- this.writePromise = writeFileAsync(this.filePath, content, 'utf-8')
78
- .then(() => true)
62
+
63
+ this.writePromise = Promise.resolve()
64
+ .then(() => {
65
+ this.filePath.saveJson(this.config);
66
+ return true;
67
+ })
79
68
  .catch(error => {
80
- console.error(`Error saving config to ${this.filePath}:`, error.message);
69
+ $.log.error(`Error saving config to ${this.filePath}:`, error.message);
81
70
  return false;
82
71
  });
83
-
72
+
84
73
  return this.writePromise;
85
74
  }
86
75
 
@@ -90,11 +79,11 @@ class ConfigManager {
90
79
  */
91
80
  getProxy() {
92
81
  const self = this;
93
-
82
+
94
83
  return new Proxy(this.config, {
95
84
  set(obj, prop, value) {
96
85
  obj[prop] = value;
97
-
86
+
98
87
  if (self.options.debounceTime > 0) {
99
88
  // 防抖处理
100
89
  if (self.writeTimer) {
@@ -107,10 +96,10 @@ class ConfigManager {
107
96
  // 无防抖,直接保存
108
97
  self.saveSync();
109
98
  }
110
-
99
+
111
100
  return true;
112
101
  },
113
-
102
+
114
103
  get(obj, prop) {
115
104
  // 提供一些特殊属性访问
116
105
  if (prop === '_saveAsync') {
@@ -122,7 +111,7 @@ class ConfigManager {
122
111
  if (prop === '_raw') {
123
112
  return obj;
124
113
  }
125
-
114
+
126
115
  return obj[prop];
127
116
  }
128
117
  });
@@ -134,11 +123,11 @@ class ConfigManager {
134
123
  */
135
124
  getAsyncProxy() {
136
125
  const self = this;
137
-
126
+
138
127
  return new Proxy(this.config, {
139
128
  set(obj, prop, value) {
140
129
  obj[prop] = value;
141
-
130
+
142
131
  if (self.options.debounceTime > 0) {
143
132
  if (self.writeTimer) {
144
133
  clearTimeout(self.writeTimer);
@@ -149,10 +138,10 @@ class ConfigManager {
149
138
  } else {
150
139
  self.saveAsync();
151
140
  }
152
-
141
+
153
142
  return true;
154
143
  },
155
-
144
+
156
145
  get(obj, prop) {
157
146
  if (prop === '_saveAsync') {
158
147
  return () => self.saveAsync();
@@ -163,7 +152,7 @@ class ConfigManager {
163
152
  if (prop === '_raw') {
164
153
  return obj;
165
154
  }
166
-
155
+
167
156
  return obj[prop];
168
157
  }
169
158
  });
@@ -182,7 +171,7 @@ function createConfig(_config, _jsonFile, options = {}) {
182
171
  if (!_jsonFile && this && this.filename) {
183
172
  _jsonFile = this.filename;
184
173
  }
185
-
174
+
186
175
  const manager = new ConfigManager(_config, _jsonFile, options);
187
176
  return manager.getProxy(); // 默认返回同步代理
188
177
  }
@@ -199,7 +188,7 @@ async function createConfigAsync(_config, _jsonFile, options = {}) {
199
188
  if (!_jsonFile && this && this.filename) {
200
189
  _jsonFile = this.filename;
201
190
  }
202
-
191
+
203
192
  const manager = new ConfigManager(_config, _jsonFile, options);
204
193
  return manager.getAsyncProxy();
205
194
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_config",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "这是超级美眉配置同步器,用于更改配置同步保存为JSON文件",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,7 +26,6 @@
26
26
  },
27
27
  "homepage": "https://gitee.com/qiuwenwu91/mm_config#readme",
28
28
  "dependencies": {
29
- "json5": "^2.2.3",
30
- "mm_expand": "^1.8.3"
29
+ "mm_logs": "^1.5.1"
31
30
  }
32
31
  }