mm_config 2.1.6 → 2.1.7

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.
package/README_EN.md ADDED
@@ -0,0 +1,382 @@
1
+ # mm_config
2
+
3
+ Super Meimei Configuration Helper - Dynamic file-based configuration with automatic synchronization.
4
+
5
+ ## Features
6
+
7
+ - Dynamic reading and modification of configuration files
8
+ - Automatic synchronization to JSON files when configuration changes
9
+ - Proxy-based property monitoring for convenient operation
10
+ - Support for both synchronous and asynchronous API operations
11
+ - Debounce functionality to reduce frequent file saves
12
+ - Built-in error handling mechanism for improved stability
13
+ - Support for JSON5 format configuration files
14
+ - Concise naming following single-word priority principle
15
+ - Built-in cache management to avoid duplicate configuration instances
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install mm_config
21
+ ```
22
+
23
+ ## Quick Start
24
+
25
+ ### Basic Usage
26
+
27
+ ```javascript
28
+ const { create } = require('mm_config');
29
+
30
+ // Create configuration with file path
31
+ const config = create("./config.json", {
32
+ name: "demo",
33
+ state: 1
34
+ });
35
+
36
+ // Modifications automatically save to file
37
+ config.name = "test";
38
+ config.sort = 2;
39
+
40
+ console.log(config); // { name: 'test', state: 1, sort: 2 }
41
+ ```
42
+
43
+ ### Async Usage
44
+
45
+ ```javascript
46
+ const { createAsync } = require('mm_config');
47
+
48
+ async function setup() {
49
+ const config = await createAsync("./async_config.json", {
50
+ name: "asyncDemo"
51
+ });
52
+
53
+ config.timestamp = new Date().toISOString();
54
+ return config;
55
+ }
56
+ ```
57
+
58
+ ## API Reference
59
+
60
+ ### Import Module
61
+
62
+ ```javascript
63
+ // Import configuration classes
64
+ const { Config, create, createAsync, clear, clearAll } = require('mm_config');
65
+ ```
66
+
67
+ ### Configuration Initialization
68
+
69
+ #### Synchronous API
70
+
71
+ ```javascript
72
+ /**
73
+ * Create synchronous configuration
74
+ * @param {string} file - Configuration file path
75
+ * @param {object} config - Configuration object, optional
76
+ * @param {object} options - Configuration options, optional
77
+ * @param {number} options.debounce_time - Debounce time in ms, default 0
78
+ * @param {boolean} options.pretty - Whether to prettify output, default true
79
+ * @param {string} options.format - Format (json/json5), default 'json'
80
+ * @returns {Proxy} Configuration proxy object
81
+ */
82
+ const config = create(file, config, options);
83
+ ```
84
+
85
+ #### Asynchronous API
86
+
87
+ ```javascript
88
+ /**
89
+ * Create asynchronous configuration
90
+ * @param {string} file - Configuration file path
91
+ * @param {object} config - Configuration object, optional
92
+ * @param {object} options - Configuration options, optional
93
+ * @returns {Promise<Proxy>} Promise of configuration proxy object
94
+ */
95
+ const config = await createAsync(file, config, options);
96
+ ```
97
+
98
+ #### Cache Management API
99
+
100
+ ```javascript
101
+ /**
102
+ * Clear configuration cache for specific file
103
+ * @param {string} file - Configuration file path
104
+ */
105
+ clear(file);
106
+
107
+ /**
108
+ * Clear all configuration caches
109
+ */
110
+ clearAll();
111
+ ```
112
+
113
+ #### Proxy Object Special Methods
114
+
115
+ Access these special methods through the proxy object:
116
+
117
+ - `_saveSync()`: Synchronously save configuration to file
118
+ - `_saveAsync()`: Asynchronously save configuration to file
119
+ - `_raw`: Get the raw configuration object
120
+
121
+ ## Usage Examples
122
+
123
+ ### 1. Basic Synchronous Configuration
124
+
125
+ ```javascript
126
+ const { create } = require('mm_config');
127
+
128
+ // Create configuration with file path
129
+ const config = create("./config.json", {
130
+ name: "demo",
131
+ state: 1
132
+ });
133
+
134
+ // Modifications automatically save to file
135
+ config.name = "test";
136
+ config.sort = 2;
137
+
138
+ // Explicit save call
139
+ config._saveSync();
140
+
141
+ // Read existing configuration
142
+ const existingConfig = create("./config.json");
143
+ console.log(existingConfig.name); // "test"
144
+ ```
145
+
146
+ ### 2. Using Async API
147
+
148
+ ```javascript
149
+ const { createAsync } = require('mm_config');
150
+
151
+ async function setupConfig() {
152
+ // Create async configuration
153
+ const config = await createAsync("./async_config.json", {
154
+ name: "asyncDemo",
155
+ version: "1.0.0"
156
+ });
157
+
158
+ // Modifications asynchronously save to file
159
+ config.timestamp = new Date().toISOString();
160
+
161
+ // Explicit async save call
162
+ await config._saveAsync();
163
+
164
+ return config;
165
+ }
166
+
167
+ setupConfig().then(config => {
168
+ console.log('Configuration setup complete:', config);
169
+ });
170
+ ```
171
+
172
+ ### 3. Using Debounce Functionality
173
+
174
+ ```javascript
175
+ const { create } = require('mm_config');
176
+
177
+ // Create configuration with debounce (500ms delay)
178
+ const config = create("./debounce_config.json", {
179
+ name: "debounceDemo"
180
+ }, {
181
+ debounce_time: 500 // 500ms debounce time
182
+ });
183
+
184
+ // Rapid modifications will only save the last change after 500ms
185
+ for (let i = 0; i < 10; i++) {
186
+ config.counter = i;
187
+ config.timestamp = new Date().toISOString();
188
+ }
189
+
190
+ // Only the last value will be saved
191
+ console.log(config.counter); // 9
192
+ ```
193
+
194
+ ### 4. Using JSON5 Format
195
+
196
+ ```javascript
197
+ const { create } = require('mm_config');
198
+
199
+ // Create configuration with JSON5 format support
200
+ const config = create("./config.json5", null, {
201
+ format: 'json5'
202
+ });
203
+
204
+ // JSON5 format supports comments, trailing commas, etc.
205
+ config.description = "This supports JSON5 format";
206
+ config.version = "1.0.0";
207
+ ```
208
+
209
+ ### 5. Direct Config Class Usage
210
+
211
+ ```javascript
212
+ const { Config } = require('mm_config');
213
+
214
+ // Create configuration instance
215
+ const cfg = new Config({
216
+ debounce_time: 100,
217
+ pretty: true
218
+ }, {
219
+ name: "directDemo"
220
+ });
221
+
222
+ // Get proxy object
223
+ const config = cfg.getProxy();
224
+
225
+ // Use configuration
226
+ config.version = "1.0.0";
227
+
228
+ // Manual save methods
229
+ cfg.saveSync();
230
+ await cfg.saveAsync();
231
+ ```
232
+
233
+ ### 6. Using Configuration Operation Methods
234
+
235
+ ```javascript
236
+ const { Config } = require('mm_config');
237
+
238
+ const cfg = new Config({}, "./methods_config.json");
239
+
240
+ async function manageConfig() {
241
+ // Set configuration value
242
+ await cfg.set("name", "test");
243
+
244
+ // Get configuration value
245
+ const name = await cfg.get("name");
246
+ console.log('Retrieved name:', name); // "test"
247
+
248
+ // Check if configuration exists
249
+ const hasName = await cfg.has("name");
250
+ console.log('Has name:', hasName); // true
251
+
252
+ // Delete configuration item
253
+ await cfg.del("name");
254
+
255
+ // Get all keys
256
+ const keys = await cfg.keys();
257
+ console.log('All keys:', keys);
258
+ }
259
+
260
+ manageConfig();
261
+ ```
262
+
263
+ ### 7. Cache Management Functionality
264
+
265
+ ```javascript
266
+ const { create, clear, clearAll } = require('mm_config');
267
+
268
+ // Create configuration instances
269
+ const config1 = create("./cache1.json", { test: "cache1" });
270
+ const config2 = create("./cache2.json", { test: "cache2" });
271
+
272
+ // Clear specific file cache
273
+ clear("./cache1.json");
274
+
275
+ // Recreate will generate new instance
276
+ const config1New = create("./cache1.json", { test: "cache1_new" });
277
+
278
+ // Clear all caches
279
+ clearAll();
280
+
281
+ // All configurations will be recreated
282
+ const config2New = create("./cache2.json", { test: "cache2_new" });
283
+ ```
284
+
285
+ ## Configuration Options
286
+
287
+ Available options when creating configurations:
288
+
289
+ | Option | Type | Default | Description |
290
+ |--------|------|---------|-------------|
291
+ | debounce_time | number | 0 | Debounce time in milliseconds, delays saves after multiple modifications |
292
+ | pretty | boolean | true | Whether to prettify JSON output, set to false for compressed JSON |
293
+ | format | string | 'json' | Configuration file format, options: 'json' or 'json5' |
294
+
295
+ ## Advanced Usage
296
+
297
+ ### Error Handling
298
+
299
+ ```javascript
300
+ const { create } = require('mm_config');
301
+
302
+ try {
303
+ const config = create("./config.json", { test: 1 });
304
+ config.invalid = "value";
305
+ } catch (error) {
306
+ console.error('Configuration operation error:', error.message);
307
+ }
308
+ ```
309
+
310
+ ### Performance Optimization
311
+
312
+ ```javascript
313
+ // Use debounce to reduce frequent file I/O operations
314
+ const config = create("./config.json", {}, {
315
+ debounce_time: 1000 // 1 second debounce
316
+ });
317
+
318
+ // Use _raw property for batch operations to avoid frequent saves
319
+ const rawConfig = config._raw;
320
+ rawConfig.field1 = "value1";
321
+ rawConfig.field2 = "value2";
322
+ rawConfig.field3 = "value3";
323
+ // Manual save once
324
+ config._saveSync();
325
+ ```
326
+
327
+ ## Important Notes
328
+
329
+ 1. **File Permissions**: Ensure write permissions for the files
330
+ 2. **Async Operations**: Async API is suitable for non-blocking environments
331
+ 3. **Cache Management**: Configuration instances are cached, same file path returns same instance
332
+ 4. **JSON5 Format**: When using JSON5 format, saves still use standard JSON format but can read JSON5 files
333
+ 5. **Memory Management**: Use clear/clearAll to clean cache and avoid memory leaks
334
+
335
+ ## Dependencies
336
+
337
+ - **json5**: For parsing JSON5 format
338
+ - **mm_expand**: For file operation extensions
339
+
340
+ ## Naming Convention
341
+
342
+ This module follows concise naming principles:
343
+ - Class names: `Config` (single-word priority)
344
+ - Method names: `get()`, `set()`, `has()`, `del()` (verb-first)
345
+ - Variable names: `val`, `key`, `bl` (concise and no redundancy)
346
+
347
+ ## Testing
348
+
349
+ ```bash
350
+ # Run tests
351
+ npm test
352
+
353
+ # Or run test file directly
354
+ node test.js
355
+ ```
356
+
357
+ ## License
358
+
359
+ ISC License
360
+
361
+ ## Version History
362
+
363
+ - **v2.1.6**: Fixed code standards issues, improved documentation
364
+ - **v2.1.5**: Optimized ESLint configuration, improved code quality
365
+ - **v2.1.4**: Added cache management, fixed memory leaks
366
+ - **v2.1.3**: Refactored API, following concise naming conventions
367
+ - **v2.1.2**: Added JSON5 format support
368
+ - **v2.1.1**: Optimized debounce functionality
369
+ - **v2.1.0**: Initial release
370
+
371
+ ## Contributing
372
+
373
+ Welcome to submit Issues and Pull Requests to improve this project.
374
+
375
+ ## Related Projects
376
+
377
+ - [mm_expand](https://github.com/supermm/mm_expand) - File operation extension module
378
+ - [mm_eslint](https://github.com/supermm/mm_eslint) - ESLint configuration plugin
379
+
380
+ ---
381
+
382
+ **mm_config** - Making configuration management simple and efficient!