config2ts 2.1.0 → 2.1.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.
@@ -1,10 +1,31 @@
1
- {
2
- // See https://go.microsoft.com/fwlink/?LinkId=733558
3
- // for the documentation about the tasks.json format
4
- "version": "0.1.0",
5
- "command": "tsc",
6
- "isShellCommand": true,
7
- "args": ["-p", ".", "--outDir", "dist"],
8
- "showOutput": "always",
9
- "problemMatcher": "$tsc"
1
+ {
2
+ // See https://go.microsoft.com/fwlink/?LinkId=733558
3
+ // for the documentation about the tasks.json format
4
+ "version": "2.0.0",
5
+ "command": "tsc",
6
+ "args": [
7
+ "-p",
8
+ ".",
9
+ "--outDir",
10
+ "dist"
11
+ ],
12
+ "problemMatcher": "$tsc",
13
+ "tasks": [
14
+ {
15
+ "label": "tsc",
16
+ "type": "shell",
17
+ "command": "tsc",
18
+ "args": [
19
+ "-p",
20
+ ".",
21
+ "--outDir",
22
+ "dist"
23
+ ],
24
+ "problemMatcher": "$tsc",
25
+ "group": {
26
+ "_id": "build",
27
+ "isDefault": false
28
+ }
29
+ }
30
+ ]
10
31
  }
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2016 huanming li
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2016 huanming li
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,21 +1,21 @@
1
- # config2ts
2
- convert config to ts file.
3
-
4
- # install
5
- run command: `npm install -g config2ts`
6
-
7
- # how to use
8
- config2ts folder contains csv
9
-
10
- ## Usage
11
- ```
12
- Options:
13
-
14
- -h, --help output usage information
15
- -V, --version output the version number
16
- -d, --dir <path> set convert path. default: ./
17
- -o, --outDir <path> set outDir path. default: same as dir
18
- -p, --prefix <prefix> set interface prefix. default: ""
19
- -s, --suffix <suffix> set interface suffix. default: "Csv"
20
- -m, --merge <name> merge all to one ts file. default: csv.ts
1
+ # config2ts
2
+ convert config to ts file.
3
+
4
+ # install
5
+ run command: `npm install -g config2ts`
6
+
7
+ # how to use
8
+ config2ts folder contains csv
9
+
10
+ ## Usage
11
+ ```
12
+ Options:
13
+
14
+ -h, --help output usage information
15
+ -V, --version output the version number
16
+ -d, --dir <path> set convert path. default: ./
17
+ -o, --outDir <path> set outDir path. default: same as dir
18
+ -p, --prefix <prefix> set interface prefix. default: ""
19
+ -s, --suffix <suffix> set interface suffix. default: "Csv"
20
+ -m, --merge <name> merge all to one ts file. default: csv.ts
21
21
  ```
package/bin/config2ts CHANGED
@@ -1,2 +1,2 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
  require('../dist/main');
package/config/data.csv CHANGED
@@ -1,5 +1,5 @@
1
- id,name,bool,num
2
- Number,String,Boolean,Number
3
- 1,"xxx",1,111
4
- 2,"xxx",,222
1
+ id,name,bool,num
2
+ String,String,Boolean,Number
3
+ 1,"xxx",1,111
4
+ 2,"xxx",,222
5
5
  3,333,,
@@ -1,34 +1,34 @@
1
- export namespace DataCsv {
2
-
3
- export interface Record {
4
- id: number;
5
- name: string;
6
- bool: boolean;
7
- num: number;
8
- };
9
-
10
- export const List: Record[] = [
11
- {
12
- "id": 1,
13
- "name": "xxx",
14
- "bool": true,
15
- "num": 111
16
- },
17
- {
18
- "id": 2,
19
- "name": "xxx",
20
- "bool": false,
21
- "num": 222
22
- },
23
- {
24
- "id": 3,
25
- "name": "333",
26
- "bool": false,
27
- "num": 0
28
- }
29
- ];
30
-
31
- export const Map: { [id: number]: Record } = {};
32
- for (const v of List) { Map[v.id] = v; };
33
-
1
+ export namespace DataCsv {
2
+
3
+ export interface Record {
4
+ id: string;
5
+ name: string;
6
+ bool: boolean;
7
+ num: number;
8
+ };
9
+
10
+ export const List: Record[] = [
11
+ {
12
+ "id": "1",
13
+ "name": "xxx",
14
+ "bool": true,
15
+ "num": 111
16
+ },
17
+ {
18
+ "id": "2",
19
+ "name": "xxx",
20
+ "bool": false,
21
+ "num": 222
22
+ },
23
+ {
24
+ "id": "3",
25
+ "name": "333",
26
+ "bool": false,
27
+ "num": 0
28
+ }
29
+ ];
30
+
31
+ export const Map: { [id: string]: Record } = {};
32
+ for (const v of List) { Map[v.id] = v; };
33
+
34
34
  };
@@ -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,62 +1,62 @@
1
- export namespace DataCsv {
2
-
3
- export interface Record {
4
- id: number;
5
- name: string;
6
- bool: boolean;
7
- num: number;
8
- };
9
-
10
- export const List: Record[] = [
11
- {
12
- "id": 1,
13
- "name": "xxx",
14
- "bool": true,
15
- "num": 111
16
- },
17
- {
18
- "id": 2,
19
- "name": "xxx",
20
- "bool": false,
21
- "num": 222
22
- },
23
- {
24
- "id": 3,
25
- "name": "333",
26
- "bool": false,
27
- "num": 0
28
- }
29
- ];
30
-
31
- export const Map: { [id: number]: Record } = {};
32
- for (const v of List) { Map[v.id] = v; };
33
-
34
- };
35
-
36
- export namespace NoIdCsv {
37
-
38
- export interface Record {
39
- name: string;
40
- bool: boolean;
41
- num: number;
42
- };
43
-
44
- export const List: Record[] = [
45
- {
46
- "name": "xxx",
47
- "bool": true,
48
- "num": 111
49
- },
50
- {
51
- "name": "xxx",
52
- "bool": false,
53
- "num": 222
54
- },
55
- {
56
- "name": "333",
57
- "bool": false,
58
- "num": 0
59
- }
60
- ];
61
-
1
+ export namespace DataCsv {
2
+
3
+ export interface Record {
4
+ id: string;
5
+ name: string;
6
+ bool: boolean;
7
+ num: number;
8
+ };
9
+
10
+ export const List: Record[] = [
11
+ {
12
+ "id": "1",
13
+ "name": "xxx",
14
+ "bool": true,
15
+ "num": 111
16
+ },
17
+ {
18
+ "id": "2",
19
+ "name": "xxx",
20
+ "bool": false,
21
+ "num": 222
22
+ },
23
+ {
24
+ "id": "3",
25
+ "name": "333",
26
+ "bool": false,
27
+ "num": 0
28
+ }
29
+ ];
30
+
31
+ export const Map: { [id: string]: Record } = {};
32
+ for (const v of List) { Map[v.id] = v; };
33
+
34
+ };
35
+
36
+ export namespace NoIdCsv {
37
+
38
+ export interface Record {
39
+ name: string;
40
+ bool: boolean;
41
+ num: number;
42
+ };
43
+
44
+ export const List: Record[] = [
45
+ {
46
+ "name": "xxx",
47
+ "bool": true,
48
+ "num": 111
49
+ },
50
+ {
51
+ "name": "xxx",
52
+ "bool": false,
53
+ "num": 222
54
+ },
55
+ {
56
+ "name": "333",
57
+ "bool": false,
58
+ "num": 0
59
+ }
60
+ ];
61
+
62
62
  };
package/dist/config2ts.js CHANGED
@@ -1,96 +1,96 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const path = require("path");
4
- const fs = require("fs");
5
- const d3 = require("d3");
6
- const changeCase = require("change-case");
7
- let toml = require('toml');
8
- let packageJson = require('../package.json');
9
- exports.Convert = {
10
- ini: function (str, moduleName) {
11
- let obj = toml.parse(str);
12
- return `export const ${moduleName} = ${JSON.stringify(obj, null, 4)};`;
13
- },
14
- csv: function (str, moduleName) {
15
- return csv2ts(str, moduleName);
16
- },
17
- toml: function (str, moduleName) {
18
- let obj = toml.parse(str);
19
- return `export const ${moduleName} = ${JSON.stringify(obj, null, 4)};`;
20
- }
21
- };
22
- function csv2ts(csvString, moduleName) {
23
- let convert = {};
24
- let result = d3.csvParse(csvString, (d, i) => {
25
- if (i === 0) {
26
- convert = d;
27
- return null;
28
- }
29
- else {
30
- for (let k in d) {
31
- if (global[convert[k]] instanceof Function) {
32
- d[k] = global[convert[k]](d[k]);
33
- }
34
- }
35
- return d;
36
- }
37
- });
38
- // 删除columns属性
39
- delete result.columns;
40
- if (result.length <= 0) {
41
- return "";
42
- }
43
- let template = `export namespace ${moduleName} {\n\n`;
44
- // generate interface
45
- template += ` export interface Record {\n`;
46
- for (let field in convert) {
47
- let fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
48
- template += ` ${field}: ${fieldType};\n`;
49
- }
50
- template += ' };\n\n';
51
- // generate table
52
- template += ` export const List: Record[] = `;
53
- // console.log(prettyFormat(result, op));
54
- template += JSON.stringify(result, null, 4).replace(/\n/g, '\n ');
55
- template += ';\n\n';
56
- if (convert['id'] === 'Number') {
57
- template += ` export const Map: { [id: number]: Record } = {};\n`;
58
- template += ` for (const v of List) { Map[v.id] = v; };\n\n`;
59
- }
60
- template += "};";
61
- return template;
62
- }
63
- function GetFileExt(filePath) {
64
- let pathObject = path.parse(filePath);
65
- return pathObject.ext.slice(1);
66
- }
67
- function GetTsString(filePath) {
68
- let pathObject = path.parse(filePath);
69
- let handle = exports.Convert[GetFileExt(filePath)];
70
- if (handle) {
71
- let moduleName = changeCase.pascalCase(pathObject.base);
72
- return handle(fs.readFileSync(filePath).toString(), moduleName);
73
- }
74
- else {
75
- return '';
76
- }
77
- }
78
- exports.GetTsString = GetTsString;
79
- function GetTsStringFromFileList(fileList) {
80
- return fileList.map((filePath) => {
81
- return GetTsString(filePath);
82
- }).join('\n\n');
83
- }
84
- exports.GetTsStringFromFileList = GetTsStringFromFileList;
85
- function GetHeaderInfo() {
86
- let header = `export let config2ts_version = "${packageJson.version}";\n\n`;
87
- header += `export let config2ts_build_timestamp = ${Date.now()};\n\n`;
88
- return header;
89
- }
90
- exports.GetHeaderInfo = GetHeaderInfo;
91
- function GetValidFileList(fileList) {
92
- return fileList.filter((filePath) => {
93
- return exports.Convert[GetFileExt(filePath)] != null;
94
- });
95
- }
96
- exports.GetValidFileList = GetValidFileList;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const path = require("path");
4
+ const fs = require("fs");
5
+ const d3 = require("d3");
6
+ const changeCase = require("change-case");
7
+ let toml = require('toml');
8
+ let packageJson = require('../package.json');
9
+ exports.Convert = {
10
+ ini: function (str, moduleName) {
11
+ let obj = toml.parse(str);
12
+ return `export const ${moduleName} = ${JSON.stringify(obj, null, 4)};`;
13
+ },
14
+ csv: function (str, moduleName) {
15
+ return csv2ts(str, moduleName);
16
+ },
17
+ toml: function (str, moduleName) {
18
+ let obj = toml.parse(str);
19
+ return `export const ${moduleName} = ${JSON.stringify(obj, null, 4)};`;
20
+ }
21
+ };
22
+ function csv2ts(csvString, moduleName) {
23
+ let convert = {};
24
+ let result = d3.csvParse(csvString, (d, i) => {
25
+ if (i === 0) {
26
+ convert = d;
27
+ return null;
28
+ }
29
+ else {
30
+ for (let k in d) {
31
+ if (global[convert[k]] instanceof Function) {
32
+ d[k] = global[convert[k]](d[k]);
33
+ }
34
+ }
35
+ return d;
36
+ }
37
+ });
38
+ // 删除columns属性
39
+ delete result.columns;
40
+ if (result.length <= 0) {
41
+ return "";
42
+ }
43
+ let template = `export namespace ${moduleName} {\n\n`;
44
+ // generate interface
45
+ template += ` export interface Record {\n`;
46
+ for (let field in convert) {
47
+ let fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
48
+ template += ` ${field}: ${fieldType};\n`;
49
+ }
50
+ template += ' };\n\n';
51
+ // generate table
52
+ template += ` export const List: Record[] = `;
53
+ // console.log(prettyFormat(result, op));
54
+ template += JSON.stringify(result, null, 4).replace(/\n/g, '\n ');
55
+ template += ';\n\n';
56
+ if (convert['id'] === 'Number') {
57
+ template += ` export const Map: { [id: number]: Record } = {};\n`;
58
+ template += ` for (const v of List) { Map[v.id] = v; };\n\n`;
59
+ }
60
+ template += "};";
61
+ return template;
62
+ }
63
+ function GetFileExt(filePath) {
64
+ let pathObject = path.parse(filePath);
65
+ return pathObject.ext.slice(1);
66
+ }
67
+ function GetTsString(filePath) {
68
+ let pathObject = path.parse(filePath);
69
+ let handle = exports.Convert[GetFileExt(filePath)];
70
+ if (handle) {
71
+ let moduleName = changeCase.pascalCase(pathObject.base);
72
+ return handle(fs.readFileSync(filePath).toString(), moduleName);
73
+ }
74
+ else {
75
+ return '';
76
+ }
77
+ }
78
+ exports.GetTsString = GetTsString;
79
+ function GetTsStringFromFileList(fileList) {
80
+ return fileList.map((filePath) => {
81
+ return GetTsString(filePath);
82
+ }).join('\n\n');
83
+ }
84
+ exports.GetTsStringFromFileList = GetTsStringFromFileList;
85
+ function GetHeaderInfo() {
86
+ let header = `export let config2ts_version = "${packageJson.version}";\n\n`;
87
+ header += `export let config2ts_build_timestamp = ${Date.now()};\n\n`;
88
+ return header;
89
+ }
90
+ exports.GetHeaderInfo = GetHeaderInfo;
91
+ function GetValidFileList(fileList) {
92
+ return fileList.filter((filePath) => {
93
+ return exports.Convert[GetFileExt(filePath)] != null;
94
+ });
95
+ }
96
+ exports.GetValidFileList = GetValidFileList;
package/dist/main.js CHANGED
@@ -1,32 +1,32 @@
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 = config2ts_1.GetValidFileList(fileList).map((filename) => {
19
- return path.join(dir, filename);
20
- });
21
- let mergeFile = path.join(outDir, merge);
22
- fs.writeFileSync(mergeFile, config2ts_1.GetHeaderInfo() + config2ts_1.GetTsStringFromFileList(fileList));
23
- console.log(`config2ts, ${fileList.length} config files, merge into: ${mergeFile}`);
24
- }
25
- else {
26
- config2ts_1.GetValidFileList(fileList).forEach((filename) => {
27
- let target = path.join(outDir, `${filename}.ts`);
28
- fs.writeFileSync(target, config2ts_1.GetTsString(path.join(dir, filename)));
29
- console.log(`config2ts, convert: ${filename} , to: ${target}`);
30
- });
31
- console.log('config2ts convert done!');
32
- }
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 = config2ts_1.GetValidFileList(fileList).map((filename) => {
19
+ return path.join(dir, filename);
20
+ });
21
+ let mergeFile = path.join(outDir, merge);
22
+ fs.writeFileSync(mergeFile, config2ts_1.GetHeaderInfo() + config2ts_1.GetTsStringFromFileList(fileList));
23
+ console.log(`config2ts, ${fileList.length} config files, merge into: ${mergeFile}`);
24
+ }
25
+ else {
26
+ config2ts_1.GetValidFileList(fileList).forEach((filename) => {
27
+ let target = path.join(outDir, `${filename}.ts`);
28
+ fs.writeFileSync(target, 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 ADDED
@@ -0,0 +1,8 @@
1
+ /** @type {import('ts-jest').JestConfigWithTsJest} */
2
+ module.exports = {
3
+ preset: 'ts-jest',
4
+ testEnvironment: 'node',
5
+ moduleNameMapper: {
6
+ 'd3': '<rootDir>/node_modules/d3/dist/d3.min.js',
7
+ }
8
+ };
package/package.json CHANGED
@@ -1,54 +1,42 @@
1
- {
2
- "name": "config2ts",
3
- "version": "2.1.0",
4
- "description": "convert config to ts file.",
5
- "main": "index.js",
6
- "directories": {
7
- "test": "test"
8
- },
9
- "bin": {
10
- "config2ts": "./bin/config2ts"
11
- },
12
- "scripts": {
13
- "test": "node_modules/.bin/jest",
14
- "watch-test": "node_modules/.bin/jest --watch"
15
- },
16
- "repository": {
17
- "type": "git",
18
- "url": "git+https://github.com/livingyang/config2ts.git"
19
- },
20
- "author": "livingyang",
21
- "license": "ISC",
22
- "bugs": {
23
- "url": "https://github.com/livingyang/config2ts/issues"
24
- },
25
- "homepage": "https://github.com/livingyang/config2ts#readme",
26
- "dependencies": {
27
- "change-case": "^3.0.1",
28
- "commander": "^2.9.0",
29
- "d3": "^4.12.2",
30
- "ini": "^1.3.5",
31
- "toml": "^2.3.3"
32
- },
33
- "devDependencies": {
34
- "@types/d3": "^4.12.0",
35
- "@types/ini": "^1.3.29",
36
- "@types/jest": "^22.0.1",
37
- "@types/node": "^9.3.0",
38
- "jest": "^22.0.6",
39
- "ts-jest": "^22.0.1",
40
- "typescript": "^2.6.2"
41
- },
42
- "jest": {
43
- "transform": {
44
- "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
45
- },
46
- "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
47
- "moduleFileExtensions": [
48
- "ts",
49
- "tsx",
50
- "js",
51
- "json"
52
- ]
53
- }
54
- }
1
+ {
2
+ "name": "config2ts",
3
+ "version": "2.1.2",
4
+ "description": "convert config to ts file.",
5
+ "main": "index.js",
6
+ "directories": {
7
+ "test": "test"
8
+ },
9
+ "bin": {
10
+ "config2ts": "./bin/config2ts"
11
+ },
12
+ "scripts": {
13
+ "test": "jest",
14
+ "watch-test": "node_modules/.bin/jest --watch"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/livingyang/config2ts.git"
19
+ },
20
+ "author": "livingyang",
21
+ "license": "ISC",
22
+ "bugs": {
23
+ "url": "https://github.com/livingyang/config2ts/issues"
24
+ },
25
+ "homepage": "https://github.com/livingyang/config2ts#readme",
26
+ "dependencies": {
27
+ "change-case": "^4.1.2",
28
+ "commander": "^9.4.1",
29
+ "d3": "^7.8.4",
30
+ "ini": "^3.0.1",
31
+ "toml": "^3.0.0"
32
+ },
33
+ "devDependencies": {
34
+ "@types/d3": "^7.4.0",
35
+ "@types/ini": "^1.3.31",
36
+ "@types/jest": "^29.1.2",
37
+ "@types/node": "^18.8.5",
38
+ "jest": "^29.5.0",
39
+ "ts-jest": "^29.1.0",
40
+ "typescript": "^4.9.5"
41
+ }
42
+ }
package/src/config2ts.ts CHANGED
@@ -1,110 +1,110 @@
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'] === 'Number') {
68
- template += ` export const Map: { [id: number]: 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
- }
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 CHANGED
@@ -1,35 +1,35 @@
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
+ 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 +1,36 @@
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
- });
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
36
 
package/tsconfig.json CHANGED
@@ -1,13 +1,14 @@
1
- {
2
- "compileOnSave": true,
3
- "compilerOptions": {
4
- "target": "es6",
5
- "module": "commonjs",
6
- "watch": false,
7
- "sourceMap": false,
8
- "outDir": "./dist"
9
- },
10
- "include": [
11
- "./src/**/*"
12
- ]
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
+ ]
13
14
  }