ts-json-schema-generator 3.0.0-native.0 → 3.0.0-native.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/README.md +37 -1
- package/bin/ts-json-schema-generator.js +6 -16
- package/index.d.ts +56 -0
- package/index.js +155 -0
- package/package.json +12 -7
- package/resolve-binary.js +34 -0
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Generate JSON schema from your TypeScript sources.
|
|
4
4
|
|
|
5
|
-
Version 3 is a native binary built on [typescript-go](https://github.com/microsoft/typescript-go), the compiler
|
|
5
|
+
Version 3 is a native binary built on [typescript-go](https://github.com/microsoft/typescript-go), the compiler released as TypeScript 7. The interface is the CLI; the package also exposes a thin [programmatic wrapper](#programmatic-usage) that runs it for you.
|
|
6
6
|
|
|
7
7
|
Installing this package pulls in a small platform-specific package containing the binary for your OS and CPU (macOS, Linux, and Windows on x64 and arm64).
|
|
8
8
|
|
|
@@ -20,6 +20,42 @@ npx ts-json-schema-generator --path 'my/project/**/*.ts' --type 'My.Type.Name'
|
|
|
20
20
|
|
|
21
21
|
The flags are the same as the 2.x CLI: `--path/-p`, `--type/-t`, `--tsconfig/-f`, `--id/-i`, `--expose/-e`, `--jsDoc/-j`, `--functions`, `--markdown-description`, `--full-description`, `--minify`, `--unstable`, `--strict-tuples`, `--no-top-ref`, `--no-type-check`, `--no-ref-encode`, `--additional-properties`, `--validation-keywords`, `--out/-o`. See [the options documentation](https://github.com/vega/ts-json-schema-generator#options) for what each one does.
|
|
22
22
|
|
|
23
|
+
One flag is new in version 3: `--outdir <dir>` writes a separate `<dir>/<type>.schema.json` for every `--type`, parsing the sources once for the whole set instead of once per file.
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npx ts-json-schema-generator --path 'src/**/*.ts' --outdir schemas --type Spec --type Config
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Each file is exactly what that type's own `--out` run would have produced. `--outdir` cannot be combined with `--out`, and the types have to be listed explicitly (no `*`). Note that `--id` would then put the same `$id` on every file, which collides in resolvers that cache by `$id`. The programmatic API below is "config in, schema out" and has no `outdir` equivalent.
|
|
30
|
+
|
|
31
|
+
## Programmatic usage
|
|
32
|
+
|
|
33
|
+
`generateSchema` takes the same options as the 2.x `Config` type and resolves with the parsed schema, so code that was "config in, schema out" migrates by swapping the call:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { generateSchema } from "ts-json-schema-generator";
|
|
37
|
+
|
|
38
|
+
const schema = await generateSchema({
|
|
39
|
+
path: "src/types.ts",
|
|
40
|
+
type: "MyType",
|
|
41
|
+
tsconfig: "tsconfig.json",
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`generateSchemaSync` is the same thing without the `await`:
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import { generateSchemaSync } from "ts-json-schema-generator";
|
|
49
|
+
|
|
50
|
+
const schema = generateSchemaSync({ path: "src/types.ts", type: "MyType" });
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Both spawn the native binary and throw (or reject) with an `Error` carrying the CLI's `stderr` when generation fails. TypeScript definitions ship with the package.
|
|
54
|
+
|
|
55
|
+
Options that carry JavaScript values cannot cross the process boundary: an existing `tsProgram`, and augmentors such as custom node parsers, type formatters, or `SchemaGenerator` subclasses. Those are unsupported here, and passing them throws — projects that need them should stay on the 2.x line, which remains the Node.js library.
|
|
56
|
+
|
|
57
|
+
Advanced: set `TS_JSON_SCHEMA_GENERATOR_BINARY` to run a specific binary instead of the one from the platform package. It applies to the CLI too, and is mainly useful for testing a local build.
|
|
58
|
+
|
|
23
59
|
## Links
|
|
24
60
|
|
|
25
61
|
- Source: [vega/ts-json-schema-generator-go](https://github.com/vega/ts-json-schema-generator-go)
|
|
@@ -1,26 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// Launcher for the native CLI
|
|
3
|
-
//
|
|
4
|
-
// execs it. Dependency-free CommonJS so it runs before anything is built.
|
|
2
|
+
// Launcher for the native CLI: locates the binary matching the host and execs
|
|
3
|
+
// it. Dependency-free CommonJS so it runs before anything is built.
|
|
5
4
|
"use strict";
|
|
6
5
|
|
|
7
6
|
const { execFileSync } = require("node:child_process");
|
|
8
|
-
|
|
9
|
-
const platform = process.platform;
|
|
10
|
-
const arch = process.arch;
|
|
11
|
-
const pkg = `ts-json-schema-generator-${platform}-${arch}`;
|
|
12
|
-
const exe = platform === "win32" ? "bin/ts-json-schema-generator.exe" : "bin/ts-json-schema-generator";
|
|
7
|
+
const { resolveBinary } = require("../resolve-binary.js");
|
|
13
8
|
|
|
14
9
|
let binary;
|
|
15
10
|
try {
|
|
16
|
-
binary =
|
|
17
|
-
} catch {
|
|
18
|
-
console.error(
|
|
19
|
-
`ts-json-schema-generator: no native binary for ${platform}-${arch}.\n` +
|
|
20
|
-
`The optional dependency "${pkg}" is not installed. It may not exist for this ` +
|
|
21
|
-
`platform, or the install skipped optional dependencies (--no-optional, --omit=optional).\n` +
|
|
22
|
-
`Supported platforms: darwin-arm64, darwin-x64, linux-arm64, linux-x64, win32-arm64, win32-x64.`,
|
|
23
|
-
);
|
|
11
|
+
binary = resolveBinary();
|
|
12
|
+
} catch (error) {
|
|
13
|
+
console.error(error.message);
|
|
24
14
|
process.exit(1);
|
|
25
15
|
}
|
|
26
16
|
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export type Expose = "all" | "none" | "export";
|
|
2
|
+
export type JsDoc = "none" | "basic" | "extended";
|
|
3
|
+
export type FunctionOptions = "fail" | "comment" | "hide";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generator options. The keys mirror the `Config` type of
|
|
7
|
+
* ts-json-schema-generator 2.x; each one maps to a flag of the native CLI.
|
|
8
|
+
* Passing any other key throws.
|
|
9
|
+
*/
|
|
10
|
+
export interface Config {
|
|
11
|
+
/** Glob of source files to read, supporting `*` and `**`. */
|
|
12
|
+
path?: string;
|
|
13
|
+
/** Type name(s) to generate a schema for; `"*"` for all exposed types. */
|
|
14
|
+
type?: string | string[];
|
|
15
|
+
/** Path to the tsconfig.json to compile with. Defaults to the nearest one above the working directory. */
|
|
16
|
+
tsconfig?: string;
|
|
17
|
+
/** `$id` of the generated schema. */
|
|
18
|
+
schemaId?: string;
|
|
19
|
+
/** Which types get their own definition. Default `"export"`. */
|
|
20
|
+
expose?: Expose;
|
|
21
|
+
/** How much of the JSDoc annotations to read. Default `"extended"`. */
|
|
22
|
+
jsDoc?: JsDoc;
|
|
23
|
+
/** What to do with function types. Default `"comment"`. */
|
|
24
|
+
functions?: FunctionOptions;
|
|
25
|
+
/** Emit `markdownDescription` alongside `description`. Implies `jsDoc: "extended"`. */
|
|
26
|
+
markdownDescription?: boolean;
|
|
27
|
+
/** Emit the raw JSDoc comment as `fullDescription`. Implies `jsDoc: "extended"`. */
|
|
28
|
+
fullDescription?: boolean;
|
|
29
|
+
/** Sort object properties. Default `true`. */
|
|
30
|
+
sortProps?: boolean;
|
|
31
|
+
/** Emit a top-level `$ref` definition. Default `true`. */
|
|
32
|
+
topRef?: boolean;
|
|
33
|
+
/** Disallow additional items on tuples. Default `false`. */
|
|
34
|
+
strictTuples?: boolean;
|
|
35
|
+
/** Skip type checking, which is faster but reports no diagnostics. Default `false`. */
|
|
36
|
+
skipTypeCheck?: boolean;
|
|
37
|
+
/** Percent-encode characters in `$ref` values. Default `true`. */
|
|
38
|
+
encodeRefs?: boolean;
|
|
39
|
+
/** Allow additional properties on objects without an index signature. Default `false`. */
|
|
40
|
+
additionalProperties?: boolean;
|
|
41
|
+
/** Extra JSDoc tags to copy into the schema as validation keywords. */
|
|
42
|
+
extraTags?: string[];
|
|
43
|
+
/** Accepted and ignored: the schema is returned as an object. */
|
|
44
|
+
minify?: boolean;
|
|
45
|
+
/** Only `"json-schema"` is supported. */
|
|
46
|
+
discriminatorType?: "json-schema";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Generates a JSON schema by running the native CLI, resolving with the parsed
|
|
51
|
+
* schema. Rejects with an `Error` carrying the CLI's `stderr` when it fails.
|
|
52
|
+
*/
|
|
53
|
+
export function generateSchema(config: Config): Promise<Record<string, unknown>>;
|
|
54
|
+
|
|
55
|
+
/** {@link generateSchema}, synchronously. Throws instead of rejecting. */
|
|
56
|
+
export function generateSchemaSync(config: Config): Record<string, unknown>;
|
package/index.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// Programmatic wrapper around the native CLI: config in, parsed schema out.
|
|
2
|
+
// Keys mirror the ts-json-schema-generator 2.x Config type so that "config in →
|
|
3
|
+
// schema out" users can migrate by swapping createGenerator().createSchema()
|
|
4
|
+
// for generateSchema(). Dependency-free CommonJS.
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const { spawn, spawnSync } = require("node:child_process");
|
|
8
|
+
const { resolveBinary } = require("./resolve-binary.js");
|
|
9
|
+
|
|
10
|
+
// spawnSync buffers the whole schema in memory and defaults to 1 MiB, which
|
|
11
|
+
// real projects exceed easily.
|
|
12
|
+
const MAX_BUFFER = 512 * 1024 * 1024;
|
|
13
|
+
|
|
14
|
+
// Each entry translates one config key into CLI arguments. Keys absent from
|
|
15
|
+
// the config, or set to undefined/null, contribute nothing.
|
|
16
|
+
const OPTIONS = {
|
|
17
|
+
path: (value, args) => args.push("--path", string("path", value)),
|
|
18
|
+
type: (value, args) => {
|
|
19
|
+
for (const type of Array.isArray(value) ? value : [value]) {
|
|
20
|
+
args.push("--type", string("type", type));
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
tsconfig: (value, args) => args.push("--tsconfig", string("tsconfig", value)),
|
|
24
|
+
schemaId: (value, args) => args.push("--id", string("schemaId", value)),
|
|
25
|
+
expose: (value, args) => args.push("--expose", string("expose", value)),
|
|
26
|
+
jsDoc: (value, args) => args.push("--jsDoc", string("jsDoc", value)),
|
|
27
|
+
functions: (value, args) => args.push("--functions", string("functions", value)),
|
|
28
|
+
markdownDescription: (value, args) => value && args.push("--markdown-description"),
|
|
29
|
+
fullDescription: (value, args) => value && args.push("--full-description"),
|
|
30
|
+
sortProps: (value, args) => value === false && args.push("--unstable"),
|
|
31
|
+
topRef: (value, args) => value === false && args.push("--no-top-ref"),
|
|
32
|
+
strictTuples: (value, args) => value === true && args.push("--strict-tuples"),
|
|
33
|
+
skipTypeCheck: (value, args) => value === true && args.push("--no-type-check"),
|
|
34
|
+
encodeRefs: (value, args) => value === false && args.push("--no-ref-encode"),
|
|
35
|
+
additionalProperties: (value, args) => value === true && args.push("--additional-properties"),
|
|
36
|
+
extraTags: (value, args) => {
|
|
37
|
+
for (const tag of Array.isArray(value) ? value : [value]) {
|
|
38
|
+
args.push("--validation-keywords", string("extraTags", tag));
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
// The schema is returned as an object, so whitespace in the CLI's output
|
|
42
|
+
// is immaterial.
|
|
43
|
+
minify: () => {},
|
|
44
|
+
discriminatorType: (value) => {
|
|
45
|
+
if (value !== "json-schema") {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`ts-json-schema-generator: discriminatorType ${JSON.stringify(value)} is not supported; ` +
|
|
48
|
+
`the native implementation always emits "json-schema" discriminators.`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function string(key, value) {
|
|
55
|
+
if (typeof value !== "string") {
|
|
56
|
+
throw new Error(`ts-json-schema-generator: ${key} must be a string, got ${typeof value}`);
|
|
57
|
+
}
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function toArgs(config) {
|
|
62
|
+
if (config === null || typeof config !== "object") {
|
|
63
|
+
const got = config === null ? "null" : typeof config;
|
|
64
|
+
throw new Error(`ts-json-schema-generator: config must be an object, got ${got}`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const unsupported = Object.keys(config).filter((key) => !Object.hasOwn(OPTIONS, key));
|
|
68
|
+
if (unsupported.length > 0) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
`ts-json-schema-generator: unsupported config ${unsupported.length === 1 ? "option" : "options"} ` +
|
|
71
|
+
`${unsupported.map((key) => JSON.stringify(key)).join(", ")}.\n` +
|
|
72
|
+
`Supported options: ${Object.keys(OPTIONS).join(", ")}.\n` +
|
|
73
|
+
`Because this package drives the native CLI in a separate process, options that carry ` +
|
|
74
|
+
`JavaScript values cannot work: an existing \`tsProgram\`, and augmentors such as custom ` +
|
|
75
|
+
`parsers, formatters, or SchemaGenerator subclasses. Stay on ts-json-schema-generator 2.x for those.`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const args = [];
|
|
80
|
+
for (const [key, value] of Object.entries(config)) {
|
|
81
|
+
if (value !== undefined && value !== null) {
|
|
82
|
+
OPTIONS[key](value, args);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return args;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function parseSchema(stdout) {
|
|
89
|
+
try {
|
|
90
|
+
return JSON.parse(stdout);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
throw new Error(`ts-json-schema-generator: could not parse the generated schema: ${error.message}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function failed(status, signal, stderr) {
|
|
97
|
+
const reason = signal ? `was killed by ${signal}` : `exited with code ${status}`;
|
|
98
|
+
const detail = stderr.trim();
|
|
99
|
+
const error = new Error(`ts-json-schema-generator ${reason}${detail ? `:\n${detail}` : ""}`);
|
|
100
|
+
error.exitCode = status;
|
|
101
|
+
error.signal = signal ?? null;
|
|
102
|
+
error.stderr = stderr;
|
|
103
|
+
return error;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// generateSchema runs the native CLI and resolves with the parsed schema.
|
|
107
|
+
function generateSchema(config) {
|
|
108
|
+
let args;
|
|
109
|
+
let binary;
|
|
110
|
+
try {
|
|
111
|
+
args = toArgs(config);
|
|
112
|
+
binary = resolveBinary();
|
|
113
|
+
} catch (error) {
|
|
114
|
+
return Promise.reject(error);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return new Promise((resolve, reject) => {
|
|
118
|
+
const child = spawn(binary, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
119
|
+
const stdout = [];
|
|
120
|
+
const stderr = [];
|
|
121
|
+
child.stdout.on("data", (chunk) => stdout.push(chunk));
|
|
122
|
+
child.stderr.on("data", (chunk) => stderr.push(chunk));
|
|
123
|
+
child.on("error", (error) => {
|
|
124
|
+
reject(new Error(`ts-json-schema-generator: failed to run ${binary}: ${error.message}`));
|
|
125
|
+
});
|
|
126
|
+
child.on("close", (status, signal) => {
|
|
127
|
+
if (status !== 0 || signal) {
|
|
128
|
+
reject(failed(status, signal, Buffer.concat(stderr).toString()));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
resolve(parseSchema(Buffer.concat(stdout)));
|
|
133
|
+
} catch (error) {
|
|
134
|
+
reject(error);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// generateSchemaSync is generateSchema without the event loop.
|
|
141
|
+
function generateSchemaSync(config) {
|
|
142
|
+
const args = toArgs(config);
|
|
143
|
+
const binary = resolveBinary();
|
|
144
|
+
|
|
145
|
+
const result = spawnSync(binary, args, { maxBuffer: MAX_BUFFER });
|
|
146
|
+
if (result.error) {
|
|
147
|
+
throw new Error(`ts-json-schema-generator: failed to run ${binary}: ${result.error.message}`);
|
|
148
|
+
}
|
|
149
|
+
if (result.status !== 0 || result.signal) {
|
|
150
|
+
throw failed(result.status, result.signal, result.stderr.toString());
|
|
151
|
+
}
|
|
152
|
+
return parseSchema(result.stdout);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
module.exports = { generateSchema, generateSchemaSync };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-json-schema-generator",
|
|
3
|
-
"version": "3.0.0-native.
|
|
3
|
+
"version": "3.0.0-native.5",
|
|
4
4
|
"description": "Generate JSON schema from your TypeScript sources — native TypeScript 7 CLI",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -22,17 +22,22 @@
|
|
|
22
22
|
"bin": {
|
|
23
23
|
"ts-json-schema-generator": "bin/ts-json-schema-generator.js"
|
|
24
24
|
},
|
|
25
|
+
"main": "index.js",
|
|
26
|
+
"types": "index.d.ts",
|
|
25
27
|
"files": [
|
|
26
28
|
"bin/ts-json-schema-generator.js",
|
|
29
|
+
"index.js",
|
|
30
|
+
"index.d.ts",
|
|
31
|
+
"resolve-binary.js",
|
|
27
32
|
"README.md"
|
|
28
33
|
],
|
|
29
34
|
"optionalDependencies": {
|
|
30
|
-
"ts-json-schema-generator-darwin-arm64": "3.0.0-native.
|
|
31
|
-
"ts-json-schema-generator-darwin-x64": "3.0.0-native.
|
|
32
|
-
"ts-json-schema-generator-linux-arm64": "3.0.0-native.
|
|
33
|
-
"ts-json-schema-generator-linux-x64": "3.0.0-native.
|
|
34
|
-
"ts-json-schema-generator-win32-arm64": "3.0.0-native.
|
|
35
|
-
"ts-json-schema-generator-win32-x64": "3.0.0-native.
|
|
35
|
+
"ts-json-schema-generator-darwin-arm64": "3.0.0-native.5",
|
|
36
|
+
"ts-json-schema-generator-darwin-x64": "3.0.0-native.5",
|
|
37
|
+
"ts-json-schema-generator-linux-arm64": "3.0.0-native.5",
|
|
38
|
+
"ts-json-schema-generator-linux-x64": "3.0.0-native.5",
|
|
39
|
+
"ts-json-schema-generator-win32-arm64": "3.0.0-native.5",
|
|
40
|
+
"ts-json-schema-generator-win32-x64": "3.0.0-native.5"
|
|
36
41
|
},
|
|
37
42
|
"engines": {
|
|
38
43
|
"node": ">=20"
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Locates the native CLI binary for the host. The binary itself lives in a
|
|
2
|
+
// per-platform optional dependency; TS_JSON_SCHEMA_GENERATOR_BINARY overrides
|
|
3
|
+
// the lookup with an explicit path. Dependency-free CommonJS so it runs before
|
|
4
|
+
// anything is built.
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const SUPPORTED = "darwin-arm64, darwin-x64, linux-arm64, linux-x64, win32-arm64, win32-x64";
|
|
8
|
+
|
|
9
|
+
// resolveBinary returns the absolute path of the native CLI, or throws an
|
|
10
|
+
// Error whose message explains why no binary is available.
|
|
11
|
+
function resolveBinary() {
|
|
12
|
+
const override = process.env.TS_JSON_SCHEMA_GENERATOR_BINARY;
|
|
13
|
+
if (override) {
|
|
14
|
+
return override;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const platform = process.platform;
|
|
18
|
+
const arch = process.arch;
|
|
19
|
+
const pkg = `ts-json-schema-generator-${platform}-${arch}`;
|
|
20
|
+
const exe = platform === "win32" ? "bin/ts-json-schema-generator.exe" : "bin/ts-json-schema-generator";
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
return require.resolve(`${pkg}/${exe}`);
|
|
24
|
+
} catch {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`ts-json-schema-generator: no native binary for ${platform}-${arch}.\n` +
|
|
27
|
+
`The optional dependency "${pkg}" is not installed. It may not exist for this ` +
|
|
28
|
+
`platform, or the install skipped optional dependencies (--no-optional, --omit=optional).\n` +
|
|
29
|
+
`Supported platforms: ${SUPPORTED}.`,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = { resolveBinary };
|