typescript-json-schema
Advanced tools
Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "typescript-json-schema", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "typescript-json-schema generates JSON Schema files from your Typescript sources", | ||
"main": "./dist/typescript-json-schema.js", | ||
"main": "typescript-json-schema.js", | ||
"bin": { | ||
@@ -14,7 +14,2 @@ "typescript-json-schema": "./bin/typescript-json-schema" | ||
}, | ||
"dependencies": { | ||
"typescript": "~1.7.5", | ||
"glob": "~6.0.2", | ||
"yargs": "^3.30.0" | ||
}, | ||
"repository": { | ||
@@ -28,5 +23,2 @@ "type": "git", | ||
}], | ||
"scripts": { | ||
"build": "tsc -p ." | ||
}, | ||
"optionalDependencies": {}, | ||
@@ -39,3 +31,17 @@ "keywords": [ | ||
"schema" | ||
] | ||
], | ||
"dependencies": { | ||
"typescript": "~1.8.0", | ||
"glob": "~7.0.0", | ||
"yargs": "^4.1.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"mocha": "^2.4.5", | ||
"source-map-support": "^0.4.0" | ||
}, | ||
"scripts": { | ||
"test": "npm run build && mocha --require source-map-support/register test", | ||
"build": "tsc -p ." | ||
} | ||
} |
# typescript-json-schema | ||
[![npm version](https://img.shields.io/npm/v/typescript-json-schema.svg)](https://www.npmjs.com/package/typescript-json-schema) | ||
[![npm version](https://img.shields.io/npm/v/typescript-json-schema.svg)](https://www.npmjs.com/package/typescript-json-schema) [![Build Status](https://travis-ci.org/YousefED/typescript-json-schema.svg?branch=master)](https://travis-ci.org/YousefED/typescript-json-schema) | ||
@@ -5,0 +5,0 @@ Generate json-schemas from your Typescript sources. |
@@ -15,3 +15,2 @@ { | ||
"suppressImplicitAnyIndexErrors": true, | ||
"outDir": "dist/", | ||
"sourceMap": true | ||
@@ -25,3 +24,4 @@ }, | ||
"node_modules", | ||
"example" | ||
"example", | ||
"test/programs" | ||
], | ||
@@ -31,5 +31,9 @@ "compileOnSave": true, | ||
"files": [ | ||
"test/schema.test.ts", | ||
"typescript-json-schema.ts", | ||
"typings/assertion-error/assertion-error.d.ts", | ||
"typings/chai/chai.d.ts", | ||
"typings/glob/glob.d.ts", | ||
"typings/minimatch/minimatch.d.ts", | ||
"typings/mocha/mocha.d.ts", | ||
"typings/node/node.d.ts", | ||
@@ -36,0 +40,0 @@ "typings/typescript/typescript.d.ts" |
@@ -5,5 +5,13 @@ import * as ts from "typescript"; | ||
var vm = require("vm"); | ||
const vm = require("vm"); | ||
export module TJS { | ||
const defaultArgs = { | ||
useRef: true, | ||
useRootRef: false, | ||
useTitle: false, | ||
useDefaultProperties: false, | ||
usePropertyOrder: false | ||
}; | ||
class JsonSchemaGenerator { | ||
@@ -27,3 +35,3 @@ private static validationKeywords = [ | ||
constructor(allSymbols: { [name: string]: ts.Type }, inheritingTypes: { [baseName: string]: string[] }, tc: ts.TypeChecker, private useRef: boolean = false, private useRootRef: boolean = false) { | ||
constructor(allSymbols: { [name: string]: ts.Type }, inheritingTypes: { [baseName: string]: string[] }, tc: ts.TypeChecker, private args = defaultArgs) { | ||
this.allSymbols = allSymbols; | ||
@@ -162,3 +170,5 @@ this.inheritingTypes = inheritingTypes; | ||
let definition: any = this.getDefinitionForType(propertyType, tc); | ||
definition.title = propertyName; | ||
if (this.args.useTitle) { | ||
definition.title = propertyName; | ||
} | ||
@@ -211,3 +221,3 @@ const comments = prop.getDocumentationComment(); | ||
private getEnumDefinition(clazzType: ts.Type, tc: ts.TypeChecker, asRef = this.useRef): any { | ||
private getEnumDefinition(clazzType: ts.Type, tc: ts.TypeChecker, asRef = this.args.useRef): any { | ||
const node = clazzType.getSymbol().getDeclarations()[0]; | ||
@@ -262,3 +272,3 @@ const fullName = tc.typeToString(clazzType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType); | ||
private getClassDefinition(clazzType: ts.Type, tc: ts.TypeChecker, asRef = this.useRef): any { | ||
private getClassDefinition(clazzType: ts.Type, tc: ts.TypeChecker, asRef = this.args.useRef): any { | ||
const node = clazzType.getSymbol().getDeclarations()[0]; | ||
@@ -296,10 +306,17 @@ const clazz = <ts.ClassDeclaration>node; | ||
const definition = { | ||
let definition: any = { | ||
type: "object", | ||
title: fullName, | ||
defaultProperties: [], // TODO: set via comment or parameter instead of hardcode here, json-editor specific | ||
properties: propertyDefinitions, | ||
propertyOrder: propertyOrder | ||
properties: propertyDefinitions | ||
}; | ||
if (this.args.useTitle) { | ||
definition.title = fullName; | ||
} | ||
if (this.args.useDefaultProperties) { | ||
definition.defaultProperties = []; | ||
} | ||
if (this.args.usePropertyOrder) { | ||
definition.propertyOrder = propertyOrder; | ||
} | ||
if (asRef) { | ||
@@ -317,5 +334,5 @@ this.reffedDefinitions[fullName] = definition; | ||
public getClassDefinitionByName(clazzName: string, includeReffedDefinitions: boolean = true): any { | ||
let def = this.getClassDefinition(this.allSymbols[clazzName], this.tc, this.useRootRef); | ||
let def = this.getClassDefinition(this.allSymbols[clazzName], this.tc, this.args.useRootRef); | ||
if (this.useRef && includeReffedDefinitions) { | ||
if (this.args.useRef && includeReffedDefinitions && Object.keys(this.reffedDefinitions).length > 0) { | ||
def.definitions = this.reffedDefinitions; | ||
@@ -328,3 +345,3 @@ } | ||
export function generateSchema(compileFiles: string[], fullTypeName: string, useRef: boolean, useRootRef: boolean) { | ||
export function generateSchema(compileFiles: string[], fullTypeName: string, args = defaultArgs) { | ||
const options: ts.CompilerOptions = { noEmit: true, emitDecoratorMetadata: true, experimentalDecorators: true, target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS }; | ||
@@ -340,3 +357,2 @@ const program = ts.createProgram(compileFiles, options); | ||
if (diagnostics.length == 0) { | ||
@@ -374,3 +390,3 @@ | ||
const generator = new JsonSchemaGenerator(allSymbols, inheritingTypes, tc, useRef, useRootRef); | ||
const generator = new JsonSchemaGenerator(allSymbols, inheritingTypes, tc, args); | ||
let definition = generator.getClassDefinitionByName(fullTypeName); | ||
@@ -384,7 +400,6 @@ definition["$schema"] = "http://json-schema.org/draft-04/schema#"; | ||
export function exec(filePattern: string, fullTypeName: string, useRef: boolean, useRootRef: boolean) { | ||
export function exec(filePattern: string, fullTypeName: string, args) { | ||
const files: string[] = glob.sync(filePattern); | ||
const definition = TJS.generateSchema(files, fullTypeName, useRef, useRootRef); | ||
console.log(JSON.stringify(definition, null, 4)); | ||
//fs.writeFile(outFile, JSON.stringify(definition, null, 4)); | ||
const definition = TJS.generateSchema(files, fullTypeName, args); | ||
process.stdout.write(JSON.stringify(definition, null, 4) + "\n"); | ||
} | ||
@@ -395,12 +410,24 @@ | ||
var args = require('yargs') | ||
var args = require("yargs") | ||
.usage(helpText) | ||
.demand(2) | ||
.boolean('r').alias('r', 'refs').default('r', true) | ||
.describe('r', 'Create shared ref definitions.') | ||
.boolean('t').alias('t', 'topRef').default('t', false) | ||
.describe('t', 'Create a top-level ref definition.') | ||
.boolean("refs").default("refs", defaultArgs.useRef) | ||
.describe("refs", "Create shared ref definitions.") | ||
.boolean("topRef").default("topRef", defaultArgs.useRootRef) | ||
.describe("topRef", "Create a top-level ref definition.") | ||
.boolean("titles").default("titles", defaultArgs.useTitle) | ||
.describe("titles", "Creates titles in the output schema.") | ||
.boolean("defaultProps").default("defaultProps", defaultArgs.useDefaultProperties) | ||
.describe("defaultProps", "Create default properties definitions.") | ||
.boolean("propOrder").default("propOrder", defaultArgs.usePropertyOrder) | ||
.describe("propOrder", "Create property order definitions.") | ||
.argv; | ||
exec(args._[0], args._[1], args.r, args.t); | ||
exec(args._[0], args._[1], { | ||
useRef: args.refs, | ||
useRootRef: args.topRef, | ||
useTitle: args.titles, | ||
useDefaultProperties: args.defaultProps, | ||
usePropertyOrder: args.propOrder | ||
}); | ||
} | ||
@@ -410,3 +437,3 @@ } | ||
if (typeof window === "undefined" && require.main === module) { | ||
TJS.run(); | ||
TJS.run(); | ||
} | ||
@@ -413,0 +440,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
226540
35
5114
3
+ Addedcamelcase@3.0.0(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedfind-up@1.1.2(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-caller-file@1.0.3(transitive)
+ Addedglob@7.0.6(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhosted-git-info@2.8.9(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-utf8@0.2.1(transitive)
+ Addedload-json-file@1.1.0(transitive)
+ Addedlodash.assign@4.2.0(transitive)
+ Addednormalize-package-data@2.5.0(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-exists@2.1.0(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpath-type@1.1.0(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedread-pkg@1.1.0(transitive)
+ Addedread-pkg-up@1.0.1(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@1.0.1(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedspdx-correct@3.2.0(transitive)
+ Addedspdx-exceptions@2.5.0(transitive)
+ Addedspdx-expression-parse@3.0.1(transitive)
+ Addedspdx-license-ids@3.0.20(transitive)
+ Addedstrip-bom@2.0.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedtypescript@1.8.10(transitive)
+ Addedvalidate-npm-package-license@3.0.4(transitive)
+ Addedwhich-module@1.0.0(transitive)
+ Addedwindow-size@0.2.0(transitive)
+ Addedyargs@4.8.1(transitive)
+ Addedyargs-parser@2.4.1(transitive)
- Removedcamelcase@2.1.1(transitive)
- Removedglob@6.0.4(transitive)
- Removedtypescript@1.7.5(transitive)
- Removedwindow-size@0.1.4(transitive)
- Removedyargs@3.32.0(transitive)
Updatedglob@~7.0.0
Updatedtypescript@~1.8.0
Updatedyargs@^4.1.0