mm_config 1.0.6 → 1.0.8
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/base.js +32 -0
- package/config.json +3 -0
- package/index.js +1 -4
- package/package.json +3 -2
- package/test.js +6 -6
package/base.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
package/config.json
CHANGED
package/index.js
CHANGED
|
@@ -10,10 +10,7 @@ module.exports = function(_config, _jsonFile) {
|
|
|
10
10
|
_jsonFile = this.filename;
|
|
11
11
|
}
|
|
12
12
|
if (!_config) {
|
|
13
|
-
_config = _jsonFile.loadJson();
|
|
14
|
-
if (!_config) {
|
|
15
|
-
_config = {};
|
|
16
|
-
}
|
|
13
|
+
_config = _jsonFile.loadJson() || {};
|
|
17
14
|
}
|
|
18
15
|
return new Proxy(_config, {
|
|
19
16
|
set: function(obj, prop, value) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_config",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
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
|
-
"
|
|
26
|
+
"json5": "^2.2.3",
|
|
27
|
+
"mm_expand": "^1.5.4"
|
|
27
28
|
}
|
|
28
29
|
}
|
package/test.js
CHANGED
|
@@ -7,12 +7,12 @@ async function test(){
|
|
|
7
7
|
// }, "./config.json");
|
|
8
8
|
|
|
9
9
|
this.filename = "./config.json";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
var config = conf({
|
|
11
|
+
name: "demo",
|
|
12
|
+
state: 2
|
|
13
|
+
});
|
|
14
|
+
config.name = "test";
|
|
15
|
+
config.sort = 2;
|
|
16
16
|
|
|
17
17
|
config = conf(null, this.filename);
|
|
18
18
|
console.log(config);
|