From a5853ea3be5aeeb361969ecc7e7f6ec2a761831f Mon Sep 17 00:00:00 2001 From: Marek Urbanowicz Date: Tue, 13 Apr 2021 16:24:43 +0200 Subject: [PATCH] Allow to pass the generator to generateSchema function --- README.md | 3 +++ test/error.test.ts | 2 +- test/out-option.test.ts | 24 ++++++++++++------------ test/schema.test.ts | 2 +- typescript-json-schema.ts | 9 +++++---- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 890e2f59..04db918a 100644 --- a/README.md +++ b/README.md @@ -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(); diff --git a/test/error.test.ts b/test/error.test.ts index 7209e07a..b5ed3ce4 100644 --- a/test/error.test.ts +++ b/test/error.test.ts @@ -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) { diff --git a/test/out-option.test.ts b/test/out-option.test.ts index 5694d78b..330c1db9 100644 --- a/test/out-option.test.ts +++ b/test/out-option.test.ts @@ -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}`); } diff --git a/test/schema.test.ts b/test/schema.test.ts index 1a2a50c4..595fbd2a 100644 --- a/test/schema.test.ts +++ b/test/schema.test.ts @@ -18,7 +18,7 @@ const ajv = new Ajv({ }, }, // TODO: enable strict mode - strict: false + strict: false, }); addFormats(ajv); diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 5a9a6a03..42fd9f70 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -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; @@ -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)); } @@ -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())); } } }