config2ts 2.2.1 → 2.2.3
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.md +13 -1
- package/config/data.csv +6 -6
- package/config/data.csv.ts +104 -83
- package/config/ini-file.ini +9 -9
- package/config/ini-file.ini.ts +8 -8
- package/config/no-id.csv +4 -4
- package/config/no-id.csv.ts +26 -26
- package/config/toml_file.toml +7 -7
- package/config/toml_file.toml.ts +7 -7
- package/config/total.ts +151 -130
- package/package.json +1 -1
- package/src/config2ts.js +160 -133
- package/tests/config2ts.spec.js +27 -22
package/README.md
CHANGED
|
@@ -5,7 +5,19 @@ convert config to ts file.
|
|
|
5
5
|
run command: `npm install -g config2ts`
|
|
6
6
|
|
|
7
7
|
# how to use
|
|
8
|
-
config2ts
|
|
8
|
+
config2ts -d config -o dist -m csv.ts
|
|
9
|
+
|
|
10
|
+
# support type
|
|
11
|
+
| csv field | typescript type |
|
|
12
|
+
| :---------: | :------------: |
|
|
13
|
+
| Index | string |
|
|
14
|
+
| String | string |
|
|
15
|
+
| Number | number |
|
|
16
|
+
| Boolean | boolean |
|
|
17
|
+
| Enum | type |
|
|
18
|
+
| String[] | string[] |
|
|
19
|
+
| Number[] | number[] |
|
|
20
|
+
| Enum[] | type[] |
|
|
9
21
|
|
|
10
22
|
## Usage
|
|
11
23
|
```
|
package/config/data.csv
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
id,name,bool,num,mytype,stringarray,numberarray
|
|
2
|
-
|
|
3
|
-
1,xxx,1,111,type1,"1, 2, 3","1, 2, 3"
|
|
4
|
-
2,xxx,,222,type2,"a,b",
|
|
5
|
-
3,333,,33,,,"1,2"
|
|
6
|
-
4,444,0,444,type2,",1,,b",",1,,3"
|
|
1
|
+
id,name,bool,num,mytype,stringarray,numberarray,typearray
|
|
2
|
+
Index,String,Boolean,Number,Enum,String[],Number[],Enum[]
|
|
3
|
+
1,xxx,1,111,type1,"1, 2, 3","1, 2, 3",t1
|
|
4
|
+
2,xxx,,222,type2,"a,b",,"t1,t2"
|
|
5
|
+
3,333,,33,,,"1,2","t2,t3,t4"
|
|
6
|
+
4,444,0,444,type2,",1,,b",",1,,3",
|
package/config/data.csv.ts
CHANGED
|
@@ -1,84 +1,105 @@
|
|
|
1
|
-
export namespace DataCsv {
|
|
2
|
-
|
|
3
|
-
export type mytype = "type1" | "type2" | "";
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
],
|
|
72
|
-
"
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
1
|
+
export namespace DataCsv {
|
|
2
|
+
|
|
3
|
+
export type mytype = "type1" | "type2" | "";
|
|
4
|
+
export const mytypeList: mytype[] = ["type1", "type2", ""];
|
|
5
|
+
|
|
6
|
+
export type typearray = "t1" | "t2" | "t3" | "t4" | "";
|
|
7
|
+
export const typearrayList: typearray[] = ["t1", "t2", "t3", "t4", ""];
|
|
8
|
+
|
|
9
|
+
export interface Record {
|
|
10
|
+
id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
bool: boolean;
|
|
13
|
+
num: number;
|
|
14
|
+
mytype: mytype;
|
|
15
|
+
stringarray: string[];
|
|
16
|
+
numberarray: number[];
|
|
17
|
+
typearray: typearray[];
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const List: Record[] = [
|
|
21
|
+
{
|
|
22
|
+
"id": "1",
|
|
23
|
+
"name": "xxx",
|
|
24
|
+
"bool": true,
|
|
25
|
+
"num": 111,
|
|
26
|
+
"mytype": "type1",
|
|
27
|
+
"stringarray": [
|
|
28
|
+
"1",
|
|
29
|
+
" 2",
|
|
30
|
+
" 3"
|
|
31
|
+
],
|
|
32
|
+
"numberarray": [
|
|
33
|
+
1,
|
|
34
|
+
2,
|
|
35
|
+
3
|
|
36
|
+
],
|
|
37
|
+
"typearray": [
|
|
38
|
+
"t1"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"id": "2",
|
|
43
|
+
"name": "xxx",
|
|
44
|
+
"bool": false,
|
|
45
|
+
"num": 222,
|
|
46
|
+
"mytype": "type2",
|
|
47
|
+
"stringarray": [
|
|
48
|
+
"a",
|
|
49
|
+
"b"
|
|
50
|
+
],
|
|
51
|
+
"numberarray": [
|
|
52
|
+
0
|
|
53
|
+
],
|
|
54
|
+
"typearray": [
|
|
55
|
+
"t1",
|
|
56
|
+
"t2"
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"id": "3",
|
|
61
|
+
"name": "333",
|
|
62
|
+
"bool": false,
|
|
63
|
+
"num": 33,
|
|
64
|
+
"mytype": "",
|
|
65
|
+
"stringarray": [
|
|
66
|
+
""
|
|
67
|
+
],
|
|
68
|
+
"numberarray": [
|
|
69
|
+
1,
|
|
70
|
+
2
|
|
71
|
+
],
|
|
72
|
+
"typearray": [
|
|
73
|
+
"t2",
|
|
74
|
+
"t3",
|
|
75
|
+
"t4"
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"id": "4",
|
|
80
|
+
"name": "444",
|
|
81
|
+
"bool": true,
|
|
82
|
+
"num": 444,
|
|
83
|
+
"mytype": "type2",
|
|
84
|
+
"stringarray": [
|
|
85
|
+
"",
|
|
86
|
+
"1",
|
|
87
|
+
"",
|
|
88
|
+
"b"
|
|
89
|
+
],
|
|
90
|
+
"numberarray": [
|
|
91
|
+
0,
|
|
92
|
+
1,
|
|
93
|
+
0,
|
|
94
|
+
3
|
|
95
|
+
],
|
|
96
|
+
"typearray": [
|
|
97
|
+
""
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
];
|
|
101
|
+
|
|
102
|
+
export const Map: { [id: string]: Record } = {};
|
|
103
|
+
for (const v of List) { Map[v.id] = v; };
|
|
104
|
+
|
|
84
105
|
};
|
package/config/ini-file.ini
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
num = 1
|
|
2
|
-
|
|
3
|
-
str = "string"
|
|
4
|
-
|
|
5
|
-
bool = true
|
|
6
|
-
|
|
7
|
-
[ItemType]
|
|
8
|
-
book = 1
|
|
9
|
-
fruit = 2
|
|
1
|
+
num = 1
|
|
2
|
+
|
|
3
|
+
str = "string"
|
|
4
|
+
|
|
5
|
+
bool = true
|
|
6
|
+
|
|
7
|
+
[ItemType]
|
|
8
|
+
book = 1
|
|
9
|
+
fruit = 2
|
package/config/ini-file.ini.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export const IniFileIni = {
|
|
2
|
-
"num": 1,
|
|
3
|
-
"str": "string",
|
|
4
|
-
"bool": true,
|
|
5
|
-
"ItemType": {
|
|
6
|
-
"book": 1,
|
|
7
|
-
"fruit": 2
|
|
8
|
-
}
|
|
1
|
+
export const IniFileIni = {
|
|
2
|
+
"num": 1,
|
|
3
|
+
"str": "string",
|
|
4
|
+
"bool": true,
|
|
5
|
+
"ItemType": {
|
|
6
|
+
"book": 1,
|
|
7
|
+
"fruit": 2
|
|
8
|
+
}
|
|
9
9
|
};
|
package/config/no-id.csv
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
name,bool,num
|
|
2
|
-
String,Boolean,Number
|
|
3
|
-
"xxx",1,111
|
|
4
|
-
"xxx",,222
|
|
1
|
+
name,bool,num
|
|
2
|
+
String,Boolean,Number
|
|
3
|
+
"xxx",1,111
|
|
4
|
+
"xxx",,222
|
|
5
5
|
333,,
|
package/config/no-id.csv.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
export namespace NoIdCsv {
|
|
2
|
-
|
|
3
|
-
export interface Record {
|
|
4
|
-
name: string;
|
|
5
|
-
bool: boolean;
|
|
6
|
-
num: number;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export const List: Record[] = [
|
|
10
|
-
{
|
|
11
|
-
"name": "xxx",
|
|
12
|
-
"bool": true,
|
|
13
|
-
"num": 111
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
"name": "xxx",
|
|
17
|
-
"bool": false,
|
|
18
|
-
"num": 222
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
"name": "333",
|
|
22
|
-
"bool": false,
|
|
23
|
-
"num": 0
|
|
24
|
-
}
|
|
25
|
-
];
|
|
26
|
-
|
|
1
|
+
export namespace NoIdCsv {
|
|
2
|
+
|
|
3
|
+
export interface Record {
|
|
4
|
+
name: string;
|
|
5
|
+
bool: boolean;
|
|
6
|
+
num: number;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const List: Record[] = [
|
|
10
|
+
{
|
|
11
|
+
"name": "xxx",
|
|
12
|
+
"bool": true,
|
|
13
|
+
"num": 111
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"name": "xxx",
|
|
17
|
+
"bool": false,
|
|
18
|
+
"num": 222
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"name": "333",
|
|
22
|
+
"bool": false,
|
|
23
|
+
"num": 0
|
|
24
|
+
}
|
|
25
|
+
];
|
|
26
|
+
|
|
27
27
|
};
|
package/config/toml_file.toml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
num = 1
|
|
2
|
-
|
|
3
|
-
str = "test"
|
|
4
|
-
|
|
5
|
-
[ItemType]
|
|
6
|
-
book = 1
|
|
7
|
-
fruit = 2
|
|
1
|
+
num = 1
|
|
2
|
+
|
|
3
|
+
str = "test"
|
|
4
|
+
|
|
5
|
+
[ItemType]
|
|
6
|
+
book = 1
|
|
7
|
+
fruit = 2
|
package/config/toml_file.toml.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export const TomlFileToml = {
|
|
2
|
-
"num": 1,
|
|
3
|
-
"str": "test",
|
|
4
|
-
"ItemType": {
|
|
5
|
-
"book": 1,
|
|
6
|
-
"fruit": 2
|
|
7
|
-
}
|
|
1
|
+
export const TomlFileToml = {
|
|
2
|
+
"num": 1,
|
|
3
|
+
"str": "test",
|
|
4
|
+
"ItemType": {
|
|
5
|
+
"book": 1,
|
|
6
|
+
"fruit": 2
|
|
7
|
+
}
|
|
8
8
|
};
|
package/config/total.ts
CHANGED
|
@@ -1,131 +1,152 @@
|
|
|
1
|
-
export namespace DataCsv {
|
|
2
|
-
|
|
3
|
-
export type mytype = "type1" | "type2" | "";
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
],
|
|
72
|
-
"
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
1
|
+
export namespace DataCsv {
|
|
2
|
+
|
|
3
|
+
export type mytype = "type1" | "type2" | "";
|
|
4
|
+
export const mytypeList: mytype[] = ["type1", "type2", ""];
|
|
5
|
+
|
|
6
|
+
export type typearray = "t1" | "t2" | "t3" | "t4" | "";
|
|
7
|
+
export const typearrayList: typearray[] = ["t1", "t2", "t3", "t4", ""];
|
|
8
|
+
|
|
9
|
+
export interface Record {
|
|
10
|
+
id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
bool: boolean;
|
|
13
|
+
num: number;
|
|
14
|
+
mytype: mytype;
|
|
15
|
+
stringarray: string[];
|
|
16
|
+
numberarray: number[];
|
|
17
|
+
typearray: typearray[];
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const List: Record[] = [
|
|
21
|
+
{
|
|
22
|
+
"id": "1",
|
|
23
|
+
"name": "xxx",
|
|
24
|
+
"bool": true,
|
|
25
|
+
"num": 111,
|
|
26
|
+
"mytype": "type1",
|
|
27
|
+
"stringarray": [
|
|
28
|
+
"1",
|
|
29
|
+
" 2",
|
|
30
|
+
" 3"
|
|
31
|
+
],
|
|
32
|
+
"numberarray": [
|
|
33
|
+
1,
|
|
34
|
+
2,
|
|
35
|
+
3
|
|
36
|
+
],
|
|
37
|
+
"typearray": [
|
|
38
|
+
"t1"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"id": "2",
|
|
43
|
+
"name": "xxx",
|
|
44
|
+
"bool": false,
|
|
45
|
+
"num": 222,
|
|
46
|
+
"mytype": "type2",
|
|
47
|
+
"stringarray": [
|
|
48
|
+
"a",
|
|
49
|
+
"b"
|
|
50
|
+
],
|
|
51
|
+
"numberarray": [
|
|
52
|
+
0
|
|
53
|
+
],
|
|
54
|
+
"typearray": [
|
|
55
|
+
"t1",
|
|
56
|
+
"t2"
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"id": "3",
|
|
61
|
+
"name": "333",
|
|
62
|
+
"bool": false,
|
|
63
|
+
"num": 33,
|
|
64
|
+
"mytype": "",
|
|
65
|
+
"stringarray": [
|
|
66
|
+
""
|
|
67
|
+
],
|
|
68
|
+
"numberarray": [
|
|
69
|
+
1,
|
|
70
|
+
2
|
|
71
|
+
],
|
|
72
|
+
"typearray": [
|
|
73
|
+
"t2",
|
|
74
|
+
"t3",
|
|
75
|
+
"t4"
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"id": "4",
|
|
80
|
+
"name": "444",
|
|
81
|
+
"bool": true,
|
|
82
|
+
"num": 444,
|
|
83
|
+
"mytype": "type2",
|
|
84
|
+
"stringarray": [
|
|
85
|
+
"",
|
|
86
|
+
"1",
|
|
87
|
+
"",
|
|
88
|
+
"b"
|
|
89
|
+
],
|
|
90
|
+
"numberarray": [
|
|
91
|
+
0,
|
|
92
|
+
1,
|
|
93
|
+
0,
|
|
94
|
+
3
|
|
95
|
+
],
|
|
96
|
+
"typearray": [
|
|
97
|
+
""
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
];
|
|
101
|
+
|
|
102
|
+
export const Map: { [id: string]: Record } = {};
|
|
103
|
+
for (const v of List) { Map[v.id] = v; };
|
|
104
|
+
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const IniFileIni = {
|
|
108
|
+
"num": 1,
|
|
109
|
+
"str": "string",
|
|
110
|
+
"bool": true,
|
|
111
|
+
"ItemType": {
|
|
112
|
+
"book": 1,
|
|
113
|
+
"fruit": 2
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export namespace NoIdCsv {
|
|
118
|
+
|
|
119
|
+
export interface Record {
|
|
120
|
+
name: string;
|
|
121
|
+
bool: boolean;
|
|
122
|
+
num: number;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export const List: Record[] = [
|
|
126
|
+
{
|
|
127
|
+
"name": "xxx",
|
|
128
|
+
"bool": true,
|
|
129
|
+
"num": 111
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
"name": "xxx",
|
|
133
|
+
"bool": false,
|
|
134
|
+
"num": 222
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"name": "333",
|
|
138
|
+
"bool": false,
|
|
139
|
+
"num": 0
|
|
140
|
+
}
|
|
141
|
+
];
|
|
142
|
+
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const TomlFileToml = {
|
|
146
|
+
"num": 1,
|
|
147
|
+
"str": "test",
|
|
148
|
+
"ItemType": {
|
|
149
|
+
"book": 1,
|
|
150
|
+
"fruit": 2
|
|
151
|
+
}
|
|
131
152
|
};
|
package/package.json
CHANGED
package/src/config2ts.js
CHANGED
|
@@ -1,133 +1,160 @@
|
|
|
1
|
-
exports.GetValidFileList = exports.GetHeaderInfo = exports.GetTsStringFromFileList = exports.GetTsString = exports.Convert = void 0;
|
|
2
|
-
var path = require("path");
|
|
3
|
-
var fs = require("fs");
|
|
4
|
-
var d3 = require("d3");
|
|
5
|
-
var changeCase = require("change-case");
|
|
6
|
-
var toml = require('toml');
|
|
7
|
-
var packageJson = require('../package.json');
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
d[k] = d[k].trim().split(',')
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
template +=
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
1
|
+
exports.GetValidFileList = exports.GetHeaderInfo = exports.GetTsStringFromFileList = exports.GetTsString = exports.Convert = void 0;
|
|
2
|
+
var path = require("path");
|
|
3
|
+
var fs = require("fs");
|
|
4
|
+
var d3 = require("d3");
|
|
5
|
+
var changeCase = require("change-case");
|
|
6
|
+
var toml = require('toml');
|
|
7
|
+
var packageJson = require('../package.json');
|
|
8
|
+
var EnumStr = 'Enum';
|
|
9
|
+
var EnumArrayString = 'Enum[]';
|
|
10
|
+
var IndexStr = 'Index';
|
|
11
|
+
|
|
12
|
+
exports.Convert = {
|
|
13
|
+
ini: function (str, moduleName) {
|
|
14
|
+
var obj = toml.parse(str);
|
|
15
|
+
return "export const ".concat(moduleName, " = ").concat(JSON.stringify(obj, null, 4), ";");
|
|
16
|
+
},
|
|
17
|
+
csv: function (str, moduleName) {
|
|
18
|
+
return csv2ts(str, moduleName);
|
|
19
|
+
},
|
|
20
|
+
toml: function (str, moduleName) {
|
|
21
|
+
var obj = toml.parse(str);
|
|
22
|
+
return "export const ".concat(moduleName, " = ").concat(JSON.stringify(obj, null, 4), ";");
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
function csv2ts(csvString, moduleName) {
|
|
26
|
+
var convert = {};
|
|
27
|
+
var result = d3.csvParse(csvString, function (d, i) {
|
|
28
|
+
if (i === 0) {
|
|
29
|
+
convert = d;
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
for (var k in d) {
|
|
34
|
+
if (global[convert[k]] instanceof Function) {
|
|
35
|
+
d[k] = global[convert[k]](d[k]);
|
|
36
|
+
} else {
|
|
37
|
+
// console.log(`convert[k]: ${convert[k]}, d[k]: ${d[k]}, k: ${k}`)
|
|
38
|
+
if (convert[k] === 'String[]') {
|
|
39
|
+
d[k] = d[k].trim().split(',');
|
|
40
|
+
} else if (convert[k] === 'Number[]') {
|
|
41
|
+
d[k] = d[k].trim().split(',').map((val) => Number(val));
|
|
42
|
+
} else if (convert[k] === 'Enum[]') {
|
|
43
|
+
d[k] = d[k].trim().split(',');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return d;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
// 删除columns属性
|
|
51
|
+
delete result.columns;
|
|
52
|
+
if (result.length <= 0) {
|
|
53
|
+
return "";
|
|
54
|
+
}
|
|
55
|
+
var template = "export namespace ".concat(moduleName, " {\n\n");
|
|
56
|
+
// generate enum type
|
|
57
|
+
for (var field in convert) {
|
|
58
|
+
if (convert[field] == EnumStr) {
|
|
59
|
+
let enumValues = [];
|
|
60
|
+
for (const row of result) {
|
|
61
|
+
let enumValue = row[field];
|
|
62
|
+
if (!enumValues.includes(enumValue)) {
|
|
63
|
+
enumValues.push(enumValue);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
enumValues = enumValues.map((value) => {return `"${value}"`});
|
|
68
|
+
template += ` export type ${field} = ${enumValues.join(' | ')};\n`;
|
|
69
|
+
// general enum list for length test
|
|
70
|
+
template += ` export const ${field}List: ${field}[] = [${enumValues.join(', ')}];\n\n`;
|
|
71
|
+
} else if (convert[field] == EnumArrayString) {
|
|
72
|
+
let enumValues = [];
|
|
73
|
+
for (const row of result) {
|
|
74
|
+
let enumArrayValue = row[field];
|
|
75
|
+
for (const enumValue of enumArrayValue) {
|
|
76
|
+
if (!enumValues.includes(enumValue)) {
|
|
77
|
+
enumValues.push(enumValue);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
enumValues = enumValues.map((value) => {return `"${value}"`});
|
|
83
|
+
template += ` export type ${field} = ${enumValues.join(' | ')};\n`;
|
|
84
|
+
// general enum list for length test
|
|
85
|
+
template += ` export const ${field}List: ${field}[] = [${enumValues.join(', ')}];\n\n`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// generate interface
|
|
90
|
+
template += " export interface Record {\n";
|
|
91
|
+
var indexField = null;
|
|
92
|
+
for (var field in convert) {
|
|
93
|
+
// var fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
|
|
94
|
+
// console.log(`generate interface field: ${field}, convert[field]: ${convert[field]}`);
|
|
95
|
+
|
|
96
|
+
var fieldType = 'string';
|
|
97
|
+
|
|
98
|
+
if (convert[field] != '') {
|
|
99
|
+
if (convert[field] == EnumStr) {
|
|
100
|
+
fieldType = field;
|
|
101
|
+
} else if (convert[field] == IndexStr) {
|
|
102
|
+
indexField = field;
|
|
103
|
+
} else if (convert[field] == EnumArrayString) {
|
|
104
|
+
fieldType = field + '[]';
|
|
105
|
+
} else {
|
|
106
|
+
fieldType = convert[field].toLowerCase();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
template += " ".concat(field, ": ").concat(fieldType, ";\n");
|
|
111
|
+
}
|
|
112
|
+
template += ' };\n\n';
|
|
113
|
+
// generate table
|
|
114
|
+
template += " export const List: Record[] = ";
|
|
115
|
+
// console.log(prettyFormat(result, op));
|
|
116
|
+
template += JSON.stringify(result, null, 4).replace(/\n/g, '\n ');
|
|
117
|
+
template += ';\n\n';
|
|
118
|
+
if (indexField != null) {
|
|
119
|
+
template += " export const Map: { [id: string]: Record } = {};\n";
|
|
120
|
+
template += ` for (const v of List) { Map[v.${indexField}] = v; };\n\n`;
|
|
121
|
+
}
|
|
122
|
+
template += "};";
|
|
123
|
+
return template;
|
|
124
|
+
}
|
|
125
|
+
function GetFileExt(filePath) {
|
|
126
|
+
var pathObject = path.parse(filePath);
|
|
127
|
+
return pathObject.ext.slice(1);
|
|
128
|
+
}
|
|
129
|
+
function GetTsString(filePath) {
|
|
130
|
+
var pathObject = path.parse(filePath);
|
|
131
|
+
var handle = exports.Convert[GetFileExt(filePath)];
|
|
132
|
+
if (handle) {
|
|
133
|
+
var moduleName = changeCase.pascalCase(pathObject.base);
|
|
134
|
+
var fileString = fs.readFileSync(filePath).toString();
|
|
135
|
+
if (fileString.charAt(0) === '\uFEFF') fileString = fileString.substr(1);
|
|
136
|
+
return handle(fileString, moduleName);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
return '';
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.GetTsString = GetTsString;
|
|
143
|
+
function GetTsStringFromFileList(fileList) {
|
|
144
|
+
return fileList.map(function (filePath) {
|
|
145
|
+
return GetTsString(filePath);
|
|
146
|
+
}).join('\n\n');
|
|
147
|
+
}
|
|
148
|
+
exports.GetTsStringFromFileList = GetTsStringFromFileList;
|
|
149
|
+
function GetHeaderInfo() {
|
|
150
|
+
var header = "export let config2ts_version = \"".concat(packageJson.version, "\";\n\n");
|
|
151
|
+
header += "export let config2ts_build_timestamp = ".concat(Date.now(), ";\n\n");
|
|
152
|
+
return header;
|
|
153
|
+
}
|
|
154
|
+
exports.GetHeaderInfo = GetHeaderInfo;
|
|
155
|
+
function GetValidFileList(fileList) {
|
|
156
|
+
return fileList.filter(function (filePath) {
|
|
157
|
+
return exports.Convert[GetFileExt(filePath)] != null;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
exports.GetValidFileList = GetValidFileList;
|
package/tests/config2ts.spec.js
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
|
-
var fs = require("fs");
|
|
2
|
-
var config2ts = require("../src/config2ts");
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
expect(config2ts.GetTsString('./config/
|
|
12
|
-
});
|
|
13
|
-
test('convert
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
var fs = require("fs");
|
|
2
|
+
var config2ts = require("../src/config2ts");
|
|
3
|
+
|
|
4
|
+
var reg = /[\t\r\f\n\s]*/g;
|
|
5
|
+
function getTsFileString(tsFilePath) {
|
|
6
|
+
return fs.readFileSync(tsFilePath).toString().replace(reg, '');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
test('convert csv file with id', function () {
|
|
10
|
+
console.log(config2ts.GetTsString('./config/data.csv'))
|
|
11
|
+
expect(config2ts.GetTsString('./config/data.csv').replace(reg, '')).toBe(getTsFileString('./config/data.csv.ts'));
|
|
12
|
+
});
|
|
13
|
+
test('convert csv file without id', function () {
|
|
14
|
+
expect(config2ts.GetTsString('./config/no-id.csv').replace(reg, '')).toBe(getTsFileString('./config/no-id.csv.ts'));
|
|
15
|
+
});
|
|
16
|
+
test('convert ini file', function () {
|
|
17
|
+
expect(config2ts.GetTsString('./config/ini-file.ini').replace(reg, '')).toBe(getTsFileString('./config/ini-file.ini.ts'));
|
|
18
|
+
});
|
|
19
|
+
test('convert toml file', function () {
|
|
20
|
+
expect(config2ts.GetTsString('./config/toml_file.toml').replace(reg, '')).toBe(getTsFileString('./config/toml_file.toml.ts'));
|
|
21
|
+
});
|
|
22
|
+
test('config2ts merge csv file list', function () {
|
|
23
|
+
expect(config2ts.GetTsStringFromFileList(["./config/data.csv", "./config/ini-file.ini", "./config/no-id.csv", "./config/toml_file.toml"]).replace(reg, '')).toBe(fs.readFileSync('./config/total.ts').toString().replace(/\s/g, ''));
|
|
24
|
+
});
|
|
25
|
+
test('config2ts GetValidFileList', function () {
|
|
26
|
+
expect(config2ts.GetValidFileList(['a.csv', 'b.ini', 'c.xxx'])).toEqual(['a.csv', 'b.ini']);
|
|
27
|
+
});
|