fkg 1.0.1 → 1.0.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.
- package/lib/index.js +28 -39
- package/lib/utils/fs.js +27 -40
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,66 +1,55 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
const {str} = require('./utils/type');
|
|
3
|
-
const {isDir,isFile,link,addDir,addJson,readJson} = require('./utils/fs');
|
|
2
|
+
const {str,obj,arr} = require('./utils/type');
|
|
3
|
+
const {isDir,isFile,link,addDir,addJson,readJson,delFile} = require('./utils/fs');
|
|
4
4
|
class fkg {
|
|
5
|
-
constructor(
|
|
5
|
+
constructor(arg) {
|
|
6
6
|
this.is = {};
|
|
7
7
|
this.data = {};
|
|
8
8
|
this.dirs = {};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.dir = link(dir,'./data');
|
|
13
|
-
if(!isDir(this.dir))
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
this.dir = null;
|
|
10
|
+
if(arg&&obj(arg)) {
|
|
11
|
+
if(arg.dir&&isDir(arg.dir)){
|
|
12
|
+
this.dir = link(arg.dir,'./data');
|
|
13
|
+
if(!isDir(this.dir)) addDir(this.dir);
|
|
14
|
+
}
|
|
15
|
+
if(arg.dbName&&arr(arg.dbName)&&arr.length>0){
|
|
16
|
+
for (let i = 0;i < arg.dbName.length;i++) {
|
|
17
|
+
const n = arg.dbName[i];
|
|
18
|
+
this.data[n] = {};
|
|
19
|
+
if(this.dir) {
|
|
20
|
+
this.dirs[n] = link(this.dir,'./'+n);
|
|
21
|
+
if(!isDir(this.dirs[n])) addDir(this.dirs[n]);
|
|
20
22
|
}
|
|
21
|
-
}
|
|
22
|
-
}else{
|
|
23
|
-
this.dir = null;
|
|
23
|
+
};
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
-
deal();
|
|
27
26
|
}
|
|
28
|
-
|
|
27
|
+
table(n) {
|
|
29
28
|
if(!n||!str(n)) return;
|
|
30
|
-
if(this.
|
|
31
|
-
this.is[n] = 0;
|
|
32
|
-
if(!this.data[n]) {
|
|
33
|
-
this.dirs[n] = link(this.dir,'./'+n+'.fkg');
|
|
34
|
-
if(isFile(this.dirs[n])){
|
|
35
|
-
const res = await readJson(this.dirs[n]);
|
|
36
|
-
if(res) {
|
|
37
|
-
this.data[n] = res;
|
|
38
|
-
}else{
|
|
39
|
-
this.data[n] = {};
|
|
40
|
-
}
|
|
41
|
-
}else{
|
|
42
|
-
this.data[n] = {};
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}else{
|
|
46
|
-
if(!this.data[n]) this.data[n] = {};
|
|
47
|
-
}
|
|
29
|
+
if(!this.data[n]) this.data[n] = {};
|
|
48
30
|
const that = this;
|
|
49
31
|
return {
|
|
50
32
|
list(){
|
|
51
33
|
return that.data[n];
|
|
52
34
|
},
|
|
53
35
|
get(id){
|
|
36
|
+
if(that.dir&&!that.data[n][id]) {
|
|
37
|
+
const url = link(that.dirs[n],'./'+id);
|
|
38
|
+
if(isFile(url)){
|
|
39
|
+
const res = readJson(url);
|
|
40
|
+
if(res) that.data[n][id] = res;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
54
43
|
return that.data[n][id];
|
|
55
44
|
},
|
|
56
45
|
set(id,d){
|
|
57
|
-
that.is[n]++;
|
|
58
46
|
that.data[n][id] = d;
|
|
47
|
+
if(that.dir) addJson(link(that.dirs[n],'./'+id),d);
|
|
59
48
|
return true;
|
|
60
49
|
},
|
|
61
50
|
del(id){
|
|
62
|
-
that.is[n]++;
|
|
63
51
|
delete that.data[n][id];
|
|
52
|
+
if(that.dir) delFile(link(that.dirs[n],'./'+id));
|
|
64
53
|
return true;
|
|
65
54
|
}
|
|
66
55
|
}
|
package/lib/utils/fs.js
CHANGED
|
@@ -9,14 +9,12 @@ const isDir = (url) => {
|
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
const addFile = (url, data)=>{
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
})
|
|
12
|
+
try {
|
|
13
|
+
fs.writeFileSync(url, data,{encoding:'utf8'});
|
|
14
|
+
return true;
|
|
15
|
+
} catch (err) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
20
18
|
}
|
|
21
19
|
module.exports = {
|
|
22
20
|
isDir,
|
|
@@ -36,19 +34,11 @@ module.exports = {
|
|
|
36
34
|
return addFile(url,JSON.stringify(data));
|
|
37
35
|
},
|
|
38
36
|
readJson(url){
|
|
39
|
-
|
|
40
|
-
fs.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if(data) {
|
|
45
|
-
resolve(JSON.parse(data))
|
|
46
|
-
}else{
|
|
47
|
-
resolve(null);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
})
|
|
37
|
+
try {
|
|
38
|
+
return JSON.parse(fs.readFileSync(url));
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
52
42
|
},
|
|
53
43
|
readFile(url) {
|
|
54
44
|
return new Promise((resolve) => {
|
|
@@ -61,7 +51,7 @@ module.exports = {
|
|
|
61
51
|
});
|
|
62
52
|
})
|
|
63
53
|
},
|
|
64
|
-
|
|
54
|
+
readdirSync(url){
|
|
65
55
|
return fs.readdirSync(url);
|
|
66
56
|
},
|
|
67
57
|
dirFile(paths) {
|
|
@@ -89,26 +79,23 @@ module.exports = {
|
|
|
89
79
|
return obj;
|
|
90
80
|
},
|
|
91
81
|
addDir(url) {
|
|
92
|
-
|
|
93
|
-
fs.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
82
|
+
try {
|
|
83
|
+
fs.mkdirSync(url);
|
|
84
|
+
return true;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
return false
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
mkdirSync(url) {
|
|
90
|
+
return fs.mkdirSync(url);
|
|
101
91
|
},
|
|
102
92
|
delFile(url) {
|
|
103
|
-
|
|
104
|
-
fs.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
})
|
|
111
|
-
})
|
|
93
|
+
try {
|
|
94
|
+
fs.unlinkSync(url);
|
|
95
|
+
return true
|
|
96
|
+
} catch (error) {
|
|
97
|
+
return false
|
|
98
|
+
}
|
|
112
99
|
},
|
|
113
100
|
getSize(obj) {
|
|
114
101
|
let size = 0;
|