config2ts 2.2.1 → 2.2.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/config/data.csv +1 -1
- package/package.json +1 -1
- package/src/config2ts.js +137 -133
- package/tests/config2ts.spec.js +22 -22
package/config/data.csv
CHANGED
package/package.json
CHANGED
package/src/config2ts.js
CHANGED
|
@@ -1,133 +1,137 @@
|
|
|
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
|
-
var
|
|
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
|
-
var fieldType = 'string';
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
template +=
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return
|
|
101
|
-
}
|
|
102
|
-
function
|
|
103
|
-
var pathObject = path.parse(filePath);
|
|
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
|
-
return
|
|
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 IndexStr = 'Index';
|
|
10
|
+
|
|
11
|
+
exports.Convert = {
|
|
12
|
+
ini: function (str, moduleName) {
|
|
13
|
+
var obj = toml.parse(str);
|
|
14
|
+
return "export const ".concat(moduleName, " = ").concat(JSON.stringify(obj, null, 4), ";");
|
|
15
|
+
},
|
|
16
|
+
csv: function (str, moduleName) {
|
|
17
|
+
return csv2ts(str, moduleName);
|
|
18
|
+
},
|
|
19
|
+
toml: function (str, moduleName) {
|
|
20
|
+
var obj = toml.parse(str);
|
|
21
|
+
return "export const ".concat(moduleName, " = ").concat(JSON.stringify(obj, null, 4), ";");
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
function csv2ts(csvString, moduleName) {
|
|
25
|
+
var convert = {};
|
|
26
|
+
var result = d3.csvParse(csvString, function (d, i) {
|
|
27
|
+
if (i === 0) {
|
|
28
|
+
convert = d;
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
for (var k in d) {
|
|
33
|
+
if (global[convert[k]] instanceof Function) {
|
|
34
|
+
d[k] = global[convert[k]](d[k]);
|
|
35
|
+
} else {
|
|
36
|
+
// console.log(`convert[k]: ${convert[k]}, d[k]: ${d[k]}, k: ${k}`)
|
|
37
|
+
if (convert[k] === 'String[]') {
|
|
38
|
+
d[k] = d[k].trim().split(',');
|
|
39
|
+
} else if (convert[k] === 'Number[]') {
|
|
40
|
+
d[k] = d[k].trim().split(',').map((val) => Number(val));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return d;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
// 删除columns属性
|
|
48
|
+
delete result.columns;
|
|
49
|
+
if (result.length <= 0) {
|
|
50
|
+
return "";
|
|
51
|
+
}
|
|
52
|
+
var template = "export namespace ".concat(moduleName, " {\n\n");
|
|
53
|
+
// generate enum type
|
|
54
|
+
for (var field in convert) {
|
|
55
|
+
if (convert[field] == EnumStr) {
|
|
56
|
+
let enumValues = [];
|
|
57
|
+
for (const row of result) {
|
|
58
|
+
let enumValue = row[field];
|
|
59
|
+
if (!enumValues.includes(enumValue)) {
|
|
60
|
+
enumValues.push(enumValue);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
enumValues = enumValues.map((value) => {return `"${value}"`});
|
|
65
|
+
template += ` export type ${field} = ${enumValues.join(' | ')};\n`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// generate interface
|
|
70
|
+
template += " export interface Record {\n";
|
|
71
|
+
var indexField = null;
|
|
72
|
+
for (var field in convert) {
|
|
73
|
+
// var fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
|
|
74
|
+
|
|
75
|
+
var fieldType = 'string';
|
|
76
|
+
|
|
77
|
+
if (convert[field] != '') {
|
|
78
|
+
if (convert[field] == EnumStr) {
|
|
79
|
+
fieldType = field;
|
|
80
|
+
} else if (convert[field] == IndexStr) {
|
|
81
|
+
indexField = field;
|
|
82
|
+
} else {
|
|
83
|
+
fieldType = convert[field].toLowerCase();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
template += " ".concat(field, ": ").concat(fieldType, ";\n");
|
|
88
|
+
}
|
|
89
|
+
template += ' };\n\n';
|
|
90
|
+
// generate table
|
|
91
|
+
template += " export const List: Record[] = ";
|
|
92
|
+
// console.log(prettyFormat(result, op));
|
|
93
|
+
template += JSON.stringify(result, null, 4).replace(/\n/g, '\n ');
|
|
94
|
+
template += ';\n\n';
|
|
95
|
+
if (indexField != null) {
|
|
96
|
+
template += " export const Map: { [id: string]: Record } = {};\n";
|
|
97
|
+
template += ` for (const v of List) { Map[v.${indexField}] = v; };\n\n`;
|
|
98
|
+
}
|
|
99
|
+
template += "};";
|
|
100
|
+
return template;
|
|
101
|
+
}
|
|
102
|
+
function GetFileExt(filePath) {
|
|
103
|
+
var pathObject = path.parse(filePath);
|
|
104
|
+
return pathObject.ext.slice(1);
|
|
105
|
+
}
|
|
106
|
+
function GetTsString(filePath) {
|
|
107
|
+
var pathObject = path.parse(filePath);
|
|
108
|
+
var handle = exports.Convert[GetFileExt(filePath)];
|
|
109
|
+
if (handle) {
|
|
110
|
+
var moduleName = changeCase.pascalCase(pathObject.base);
|
|
111
|
+
var fileString = fs.readFileSync(filePath).toString();
|
|
112
|
+
if (fileString.charAt(0) === '\uFEFF') fileString = fileString.substr(1);
|
|
113
|
+
return handle(fileString, moduleName);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
return '';
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.GetTsString = GetTsString;
|
|
120
|
+
function GetTsStringFromFileList(fileList) {
|
|
121
|
+
return fileList.map(function (filePath) {
|
|
122
|
+
return GetTsString(filePath);
|
|
123
|
+
}).join('\n\n');
|
|
124
|
+
}
|
|
125
|
+
exports.GetTsStringFromFileList = GetTsStringFromFileList;
|
|
126
|
+
function GetHeaderInfo() {
|
|
127
|
+
var header = "export let config2ts_version = \"".concat(packageJson.version, "\";\n\n");
|
|
128
|
+
header += "export let config2ts_build_timestamp = ".concat(Date.now(), ";\n\n");
|
|
129
|
+
return header;
|
|
130
|
+
}
|
|
131
|
+
exports.GetHeaderInfo = GetHeaderInfo;
|
|
132
|
+
function GetValidFileList(fileList) {
|
|
133
|
+
return fileList.filter(function (filePath) {
|
|
134
|
+
return exports.Convert[GetFileExt(filePath)] != null;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
exports.GetValidFileList = GetValidFileList;
|
package/tests/config2ts.spec.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
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
|
+
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
|
+
});
|