How to use the typescript-json-schema.buildGenerator function in typescript-json-schema

To help you get started, we’ve selected a few typescript-json-schema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github intl-wc / intl / packages / utils / src / generate-schemas.ts View on Github external
export default async function generateSchemas() {
    const program = TJS.getProgramFromFiles([resolve('.', 'intl.config.ts')], null);
    const settings: TJS.PartialArgs = {
        ignoreErrors: false,
        required: true
    }
    // const schema = TJS.generateSchema(program, 'Schema');
    const generator = TJS.buildGenerator(program, settings);
    if (generator) {
        // const symbols = generator.getUserSymbols();
        // const symbols = generator.getMainFileSymbols(program);
        const schema = generator.getSchemaForSymbol('Schema', false);
        let generatedSchema = { ...schema };
        let lazyRefs = new Map();
        
        if (schema.properties) {
            // Filter lazy props, get reference to their interfaces
            const lazy = Object.entries(schema.properties).filter(lazyProps())
            for (let [key, value] of lazy) {
                const lazyRef = value.properties.interface['$ref'].split('/').pop();
                lazyRefs.set(key, generator.getSchemaForSymbol(lazyRef, false));
                
                const newValue = { ...value };
                delete newValue.properties.interface;
github Bearer / bearer-js / packages / cli / src / utils / generators.ts View on Github external
async build() {
    const files = await globby(`${this.srcFunctionsDir}/*.ts`)
    const programGenerator = TJS.getProgramFromFiles(
      files,
      {
        ...config.config.compilerOptions,
        allowUnusedLabels: true,
        // be indulgent
        noUnusedParameters: false,
        noUnusedLocals: false
      },
      './ok'
    )

    const generator = TJS.buildGenerator(programGenerator, {
      required: true
    })

    if (!generator) {
      throw new Error('Please fix above issues before')
    }

    files.forEach(file => {
      const sourceFile = ts.createSourceFile(
        file,
        fs.readFileSync(file, 'utf8'),
        ts.ScriptTarget.Latest,
        false,
        ts.ScriptKind.TSX
      )
      ts.transform(sourceFile, [transformer(generator)])
github natergj / excel4node / scripts / generateJsonSchemas.ts View on Github external
import * as path from 'path';
import * as fs from 'fs';
import * as TJS from 'typescript-json-schema';

const settings: TJS.PartialArgs = {
  required: true,
};

const compilerOptions: TJS.CompilerOptions = {
  strictNullChecks: true,
  required: true,
};

const entryFile = path.resolve(__dirname, '../src/index.ts');
const program = TJS.getProgramFromFiles([entryFile], compilerOptions);
const generator = TJS.buildGenerator(program, settings);

if (generator) {
  const symbols = generator.getSymbols();
  symbols.forEach(symbol => {
    if (
      symbol.fullyQualifiedName.includes('excel4node') &&
      symbol.fullyQualifiedName.includes('types') &&
      !symbol.fullyQualifiedName.includes('node_modules')
    ) {
      const outFile = getSchemaPath(symbol.fullyQualifiedName);
      const schema = TJS.generateSchema(program, symbol.name, settings);
      fs.writeFileSync(outFile, JSON.stringify(schema, null, '  '));
    }
  });
}
process.exit();
github cytle / faker-ts / lib / tsSchema.ts View on Github external
export function tsSchemaGenerator(files: string[], jsonCompilerOptions?: ts.CompilerOptions, basePath?: string) {
  const options = getTsOptions(jsonCompilerOptions, basePath);
  const settings: PartialArgs = {
    required: true,
  };
  const program = ts.createProgram(files, options);
  return buildGenerator(program, settings);
}
github cytle / faker-ts / lib / tsMock.ts View on Github external
public setProgram(program: ts.Program) {
    const generator = buildGenerator(program, { required: true });
    if (generator === null) {
      throw new Error('generator is null');
    }
    this.program = program;
    this.generator = generator;
  }
  public generateSchema(fullTypeName: string = '*', onlyIncludeFiles?: string[]) {
github Bearer / bearer-js / packages / transpiler / src / index.ts View on Github external
}

    const parsed = ts.parseJsonConfigFileContent(config, ts.sys, this.VIEWS_DIRECTORY)
    this.rootFileNames = parsed.fileNames

    if (!this.rootFileNames.length) {
      logger('No file to transpile')
    }

    const program = TJS.getProgramFromFiles(
      this.rootFileNames,
      { ...config.config.compilerOptions },
      this.ROOT_DIRECTORY
    )

    this.generator = TJS.buildGenerator(program, settings)
    const servicesHost: ts.LanguageServiceHost = {
      getScriptFileNames: () => this.rootFileNames,
      getScriptVersion: fileName => this.files[fileName] && this.files[fileName].version.toString(),
      getScriptSnapshot: fileName => {
        if (!fs.existsSync(fileName)) {
          return null
        }

        return ts.ScriptSnapshot.fromString(fs.readFileSync(fileName).toString())
      },
      getCurrentDirectory: () => process.cwd(),
      getCompilationSettings: () => this.compilerOptions,
      getDefaultLibFileName: options => ts.getDefaultLibFilePath(options),
      getCustomTransformers: () => this.transformers,
      fileExists: ts.sys.fileExists,
      readFile: ts.sys.readFile,
github cytle / faker-ts / lib / tsSchema.ts View on Github external
tsWatcher(files, options).on('afterProgramCreate', (p: ts.SemanticDiagnosticsBuilderProgram) => {
    program = p.getProgram();
    const generator = buildGenerator(program, settings);
    if (generator) {
      mockGenerator = generator;
    }
  });
  function getMockGenerator() {