dtsgenerator 3.12.1 → 3.14.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/dist/utils.js CHANGED
@@ -1,10 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.globFiles = exports.readUrl = exports.readStream = void 0;
3
+ exports.readConfig = exports.globFiles = exports.buildProxyOptions = exports.readUrl = exports.readStream = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const fs = (0, tslib_1.__importStar)(require("fs"));
5
6
  const url_1 = require("url");
6
- const glob_1 = tslib_1.__importDefault(require("glob"));
7
- const https_proxy_agent_1 = tslib_1.__importDefault(require("https-proxy-agent"));
7
+ const glob_1 = (0, tslib_1.__importDefault)(require("glob"));
8
+ const https_proxy_agent_1 = (0, tslib_1.__importDefault)(require("https-proxy-agent"));
9
+ const typescript_1 = require("typescript");
10
+ const commandOptions_1 = require("./commandOptions");
8
11
  function readStream(stream, encoding = 'utf8') {
9
12
  stream.setEncoding(encoding);
10
13
  return new Promise((resolve, reject) => {
@@ -54,13 +57,14 @@ function buildProxyOptions(url) {
54
57
  if (proxyUrl.username) {
55
58
  agentOptions.auth = proxyUrl.username + ':' + proxyUrl.password;
56
59
  }
57
- return { agent: https_proxy_agent_1.default(agentOptions) };
60
+ return { agent: (0, https_proxy_agent_1.default)(agentOptions) };
58
61
  }
59
62
  return undefined;
60
63
  }
64
+ exports.buildProxyOptions = buildProxyOptions;
61
65
  function globFiles(pattern, options) {
62
66
  return new Promise((resolve, reject) => {
63
- glob_1.default(pattern, options !== null && options !== void 0 ? options : {}, (err, matches) => {
67
+ (0, glob_1.default)(pattern, options !== null && options !== void 0 ? options : {}, (err, matches) => {
64
68
  if (err) {
65
69
  reject(err);
66
70
  }
@@ -71,3 +75,77 @@ function globFiles(pattern, options) {
71
75
  });
72
76
  }
73
77
  exports.globFiles = globFiles;
78
+ function readConfig(options) {
79
+ var _a;
80
+ let pc = {};
81
+ const configFile = (_a = options.configFile) !== null && _a !== void 0 ? _a : commandOptions_1.defaultConfigFile;
82
+ try {
83
+ pc = loadJSON(configFile);
84
+ pc.configFile = configFile;
85
+ }
86
+ catch (err) {
87
+ if (options.configFile != null) {
88
+ console.error('Error to load config file from ' + options.configFile);
89
+ }
90
+ }
91
+ if (pc.input == null) {
92
+ pc.input = {
93
+ files: [],
94
+ urls: [],
95
+ stdin: false,
96
+ };
97
+ }
98
+ if (options.files.length > 0) {
99
+ pc.input.files = options.files;
100
+ }
101
+ else if (pc.input.files == null) {
102
+ pc.input.files = [];
103
+ }
104
+ if (options.urls.length > 0) {
105
+ pc.input.urls = options.urls;
106
+ }
107
+ else if (pc.input.urls == null) {
108
+ pc.input.urls = [];
109
+ }
110
+ pc.input.stdin = options.isReadFromStdin();
111
+ if (options.out != null) {
112
+ pc.outputFile = options.out;
113
+ }
114
+ if (options.target != null) {
115
+ pc.target = convertToScriptTarget(options.target);
116
+ }
117
+ else if (pc.target != null) {
118
+ pc.target = convertToScriptTarget(pc.target);
119
+ }
120
+ pc.outputAST = !!options.outputAST;
121
+ return pc;
122
+ }
123
+ exports.readConfig = readConfig;
124
+ function loadJSON(file) {
125
+ const content = fs.readFileSync(file, 'utf-8');
126
+ return JSON.parse(content);
127
+ }
128
+ function convertToScriptTarget(target) {
129
+ switch (target.trim().toLowerCase()) {
130
+ case 'es3':
131
+ return typescript_1.ScriptTarget.ES3;
132
+ case 'es5':
133
+ return typescript_1.ScriptTarget.ES5;
134
+ case 'es2015':
135
+ return typescript_1.ScriptTarget.ES2015;
136
+ case 'es2016':
137
+ return typescript_1.ScriptTarget.ES2016;
138
+ case 'es2017':
139
+ return typescript_1.ScriptTarget.ES2017;
140
+ case 'es2018':
141
+ return typescript_1.ScriptTarget.ES2018;
142
+ case 'es2019':
143
+ return typescript_1.ScriptTarget.ES2019;
144
+ case 'es2020':
145
+ return typescript_1.ScriptTarget.ES2020;
146
+ case 'esnext':
147
+ return typescript_1.ScriptTarget.ESNext;
148
+ default:
149
+ return typescript_1.ScriptTarget.Latest;
150
+ }
151
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dtsgenerator",
3
- "version": "3.12.1",
3
+ "version": "3.14.0",
4
4
  "description": "TypeScript d.ts file generator for JSON Schema file",
5
5
  "bin": {
6
6
  "dtsgen": "bin/dtsgen"
@@ -11,17 +11,16 @@
11
11
  "build": "tsc -p ./tsconfig-build.json",
12
12
  "clean": "rimraf dist/ build/ {src,test}/**/*.{js,js.map} *.tsbuildinfo",
13
13
  "compile": "tsc -p .",
14
- "coverage": "nyc report --reporter=text-lcov | coveralls",
15
- "do-test": "cross-env TS_NODE_FILES=true mocha --exit --require ts-node/register --colors test/*_test.ts",
16
- "lint:fix": "npm run lint -- --fix",
17
- "lint": "eslint --ext ts src/ test/",
14
+ "lint": "eslint --fix --ext ts src/ test/",
18
15
  "format": "prettier --write 'src/**/*.ts' 'test/*.ts' 'test/core/*.ts'",
19
- "precompile": "npm run lint:fix && npm run format",
16
+ "precompile": "npm run lint && npm run format",
20
17
  "prepublishOnly": "npm run build",
21
18
  "pretest": "npm run compile",
22
- "test:update-snapshot": "UPDATE_SNAPSHOT=1 npm run test",
19
+ "do-test": "cross-env TS_NODE_FILES=true mocha --exit --require ts-node/register --require source-map-support/register --colors test/*_test.ts test/**/*_test.ts",
23
20
  "test": "nyc npm run do-test",
24
- "watch": "npm run compile -- -w"
21
+ "test:update-snapshot": "UPDATE_SNAPSHOT=1 npm run test",
22
+ "coverage": "nyc report --reporter=lcov",
23
+ "prepare": "husky install"
25
24
  },
26
25
  "directories": {
27
26
  "test": "test/"
@@ -45,50 +44,42 @@
45
44
  "node": ">= 10.0"
46
45
  },
47
46
  "dependencies": {
48
- "commander": "^7.2.0",
49
- "cross-fetch": "^3.1.4",
50
- "debug": "^4.3.1",
51
- "glob": "^7.1.7",
47
+ "commander": "^8.3.0",
48
+ "cross-fetch": "^3.1.5",
49
+ "debug": "^4.3.3",
50
+ "glob": "^7.2.0",
52
51
  "https-proxy-agent": "^5.0.0",
53
52
  "js-yaml": "^4.1.0",
54
- "tslib": "^2.2.0",
55
- "typescript": "^4.2.4"
53
+ "tslib": "^2.3.1",
54
+ "typescript": "^4.5.5"
56
55
  },
57
56
  "devDependencies": {
58
- "@dtsgenerator/do-nothing": "^2.4.0",
59
- "@dtsgenerator/replace-namespace": "^1.4.1",
60
- "@dtsgenerator/single-quote": "^1.5.1",
61
- "@types/debug": "^4.1.5",
62
- "@types/glob": "^7.1.3",
63
- "@types/js-yaml": "^4.0.1",
64
- "@types/mkdirp": "^1.0.1",
65
- "@types/mocha": "^8.2.2",
66
- "@types/node": "^10.17.60",
67
- "@types/prettier": "^2.2.3",
68
- "@typescript-eslint/eslint-plugin": "^4.24.0",
69
- "@typescript-eslint/parser": "^4.24.0",
70
- "coveralls": "^3.1.0",
57
+ "@dtsgenerator/do-nothing": "^2.5.1",
58
+ "@dtsgenerator/eslint-config": "^0.5.0",
59
+ "@dtsgenerator/replace-namespace": "^1.5.1",
60
+ "@dtsgenerator/single-quote": "^1.6.1",
61
+ "@istanbuljs/nyc-config-typescript": "^1.0.2",
62
+ "@types/debug": "^4.1.7",
63
+ "@types/glob": "^7.2.0",
64
+ "@types/js-yaml": "^4.0.5",
65
+ "@types/mkdirp": "^1.0.2",
66
+ "@types/mocha": "^9.1.0",
67
+ "@types/node": "^16.11.21",
71
68
  "cross-env": "^7.0.3",
72
- "eslint": "^7.26.0",
73
- "eslint-config-prettier": "^8.3.0",
74
- "eslint-import-resolver-typescript": "^2.4.0",
75
- "eslint-plugin-import": "^2.23.2",
76
- "eslint-plugin-prettier": "^3.4.0",
77
- "eslint-plugin-sort-imports-es6-autofix": "^0.6.0",
78
- "husky": "^6.0.0",
79
- "lint-staged": "^11.0.0",
80
- "mocha": "^8.4.0",
69
+ "eslint": "^8.7.0",
70
+ "husky": "^7.0.4",
71
+ "lint-staged": "^12.3.2",
72
+ "mocha": "^9.2.0",
81
73
  "nyc": "^15.1.0",
82
- "prettier": "^2.3.0",
74
+ "prettier": "^2.5.1",
83
75
  "rimraf": "^3.0.2",
84
- "ts-node": "^9.1.1"
85
- },
86
- "husky": {
87
- "hooks": {
88
- "pre-commit": "lint-staged"
89
- }
76
+ "source-map-support": "^0.5.21",
77
+ "ts-node": "^10.4.0"
90
78
  },
91
79
  "lint-staged": {
92
- "*.ts": "npm run precompile"
80
+ "**/*.ts": [
81
+ "prettier --write",
82
+ "eslint --fix"
83
+ ]
93
84
  }
94
85
  }
package/src/cli.ts CHANGED
@@ -1,89 +1,14 @@
1
1
  import * as fs from 'fs';
2
2
  import { dirname } from 'path';
3
- import { ScriptTarget } from 'typescript';
4
- import opts, {
5
- initialize,
6
- CommandOptions,
7
- defaultConfigFile,
8
- } from './commandOptions';
3
+ import opts, { initialize } from './commandOptions';
9
4
  import dtsgenerator, {
10
5
  Schema,
11
6
  readSchemaFromStdin,
12
7
  readSchemasFromFile,
13
8
  readSchemaFromUrl,
14
9
  } from './core';
15
- import config, { Config, setConfig, showConfig } from './core/config';
16
-
17
- function readConfig(options: CommandOptions): Partial<Config> {
18
- let pc: Partial<Config> = {};
19
- const configFile = options.configFile ?? defaultConfigFile;
20
- try {
21
- pc = loadJSON(configFile);
22
- pc.configFile = configFile;
23
- } catch (err) {
24
- if (options.configFile != null) {
25
- console.error(
26
- 'Error to load config file from ' + options.configFile
27
- );
28
- }
29
- }
30
-
31
- if (pc.input == null) {
32
- pc.input = {
33
- files: [],
34
- urls: [],
35
- stdin: false,
36
- };
37
- }
38
- if (options.files.length > 0) {
39
- pc.input.files = options.files;
40
- } else if (pc.input.files == null) {
41
- pc.input.files = [];
42
- }
43
- if (options.urls.length > 0) {
44
- pc.input.urls = options.urls;
45
- } else if (pc.input.urls == null) {
46
- pc.input.urls = [];
47
- }
48
- pc.input.stdin = options.isReadFromStdin();
49
-
50
- if (options.out != null) {
51
- pc.outputFile = options.out;
52
- }
53
- if (options.target != null) {
54
- pc.target = convertToScriptTarget(options.target);
55
- }
56
- pc.outputAST = !!options.outputAST;
57
- return pc;
58
- }
59
- function loadJSON(file: string): Partial<Config> {
60
- const content = fs.readFileSync(file, 'utf-8');
61
- return JSON.parse(content) as Partial<Config>;
62
- }
63
- function convertToScriptTarget(target: string): ScriptTarget {
64
- switch (target.trim().toLowerCase()) {
65
- case 'es3':
66
- return ScriptTarget.ES3;
67
- case 'es5':
68
- return ScriptTarget.ES5;
69
- case 'es2015':
70
- return ScriptTarget.ES2015;
71
- case 'es2016':
72
- return ScriptTarget.ES2016;
73
- case 'es2017':
74
- return ScriptTarget.ES2017;
75
- case 'es2018':
76
- return ScriptTarget.ES2018;
77
- case 'es2019':
78
- return ScriptTarget.ES2019;
79
- case 'es2020':
80
- return ScriptTarget.ES2020;
81
- case 'esnext':
82
- return ScriptTarget.ESNext;
83
- default:
84
- return ScriptTarget.Latest;
85
- }
86
- }
10
+ import config, { setConfig, showConfig } from './core/config';
11
+ import { readConfig } from './utils';
87
12
 
88
13
  async function loadContents(): Promise<Schema[]> {
89
14
  let contents: Schema[] = [];
@@ -47,7 +47,7 @@ function parse(options: CommandOptions, argv: string[]): commander.Command {
47
47
  }
48
48
 
49
49
  // eslint-disable-next-line @typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-assignment
50
- const pkg: Record<string, any> = require('../package.json');
50
+ const pkg: Record<string, string> = require('../package.json');
51
51
 
52
52
  // <hoge> is required, [hoge] is optional
53
53
  program
@@ -9,9 +9,6 @@ function buildTypeNameIdentifier(name: string): ts.Identifier {
9
9
  return ts.factory.createIdentifier(toValidIdentifier(name, config.target));
10
10
  }
11
11
  function buildPropertyNameIdentifier(name: string): ts.PropertyName {
12
- if (/^\d/.test(name)) {
13
- name = '$' + name;
14
- }
15
12
  if (checkInvalidCharacter(name, config.target)) {
16
13
  return ts.factory.createIdentifier(name);
17
14
  } else {
@@ -198,6 +195,17 @@ export function buildUnionTypeNode<T>(
198
195
  return ts.factory.createParenthesizedType(node);
199
196
  }
200
197
 
198
+ export function buildIntersectionTypeNode(
199
+ types: readonly ts.TypeNode[],
200
+ terminate: boolean
201
+ ): ts.TypeNode {
202
+ const node = ts.factory.createIntersectionTypeNode(types);
203
+ if (terminate) {
204
+ return node;
205
+ }
206
+ return ts.factory.createParenthesizedType(node);
207
+ }
208
+
201
209
  export function buildTupleTypeNode(
202
210
  types: ts.TypeNode | ts.TypeNode[],
203
211
  minItems?: number,
@@ -325,7 +333,7 @@ function getComment(schema: NormalizedSchema): string[] {
325
333
  function protectComment(str: string): string {
326
334
  return str.replace(/\*\//g, '*\u200B/'); // Unicode [ZERO WIDTH SPACE]
327
335
  }
328
- function appendComment(value?: string): void {
336
+ function appendComment(value?: any): void {
329
337
  if (value == null) {
330
338
  return;
331
339
  }
@@ -18,13 +18,15 @@ export interface Config {
18
18
  }
19
19
 
20
20
  const defaultConfig: Config = {
21
+ configFile: undefined,
21
22
  input: {
22
23
  files: [],
23
24
  urls: [],
24
25
  stdin: false,
25
26
  },
26
- outputAST: false,
27
+ outputFile: undefined,
27
28
  target: ScriptTarget.Latest,
29
+ outputAST: false,
28
30
  plugins: {},
29
31
  };
30
32