Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ const schema = TJS.generateSchema(program, "MyType", settings);

const generator = TJS.buildGenerator(program, settings);

// generator can be also reused to speed up generating the schema if usecase allows:
const schemaWithReusedGenerator = TJS.generateSchema(program, "MyType", settings, [], generator);

// all symbols
const symbols = generator.getUserSymbols();

Expand Down
2 changes: 1 addition & 1 deletion test/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { exec, getDefaultArgs } from "../typescript-json-schema";

describe("error", () => {
it("error-check", async () => {
try {
try {
await exec("test/programs/dates/", "MyObject", getDefaultArgs());
assert.fail("Expected exec to fail");
} catch (err) {
Expand Down
24 changes: 12 additions & 12 deletions test/out-option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { assert } from "chai";
import { exec, getDefaultArgs } from "../typescript-json-schema";

describe("out option", () => {
beforeEach(() => new Promise((resolve, reject) => {
require("fs").rm(
"./dist/test/doesnotexist",
{ recursive: true, force: true },
(err: Error) => (err ? reject(err) : resolve(null))
);
}));
beforeEach(
() =>
new Promise((resolve, reject) => {
require("fs").rm("./dist/test/doesnotexist", { recursive: true, force: true }, (err: Error) =>
err ? reject(err) : resolve(null)
);
})
);
it("should create parent directory when necessary", async () => {
try {
await exec(
"test/programs/interface-single/main.ts",
"MyObject",
{ ...getDefaultArgs(), out: "./dist/test/doesnotexist/schema.json" }
);
await exec("test/programs/interface-single/main.ts", "MyObject", {
...getDefaultArgs(),
out: "./dist/test/doesnotexist/schema.json",
});
} catch (err) {
assert.fail(`Execution should not have failed: ${err.stack}`);
}
Expand Down
2 changes: 1 addition & 1 deletion test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ajv = new Ajv({
},
},
// TODO: enable strict mode
strict: false
strict: false,
});

addFormats(ajv);
Expand Down
9 changes: 5 additions & 4 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1593,9 +1593,10 @@ export function generateSchema(
program: ts.Program,
fullTypeName: string,
args: PartialArgs = {},
onlyIncludeFiles?: string[]
onlyIncludeFiles?: string[],
externalGenerator?: JsonSchemaGenerator
): Definition | null {
const generator = buildGenerator(program, args, onlyIncludeFiles);
const generator = externalGenerator ?? buildGenerator(program, args, onlyIncludeFiles);

if (generator === null) {
return null;
Expand Down Expand Up @@ -1684,7 +1685,7 @@ export async function exec(filePattern: string, fullTypeName: string, args = get
if (mkErr) {
return reject(new Error("Unable to create parent directory for output file: " + mkErr.message));
}
fs.writeFile(args.out, json, function(wrErr: Error) {
fs.writeFile(args.out, json, function (wrErr: Error) {
if (wrErr) {
return reject(new Error("Unable to write output file: " + wrErr.message));
}
Expand All @@ -1695,7 +1696,7 @@ export async function exec(filePattern: string, fullTypeName: string, args = get
} else {
const hasBeenBuffered = process.stdout.write(json);
if (hasBeenBuffered) {
return new Promise(resolve => process.stdout.on("drain", () => resolve()));
return new Promise((resolve) => process.stdout.on("drain", () => resolve()));
}
}
}