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