config2ts 2.1.5 → 2.2.0

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
@@ -14,7 +14,7 @@ config2ts folder contains csv
14
14
  -h, --help output usage information
15
15
  -V, --version output the version number
16
16
  -d, --dir <path> set convert path. default: ./
17
- -o, --outDir <path> set outDir path. default: same as dir
17
+ -o, --outDir <path> set outDir path. default: ./
18
18
  -p, --prefix <prefix> set interface prefix. default: ""
19
19
  -s, --suffix <suffix> set interface suffix. default: "Csv"
20
20
  -m, --merge <name> merge all to one ts file. default: csv.ts
package/bin/config2ts CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- require('../dist/main');
2
+ require('../src/main');
package/config/data.csv CHANGED
@@ -1,5 +1,6 @@
1
- id,name,bool,num
2
- String,String,Boolean,Number
3
- 1,"xxx",1,111
4
- 2,"xxx",,222
5
- 3,333,,
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,10 +1,14 @@
1
1
  export namespace DataCsv {
2
2
 
3
+ export type mytype = "type1" | "type2" | "";
3
4
  export interface Record {
4
5
  id: string;
5
6
  name: string;
6
7
  bool: boolean;
7
8
  num: number;
9
+ mytype: mytype;
10
+ stringarray: string[];
11
+ numberarray: number[];
8
12
  };
9
13
 
10
14
  export const List: Record[] = [
@@ -12,19 +16,65 @@ export namespace DataCsv {
12
16
  "id": "1",
13
17
  "name": "xxx",
14
18
  "bool": true,
15
- "num": 111
19
+ "num": 111,
20
+ "mytype": "type1",
21
+ "stringarray": [
22
+ "1",
23
+ " 2",
24
+ " 3"
25
+ ],
26
+ "numberarray": [
27
+ 1,
28
+ 2,
29
+ 3
30
+ ]
16
31
  },
17
32
  {
18
33
  "id": "2",
19
34
  "name": "xxx",
20
35
  "bool": false,
21
- "num": 222
36
+ "num": 222,
37
+ "mytype": "type2",
38
+ "stringarray": [
39
+ "a",
40
+ "b"
41
+ ],
42
+ "numberarray": [
43
+ 0
44
+ ]
22
45
  },
23
46
  {
24
47
  "id": "3",
25
48
  "name": "333",
26
49
  "bool": false,
27
- "num": 0
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
+ ]
28
78
  }
29
79
  ];
30
80
 
package/config/total.ts CHANGED
@@ -1,10 +1,14 @@
1
1
  export namespace DataCsv {
2
2
 
3
+ export type mytype = "type1" | "type2" | "";
3
4
  export interface Record {
4
5
  id: string;
5
6
  name: string;
6
7
  bool: boolean;
7
8
  num: number;
9
+ mytype: mytype;
10
+ stringarray: string[];
11
+ numberarray: number[];
8
12
  };
9
13
 
10
14
  export const List: Record[] = [
@@ -12,19 +16,65 @@ export namespace DataCsv {
12
16
  "id": "1",
13
17
  "name": "xxx",
14
18
  "bool": true,
15
- "num": 111
19
+ "num": 111,
20
+ "mytype": "type1",
21
+ "stringarray": [
22
+ "1",
23
+ " 2",
24
+ " 3"
25
+ ],
26
+ "numberarray": [
27
+ 1,
28
+ 2,
29
+ 3
30
+ ]
16
31
  },
17
32
  {
18
33
  "id": "2",
19
34
  "name": "xxx",
20
35
  "bool": false,
21
- "num": 222
36
+ "num": 222,
37
+ "mytype": "type2",
38
+ "stringarray": [
39
+ "a",
40
+ "b"
41
+ ],
42
+ "numberarray": [
43
+ 0
44
+ ]
22
45
  },
23
46
  {
24
47
  "id": "3",
25
48
  "name": "333",
26
49
  "bool": false,
27
- "num": 0
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
+ ]
28
78
  }
29
79
  ];
