mm_config 1.0.7 → 1.0.9

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 +33 -0
  2. package/index.js +4 -15
  3. package/package.json +3 -2
package/base.js ADDED
@@ -0,0 +1,33 @@
1
+ const JSON5 = require('json5')
2
+ const {
3
+ writeFileSync,
4
+ readFileSync
5
+ } = require('fs');
6
+
7
+ /**
8
+ * 重够配置,同步保存
9
+ * @param {Object} _config 配置对象
10
+ * @param {String} _jsonFile 配置文件
11
+ */
12
+ module.exports = function(_config, _jsonFile) {
13
+ if (!_jsonFile) {
14
+ _jsonFile = this.filename;
15
+ }
16
+ if (!_config) {
17
+ var str = readFileSync(_jsonFile, 'utf-8');
18
+ if (!str) {
19
+ _config = {};
20
+ } else {
21
+ _config = JSON5.parse(str);
22
+ }
23
+ }
24
+ return new Proxy(_config, {
25
+ set: function(obj, prop, value) {
26
+ obj[prop] = value;
27
+ if (_jsonFile) {
28
+ writeFileSync(_jsonFile, JSON.stringify(obj, null, 4), 'utf-8');
29
+ }
30
+ return true
31
+ }
32
+ });
33
+ }
package/index.js CHANGED
@@ -1,8 +1,4 @@
1
- const JSON5 = require('json5')
2
- const {
3
- writeFileSync,
4
- readFileSync
5
- } = require('fs');
1
+ require("mm_expand");
6
2
 
7
3
  /**
8
4
  * 重够配置,同步保存
@@ -14,22 +10,15 @@ module.exports = function(_config, _jsonFile) {
14
10
  _jsonFile = this.filename;
15
11
  }
16
12
  if (!_config) {
17
- var str = readFileSync(_jsonFile, 'utf-8');
18
- if (!str) {
19
- _config = {};
20
- } else {
21
- _config = JSON5.parse(str);
22
- }
13
+ _config = _jsonFile.loadJson() || {};
23
14
  }
24
15
  return new Proxy(_config, {
25
- get: function(obj, prop, value) {
26
- return value;
27
- },
28
16
  set: function(obj, prop, value) {
29
17
  obj[prop] = value;
30
18
  if (_jsonFile) {
31
- writeFileSync(_jsonFile, JSON.stringify(obj, null, 4), 'utf-8');
19
+ _jsonFile.saveJson(obj);
32
20
  }
21
+ return true
33
22
  }
34
23
  });
35
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_config",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "这是超级美眉配置同步器,用于更改配置同步保存为JSON文件",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -23,6 +23,7 @@
23
23
  },
24
24
  "homepage": "https://github.com/qiuwenwu/mm_config#readme",
25
25
  "dependencies": {
26
- "json5": "^2.2.3"
26
+ "json5": "^2.2.3",
27
+ "mm_expand": "^1.5.4"
27
28
  }
28
29
  }