config2ts 2.1.3 → 2.1.5

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/dist/config2ts.js CHANGED
@@ -1,97 +1,97 @@
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;
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 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 = (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
- }
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 CHANGED
@@ -1,8 +1,5 @@
1
1
  /** @type {import('ts-jest').JestConfigWithTsJest} */
2
2
  module.exports = {
3
3
  preset: 'ts-jest',
4
- testEnvironment: 'node',
5
- moduleNameMapper: {
6
- 'd3': '<rootDir>/node_modules/d3/dist/d3.min.js',
7
- }
4
+ testEnvironment: 'node'
8
5
  };
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.1.3",
3
+ "version": "2.1.5",
4
4
  "description": "convert config to ts file.",
5
5
  "main": "index.js",
6
+ "type": "commonjs",
6
7
  "directories": {
7
8
  "test": "test"
8
9
  },
@@ -25,16 +26,14 @@
25
26
  "homepage": "https://github.com/livingyang/config2ts#readme",
26
27
  "dependencies": {
27
28
  "change-case": "^4.1.2",
28
- "commander": "^9.4.1",
29
- "d3": "^7.8.4",
29
+ "commander": "^9.5.0",
30
+ "d3": "^4.12.2",
30
31
  "ini": "^3.0.1",
31
32
  "toml": "^3.0.0"
32
33
  },
33
34
  "devDependencies": {
34
- "@types/d3": "^7.4.0",
35
35
  "@types/ini": "^1.3.31",
36
36
  "@types/jest": "^29.1.2",
37
- "@types/node": "^18.8.5",
38
37
  "jest": "^29.5.0",
39
38
  "ts-jest": "^29.1.0",
40
39
  "typescript": "^4.9.5"