30
80
 
@@ -33,6 +83,16 @@ export namespace DataCsv {
33
83
 
34
84
  };
35
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
+
36
96
  export namespace NoIdCsv {
37
97
 
38
98
  export interface Record {
@@ -59,4 +119,13 @@ export namespace NoIdCsv {
59
119
  }
60
120
  ];
61
121
 
122
+ };
123
+
124
+ export const TomlFileToml = {
125
+ "num": 1,
126
+ "str": "test",
127
+ "ItemType": {
128
+ "book": 1,
129
+ "fruit": 2
130
+ }
62
131
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.1.5",
3
+ "version": "2.2.0",
4
4
  "description": "convert config to ts file.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -32,10 +32,6 @@
32
32
  "toml": "^3.0.0"
33
33
  },
34
34
  "devDependencies": {
35
- "@types/ini": "^1.3.31",
36
- "@types/jest": "^29.1.2",
37
- "jest": "^29.5.0",
38
- "ts-jest": "^29.1.0",
39
- "typescript": "^4.9.5"
35
+ "jest": "^29.5.0"
40
36
  }
41
37
  }
@@ -0,0 +1,131 @@
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
+ return handle(fs.readFileSync(filePath).toString(), moduleName);
108
+ }
109
+ else {
110
+ return '';
111
+ }
112
+ }
113
+ exports.GetTsString = GetTsString;
114
+ function GetTsStringFromFileList(fileList) {
115
+ return fileList.map(function (filePath) {
116
+ return GetTsString(filePath);
117
+ }).join('\n\n');
118
+ }
119
+ exports.GetTsStringFromFileList = GetTsStringFromFileList;
120
+ function GetHeaderInfo() {
121
+ var header = "export let config2ts_version = \"".concat(packageJson.version, "\";\n\n");
122
+ header += "export let config2ts_build_timestamp = ".concat(Date.now(), ";\n\n");
123
+ return header;
124
+ }
125
+ exports.GetHeaderInfo = GetHeaderInfo;
126
+ function GetValidFileList(fileList) {
127
+ return fileList.filter(function (filePath) {
128
+ return exports.Convert[GetFileExt(filePath)] != null;
129
+ });
130
+ }
131
+ exports.GetValidFileList = GetValidFileList;
package/src/main.js ADDED
@@ -0,0 +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.GetTsStringFromFileList(fileList));
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
+ }
@@ -0,0 +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
+ });
package/dist/config2ts.js DELETED
@@ -1,97 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GetValidFileList = exports.GetHeaderInfo = exports.GetTsStringFromFileList = exports.GetTsString = exports.Convert = void 0;
4
- const path = require("path");
5
- const fs = require("fs");
6
- const d3 = require("d3");
7
- const changeCase = require("change-case");
8
- let toml = require('toml');
9
- let packageJson = require('../package.json');
10
- exports.Convert = {
11
- ini: function (str, moduleName) {
12
- let obj = toml.parse(str);
13
- return `export const ${moduleName} = ${JSON.stringify(obj, null, 4)};`;
14
- },
15
- csv: function (str, moduleName) {
16
- return csv2ts(str, moduleName);
17
- },
18
- toml: function (str, moduleName) {
19
- let obj = toml.parse(str);
20
- return `export const ${moduleName} = ${JSON.stringify(obj, null, 4)};`;
21
- }
22
- };
23
- function csv2ts(csvString, moduleName) {
24
- let convert = {};
25
- let result = d3.csvParse(csvString, (d, i) => {
26
- if (i === 0) {
27
- convert = d;
28
- return null;
29
- }
30
- else {
31
- for (let k in d) {
32
- if (global[convert[k]] instanceof Function) {
33
- d[k] = global[convert[k]](d[k]);
34
- }
35
- }
36
- return d;
37
- }
38
- });
39
- // 删除columns属性
40
- delete result.columns;
41
- if (result.length <= 0) {
42
- return "";
43
- }
44
- let template = `export namespace ${moduleName} {\n\n`;
45
- // generate interface
46
- template += ` export interface Record {\n`;
47
- for (let field in convert) {
48
- let fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
49
- template += ` ${field}: ${fieldType};\n`;
50
- }
51
- template += ' };\n\n';
52
- // generate table
53
- template += ` export const List: Record[] = `;
54
- // console.log(prettyFormat(result, op));
55
- template += JSON.stringify(result, null, 4).replace(/\n/g, '\n ');
56
- template += ';\n\n';
57
- if (convert['id'] === 'String') {
58
- template += ` export const Map: { [id: string]: Record } = {};\n`;
59
- template += ` for (const v of List) { Map[v.id] = v; };\n\n`;
60
- }
61
- template += "};";
62
- return template;
63
- }
64
- function GetFileExt(filePath) {
65
- let pathObject = path.parse(filePath);
66
- return pathObject.ext.slice(1);
67
- }
68
- function GetTsString(filePath) {
69
- let pathObject = path.parse(filePath);
70
- let handle = exports.Convert[GetFileExt(filePath)];
71
- if (handle) {
72
- let moduleName = changeCase.pascalCase(pathObject.base);
73
- return handle(fs.readFileSync(filePath).toString(), moduleName);
74
- }
75
- else {
76
- return '';
77
- }
78
- }
79
- exports.GetTsString = GetTsString;
80
- function GetTsStringFromFileList(fileList) {
81
- return fileList.map((filePath) => {
82
- return GetTsString(filePath);
83
- }).join('\n\n');
84
- }
85
- exports.GetTsStringFromFileList = GetTsStringFromFileList;
86
- function GetHeaderInfo() {
87
- let header = `export let config2ts_version = "${packageJson.version}";\n\n`;
88
- header += `export let config2ts_build_timestamp = ${Date.now()};\n\n`;
89
- return header;
90
- }
91
- exports.GetHeaderInfo = GetHeaderInfo;
92
- function GetValidFileList(fileList) {
93
- return fileList.filter((filePath) => {
94
- return exports.Convert[GetFileExt(filePath)] != null;
95
- });
96
- }
97
- exports.GetValidFileList = GetValidFileList;
package/dist/main.js DELETED
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const fs = require("fs");
4
- const path = require("path");
5
- const program = require("commander");
6
- const config2ts_1 = require("./config2ts");
7
- program.version(require(path.join(__dirname, '..', 'package.json'))['version']);
8
- program
9
- .option('-d, --dir <path>', 'set convert path. default: ./')
10
- .option('-o, --outDir <path>', 'set outDir path. default: same as dir')
11
- .option('-m, --merge <name>', 'merge all to one ts file.')
12
- .parse(process.argv);
13
- let dir = program['dir'] || '.';
14
- let outDir = program['outDir'] || dir;
15
- let merge = program['merge'];
16
- let fileList = fs.readdirSync(dir);
17
- if (merge) {
18
- fileList = (0, config2ts_1.GetValidFileList)(fileList).map((filename) => {
19
- return path.join(dir, filename);
20
- });
21
- let mergeFile = path.join(outDir, merge);
22
- fs.writeFileSync(mergeFile, (0, config2ts_1.GetHeaderInfo)() + (0, config2ts_1.GetTsStringFromFileList)(fileList));
23
- console.log(`config2ts, ${fileList.length} config files, merge into: ${mergeFile}`);
24
- }
25
- else {
26
- (0, config2ts_1.GetValidFileList)(fileList).forEach((filename) => {
27
- let target = path.join(outDir, `${filename}.ts`);
28
- fs.writeFileSync(target, (0, config2ts_1.GetTsString)(path.join(dir, filename)));
29
- console.log(`config2ts, convert: ${filename} , to: ${target}`);
30
- });
31
- console.log('config2ts convert done!');
32
- }
package/jest.config.js DELETED
@@ -1,5 +0,0 @@
1
- /** @type {import('ts-jest').JestConfigWithTsJest} */
2
- module.exports = {
3
- preset: 'ts-jest',
4
- testEnvironment: 'node'
5
- };
package/src/config2ts.ts DELETED
@@ -1,110 +0,0 @@
1
- import * as path from 'path';
2
- import * as fs from 'fs';
3
- import * as d3 from 'd3';
4
- import * as changeCase from 'change-case';
5
- import * as ini from 'ini';
6
- let toml = require('toml');
7
-
8
- let packageJson = require('../package.json');
9
-
10
- export const Convert = {
11
- ini: function(str: string, moduleName: string) {
12
- let obj = toml.parse(str);
13
- return `export const ${moduleName} = ${JSON.stringify(obj, null, 4)};`;
14
- },
15
-
16
- csv: function(str: string, moduleName: string) {
17
- return csv2ts(str, moduleName);
18
- },
19
-
20
- toml: function(str: string, moduleName: string) {
21
- let obj = toml.parse(str);
22
- return `export const ${moduleName} = ${JSON.stringify(obj, null, 4)};`;
23
- }
24
- };
25
-
26
- function csv2ts(csvString: string, moduleName: string) {
27
- let convert = {};
28
- let result = d3.csvParse(csvString, (d, i) => {
29
- if (i === 0) {
30
- convert = d;
31
- return null;
32
- }
33
- else {
34
- for (let k in d) {
35
- if (global[convert[k]] instanceof Function) {
36
- d[k] = global[convert[k]](d[k]);
37
- }
38
- }
39
- return d;
40
- }
41
- });
42
-
43
- // 删除columns属性
44
- delete result.columns;
45
-
46
- if (result.length <= 0) {
47
- return "";
48
- }
49
-
50
- let template = `export namespace ${moduleName} {\n\n`;
51
-
52
- // generate interface
53
- template += ` export interface Record {\n`;
54
-
55
- for (let field in convert) {
56
- let fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
57
- template += ` ${field}: ${fieldType};\n`;
58
- }
59
- template += ' };\n\n';
60
-
61
- // generate table
62
- template += ` export const List: Record[] = `;
63
- // console.log(prettyFormat(result, op));
64
- template += JSON.stringify(result, null, 4).replace(/\n/g, '\n ');
65
- template += ';\n\n';
66
-
67
- if (convert['id'] === 'String') {
68
- template += ` export const Map: { [id: string]: Record } = {};\n`;
69
- template += ` for (const v of List) { Map[v.id] = v; };\n\n`;
70
- }
71
-
72
- template += "};"
73
-
74
- return template;
75
- }
76
-
77
- function GetFileExt(filePath: string) {
78
- let pathObject = path.parse(filePath);
79
- return pathObject.ext.slice(1);
80
- }
81
-
82
- export function GetTsString(filePath: string) {
83
- let pathObject = path.parse(filePath);
84
- let handle = Convert[GetFileExt(filePath)];
85
- if (handle) {
86
- let moduleName = changeCase.pascalCase(pathObject.base);
87
- return handle(fs.readFileSync(filePath).toString(), moduleName);
88
- }
89
- else {
90
- return '';
91
- }
92
- }
93
-
94
- export function GetTsStringFromFileList(fileList: string[]) {
95
- return fileList.map((filePath) => {
96
- return GetTsString(filePath);
97
- }).join('\n\n');
98
- }
99
-
100
- export function GetHeaderInfo() {
101
- let header = `export let config2ts_version = "${packageJson.version}";\n\n`;
102
- header += `export let config2ts_build_timestamp = ${Date.now()};\n\n`;
103
- return header;
104
- }
105
-
106
- export function GetValidFileList(fileList: string[]) {
107
- return fileList.filter((filePath) => {
108
- return Convert[GetFileExt(filePath)] != null;
109
- })
110
- }
package/src/main.ts DELETED
@@ -1,35 +0,0 @@
1
- import * as fs from 'fs';
2
- import * as path from 'path';
3
- import * as program from 'commander';
4
- import {GetTsString, GetHeaderInfo, GetValidFileList, GetTsStringFromFileList} from './config2ts';
5
-
6
- program.version(require(path.join(__dirname, '..', 'package.json'))['version']);
7
-
8
- program
9
- .option('-d, --dir <path>', 'set convert path. default: ./')
10
- .option('-o, --outDir <path>', 'set outDir path. default: same as dir')
11
- .option('-m, --merge <name>', 'merge all to one ts file.')
12
- .parse(process.argv);
13
-
14
- let dir = program['dir'] || '.';
15
- let outDir = program['outDir'] || dir;
16
- let merge = program['merge'];
17
-
18
- let fileList = fs.readdirSync(dir);
19
-
20
- if (merge) {
21
- fileList = GetValidFileList(fileList).map((filename) => {
22
- return path.join(dir, filename);
23
- });
24
- let mergeFile = path.join(outDir, merge);
25
- fs.writeFileSync(mergeFile, GetHeaderInfo() + GetTsStringFromFileList(fileList));
26
- console.log(`config2ts, ${fileList.length} config files, merge into: ${mergeFile}`);
27
- }
28
- else {
29
- GetValidFileList(fileList).forEach((filename) => {
30
- let target = path.join(outDir, `${filename}.ts`);
31
- fs.writeFileSync(target, GetTsString(path.join(dir, filename)));
32
- console.log(`config2ts, convert: ${filename} , to: ${target}`);
33
- });
34
- console.log('config2ts convert done!');
35
- }
@@ -1,36 +0,0 @@
1
- import * as fs from 'fs';
2
- import * as assert from 'assert';
3
- import * as ini from 'ini';
4
-
5
- import {
6
- Convert,
7
- GetTsString,
8
- GetHeaderInfo,
9
- GetTsStringFromFileList,
10
- GetValidFileList} from '../src/config2ts';
11
-
12
- test('convert csv file with id', () => {
13
- expect(GetTsString('./config/data.csv')).toBe(fs.readFileSync('./config/data.csv.ts').toString());
14
- });
15
-
16
- test('convert csv file without id', () => {
17
- expect(GetTsString('./config/no-id.csv')).toBe(fs.readFileSync('./config/no-id.csv.ts').toString());
18
- });
19
-
20
- test('convert ini file', () => {
21
- expect(GetTsString('./config/ini-file.ini')).toBe(fs.readFileSync('./config/ini-file.ini.ts').toString());
22
- });
23
-
24
- test('convert toml file', () => {
25
- // console.log(toml.parse(fs.readFileSync('./config/toml_file.toml')));
26
- expect(GetTsString('./config/toml_file.toml')).toBe(fs.readFileSync('./config/toml_file.toml.ts').toString());
27
- });
28
-
29
- test('config2ts merge csv file list', () => {
30
- expect(GetTsStringFromFileList(["./config/data.csv", "./config/no-id.csv"])).toBe(fs.readFileSync('./config/total.ts').toString());
31
- });
32
-
33
- test('config2ts merge csv file list', () => {
34
- expect(GetValidFileList(['a.csv', 'b.ini', 'c.xxx'])).toEqual(['a.csv', 'b.ini']);
35
- });
36
-
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compileOnSave": true,
3
- "compilerOptions": {
4
- "target": "es6",
5
- "module": "commonjs",
6
- "watch": false,
7
- "sourceMap": false,
8
- "allowJs": true,
9
- "outDir": "./dist"
10
- },
11
- "include": [
12
- "./src/**/*"
13
- ]
14
- }