config2ts 2.1.6 → 2.2.1
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 +6 -6
- package/config/data.csv.ts +42 -4
- package/config/total.ts +61 -4
- package/package.json +1 -1
- package/src/config2ts.js +10 -1
- package/src/main.js +43 -43
- package/tests/config2ts.spec.js +1 -1
package/config/data.csv
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
id,name,bool,num,mytype
|
|
2
|
-
String,String,Boolean,Number,Enum
|
|
3
|
-
1,xxx,1,111,type1
|
|
4
|
-
2,xxx,,222,type2
|
|
5
|
-
3,333,,33,
|
|
6
|
-
4,444,0,444,type2
|
|
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"
|
package/config/data.csv.ts
CHANGED
|
@@ -7,6 +7,8 @@ export namespace DataCsv {
|
|
|
7
7
|
bool: boolean;
|
|
8
8
|
num: number;
|
|
9
9
|
mytype: mytype;
|
|
10
|
+
stringarray: string[];
|
|
11
|
+
numberarray: number[];
|
|
10
12
|
};
|
|
11
13
|
|
|
12
14
|
export const List: Record[] = [
|
|
@@ -15,28 +17,64 @@ export namespace DataCsv {
|
|
|
15
17
|
"name": "xxx",
|
|
16
18
|
"bool": true,
|
|
17
19
|
"num": 111,
|
|
18
|
-
"mytype": "type1"
|
|
20
|
+
"mytype": "type1",
|
|
21
|
+
"stringarray": [
|
|
22
|
+
"1",
|
|
23
|
+
" 2",
|
|
24
|
+
" 3"
|
|
25
|
+
],
|
|
26
|
+
"numberarray": [
|
|
27
|
+
1,
|
|
28
|
+
2,
|
|
29
|
+
3
|
|
30
|
+
]
|
|
19
31
|
},
|
|
20
32
|
{
|
|
21
33
|
"id": "2",
|
|
22
34
|
"name": "xxx",
|
|
23
35
|
"bool": false,
|
|
24
36
|
"num": 222,
|
|
25
|
-
"mytype": "type2"
|
|
37
|
+
"mytype": "type2",
|
|
38
|
+
"stringarray": [
|
|
39
|
+
"a",
|
|
40
|
+
"b"
|
|
41
|
+
],
|
|
42
|
+
"numberarray": [
|
|
43
|
+
0
|
|
44
|
+
]
|
|
26
45
|
},
|
|
27
46
|
{
|
|
28
47
|
"id": "3",
|
|
29
48
|
"name": "333",
|
|
30
49
|
"bool": false,
|
|
31
50
|
"num": 33,
|
|
32
|
-
"mytype": ""
|
|
51
|
+
"mytype": "",
|
|
52
|
+
"stringarray": [
|
|
53
|
+
""
|
|
54
|
+
],
|
|
55
|
+
"numberarray": [
|
|
56
|
+
1,
|
|
57
|
+
2
|
|
58
|
+
]
|
|
33
59
|
},
|
|
34
60
|
{
|
|
35
61
|
"id": "4",
|
|
36
62
|
"name": "444",
|
|
37
63
|
"bool": true,
|
|
38
64
|
"num": 444,
|
|
39
|
-
"mytype": "type2"
|
|
65
|
+
"mytype": "type2",
|
|
66
|
+
"stringarray": [
|
|
67
|
+
"",
|
|
68
|
+
"1",
|
|
69
|
+
"",
|
|
70
|
+
"b"
|
|
71
|
+
],
|
|
72
|
+
"numberarray": [
|
|
73
|
+
0,
|
|
74
|
+
1,
|
|
75
|
+
0,
|
|
76
|
+
3
|
|
77
|
+
]
|
|
40
78
|
}
|
|
41
79
|
];
|
|
42
80
|
|
package/config/total.ts
CHANGED
|
@@ -7,6 +7,8 @@ export namespace DataCsv {
|
|
|
7
7
|
bool: boolean;
|
|
8
8
|
num: number;
|
|
9
9
|
mytype: mytype;
|
|
10
|
+
stringarray: string[];
|
|
11
|
+
numberarray: number[];
|
|
10
12
|
};
|
|
11
13
|
|
|
12
14
|
export const List: Record[] = [
|
|
@@ -15,28 +17,64 @@ export namespace DataCsv {
|
|
|
15
17
|
"name": "xxx",
|
|
16
18
|
"bool": true,
|
|
17
19
|
"num": 111,
|
|
18
|
-
"mytype": "type1"
|
|
20
|
+
"mytype": "type1",
|
|
21
|
+
"stringarray": [
|
|
22
|
+
"1",
|
|
23
|
+
" 2",
|
|
24
|
+
" 3"
|
|
25
|
+
],
|
|
26
|
+
"numberarray": [
|
|
27
|
+
1,
|
|
28
|
+
2,
|
|
29
|
+
3
|
|
30
|
+
]
|
|
19
31
|
},
|
|
20
32
|
{
|
|
21
33
|
"id": "2",
|
|
22
34
|
"name": "xxx",
|
|
23
35
|
"bool": false,
|
|
24
36
|
"num": 222,
|
|
25
|
-
"mytype": "type2"
|
|
37
|
+
"mytype": "type2",
|
|
38
|
+
"stringarray": [
|
|
39
|
+
"a",
|
|
40
|
+
"b"
|
|
41
|
+
],
|
|
42
|
+
"numberarray": [
|
|
43
|
+
0
|
|
44
|
+
]
|
|
26
45
|
},
|
|
27
46
|
{
|
|
28
47
|
"id": "3",
|
|
29
48
|
"name": "333",
|
|
30
49
|
"bool": false,
|
|
31
50
|
"num": 33,
|
|
32
|
-
"mytype": ""
|
|
51
|
+
"mytype": "",
|
|
52
|
+
"stringarray": [
|
|
53
|
+
""
|
|
54
|
+
],
|
|
55
|
+
"numberarray": [
|
|
56
|
+
1,
|
|
57
|
+
2
|
|
58
|
+
]
|
|
33
59
|
},
|
|
34
60
|
{
|
|
35
61
|
"id": "4",
|
|
36
62
|
"name": "444",
|
|
37
63
|
"bool": true,
|
|
38
64
|
"num": 444,
|
|
39
|
-
"mytype": "type2"
|
|
65
|
+
"mytype": "type2",
|
|
66
|
+
"stringarray": [
|
|
67
|
+
"",
|
|
68
|
+
"1",
|
|
69
|
+
"",
|
|
70
|
+
"b"
|
|
71
|
+
],
|
|
72
|
+
"numberarray": [
|
|
73
|
+
0,
|
|
74
|
+
1,
|
|
75
|
+
0,
|
|
76
|
+
3
|
|
77
|
+
]
|
|
40
78
|
}
|
|
41
79
|
];
|
|
42
80
|
|
|
@@ -45,6 +83,16 @@ export namespace DataCsv {
|
|
|
45
83
|
|
|
46
84
|
};
|
|
47
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
|
+
|
|
48
96
|
export namespace NoIdCsv {
|
|
49
97
|
|
|
50
98
|
export interface Record {
|
|
@@ -71,4 +119,13 @@ export namespace NoIdCsv {
|
|
|
71
119
|
}
|
|
72
120
|
];
|
|
73
121
|
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const TomlFileToml = {
|
|
125
|
+
"num": 1,
|
|
126
|
+
"str": "test",
|
|
127
|
+
"ItemType": {
|
|
128
|
+
"book": 1,
|
|
129
|
+
"fruit": 2
|
|
130
|
+
}
|
|
74
131
|
};
|
package/package.json
CHANGED
package/src/config2ts.js
CHANGED
|
@@ -31,6 +31,13 @@ function csv2ts(csvString, moduleName) {
|
|
|
31
31
|
for (var k in d) {
|
|
32
32
|
if (global[convert[k]] instanceof Function) {
|
|
33
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
|
+
}
|
|
34
41
|
}
|
|
35
42
|
}
|
|
36
43
|
return d;
|
|
@@ -97,7 +104,9 @@ function GetTsString(filePath) {
|
|
|
97
104
|
var handle = exports.Convert[GetFileExt(filePath)];
|
|
98
105
|
if (handle) {
|
|
99
106
|
var moduleName = changeCase.pascalCase(pathObject.base);
|
|
100
|
-
|
|
107
|
+
var fileString = fs.readFileSync(filePath).toString();
|
|
108
|
+
if (fileString.charAt(0) === '\uFEFF') fileString = fileString.substr(1);
|
|
109
|
+
return handle(fileString, moduleName);
|
|
101
110
|
}
|
|
102
111
|
else {
|
|
103
112
|
return '';
|
package/src/main.js
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
var fs = require("fs");
|
|
2
|
-
var path = require("path");
|
|
3
|
-
var program = require("commander");
|
|
4
|
-
var config2ts = require("./config2ts");
|
|
5
|
-
program.version(require(path.join(__dirname, '..', 'package.json'))['version']);
|
|
6
|
-
program
|
|
7
|
-
.option('-d, --dir <path>', 'set convert path. default: ./')
|
|
8
|
-
.option('-o, --outDir <path>', 'set outDir path. default: ./')
|
|
9
|
-
.option('-m, --merge <name>', 'merge all to one ts file.')
|
|
10
|
-
.parse(process.argv);
|
|
11
|
-
|
|
12
|
-
const options = program.opts();
|
|
13
|
-
console.log(options);
|
|
14
|
-
|
|
15
|
-
var dir = options['dir'] || '.';
|
|
16
|
-
dir = path.resolve(dir);
|
|
17
|
-
console.log('dir:', dir);
|
|
18
|
-
|
|
19
|
-
var outDir = options['outDir'] || dir;
|
|
20
|
-
outDir = path.resolve(outDir);
|
|
21
|
-
console.log('outDir:', outDir);
|
|
22
|
-
if (!fs.existsSync(outDir)) {
|
|
23
|
-
fs.mkdirSync(outDir, {recursive: true});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
var merge = options['merge'];
|
|
27
|
-
var fileList = fs.readdirSync(dir);
|
|
28
|
-
if (merge) {
|
|
29
|
-
fileList = config2ts.GetValidFileList(fileList).map(function (filename) {
|
|
30
|
-
return path.join(dir, filename);
|
|
31
|
-
});
|
|
32
|
-
var mergeFile = path.join(outDir, merge);
|
|
33
|
-
fs.writeFileSync(mergeFile, config2ts.
|
|
34
|
-
console.log("config2ts, ".concat(fileList.length, " config files, merge into: ").concat(mergeFile));
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
config2ts.GetValidFileList(fileList).forEach(function (filename) {
|
|
38
|
-
var target = path.join(outDir, "".concat(filename, ".ts"));
|
|
39
|
-
fs.writeFileSync(target, config2ts.GetTsString(path.join(dir, filename)));
|
|
40
|
-
console.log("config2ts, convert: ".concat(filename, " , to: ").concat(target));
|
|
41
|
-
});
|
|
42
|
-
console.log('config2ts convert done!');
|
|
43
|
-
}
|
|
1
|
+
var fs = require("fs");
|
|
2
|
+
var path = require("path");
|
|
3
|
+
var program = require("commander");
|
|
4
|
+
var config2ts = require("./config2ts");
|
|
5
|
+
program.version(require(path.join(__dirname, '..', 'package.json'))['version']);
|
|
6
|
+
program
|
|
7
|
+
.option('-d, --dir <path>', 'set convert path. default: ./')
|
|
8
|
+
.option('-o, --outDir <path>', 'set outDir path. default: ./')
|
|
9
|
+
.option('-m, --merge <name>', 'merge all to one ts file.')
|
|
10
|
+
.parse(process.argv);
|
|
11
|
+
|
|
12
|
+
const options = program.opts();
|
|
13
|
+
console.log(options);
|
|
14
|
+
|
|
15
|
+
var dir = options['dir'] || '.';
|
|
16
|
+
dir = path.resolve(dir);
|
|
17
|
+
console.log('dir:', dir);
|
|
18
|
+
|
|
19
|
+
var outDir = options['outDir'] || dir;
|
|
20
|
+
outDir = path.resolve(outDir);
|
|
21
|
+
console.log('outDir:', outDir);
|
|
22
|
+
if (!fs.existsSync(outDir)) {
|
|
23
|
+
fs.mkdirSync(outDir, {recursive: true});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var merge = options['merge'];
|
|
27
|
+
var fileList = fs.readdirSync(dir);
|
|
28
|
+
if (merge) {
|
|
29
|
+
fileList = config2ts.GetValidFileList(fileList).map(function (filename) {
|
|
30
|
+
return path.join(dir, filename);
|
|
31
|
+
});
|
|
32
|
+
var mergeFile = path.join(outDir, merge);
|
|
33
|
+
fs.writeFileSync(mergeFile, config2ts.GetTsStringFromFileList(fileList), {encoding: 'utf-8'});
|
|
34
|
+
console.log("config2ts, ".concat(fileList.length, " config files, merge into: ").concat(mergeFile));
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
config2ts.GetValidFileList(fileList).forEach(function (filename) {
|
|
38
|
+
var target = path.join(outDir, "".concat(filename, ".ts"));
|
|
39
|
+
fs.writeFileSync(target, config2ts.GetTsString(path.join(dir, filename)), {encoding: 'utf-8'});
|
|
40
|
+
console.log("config2ts, convert: ".concat(filename, " , to: ").concat(target));
|
|
41
|
+
});
|
|
42
|
+
console.log('config2ts convert done!');
|
|
43
|
+
}
|
package/tests/config2ts.spec.js
CHANGED
|
@@ -15,7 +15,7 @@ test('convert toml file', function () {
|
|
|
15
15
|
expect(config2ts.GetTsString('./config/toml_file.toml')).toBe(fs.readFileSync('./config/toml_file.toml.ts').toString());
|
|
16
16
|
});
|
|
17
17
|
test('config2ts merge csv file list', function () {
|
|
18
|
-
expect(config2ts.GetTsStringFromFileList(["./config/data.csv", "./config/no-id.csv"])).toBe(fs.readFileSync('./config/total.ts').toString());
|
|
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
19
|
});
|
|
20
20
|
test('config2ts merge csv file list', function () {
|
|
21
21
|
expect(config2ts.GetValidFileList(['a.csv', 'b.ini', 'c.xxx'])).toEqual(['a.csv', 'b.ini']);
|