-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Guidewire/init
Migration to GW
- Loading branch information
Showing
17 changed files
with
9,995 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: CI | ||
on: [push] | ||
jobs: | ||
build: | ||
name: Lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }} | ||
|
||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
node: ['14.x'] | ||
os: [windows-latest, macOS-latest] | ||
|
||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v2 | ||
|
||
- name: Use Node ${{ matrix.node }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node }} | ||
|
||
- name: Install deps and build (with cache) | ||
uses: bahmutov/npm-install@v1 | ||
|
||
- name: Lint | ||
run: yarn lint | ||
|
||
- name: Test | ||
run: yarn test --ci --coverage --maxWorkers=2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*.log | ||
.DS_Store | ||
node_modules | ||
.cache | ||
dist | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Guidewire Software | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,42 @@ | ||
# ts-to-json-schema-tests | ||
# Generating JSON Schema from TypeScript types | ||
|
||
Test JSON Schema generation libraries capabilities. | ||
|
||
The purpose of this repository is to show if different | ||
TypeScript to JSON Schema generating libraries | ||
can handle anything more complicated. | ||
|
||
## Testing approach | ||
|
||
In `src` directory are the library sources to be generated. There are 2 types tests: | ||
|
||
- `simple.ts` contains very simple tests case without any external libraries | ||
- `index.tsx` contains example React component with many different possible React types (from [typescript-cheatsheets](https://github.com/typescript-cheatsheets/react#basic-prop-types-examples)) | ||
|
||
For each tested package, there is generator script in `generators` directory. To run it use: | ||
|
||
```shell | ||
yarn generate:<tested-package-name> | ||
``` | ||
|
||
The generator should generate types for `Simple` and `ExampleProps` types. Output will be stored in `schemas` directory if generation succeeded under following format: | ||
|
||
``` | ||
<tested-package-name>-<type-name>.json | ||
``` | ||
|
||
## Installation | ||
|
||
Install dependencies using `yarn`. | ||
|
||
```shell | ||
yarn | ||
``` | ||
|
||
## Results | ||
|
||
| Library | Command | `Simple` | `ExampleProps` | | ||
| ---------------------------------------------------------------------------- | ---------------------------------------- | -------- | -------------- | | ||
| [typescript-json-schema](https://github.com/YousefED/typescript-json-schema) | `yarn generate:typescript-json-schema` | ✅ | ❌ | | ||
| [ts-json-schema-generator](https://github.com/vega/ts-json-schema-generator) | `yarn generate:ts-json-schema-generator` | ❌ | ❌ | | ||
| [quicktype](https://github.com/quicktype/quicktype) | `yarn generate:quicktype` | ✅ | ❌ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { writeFileSync } from 'fs'; | ||
import * as path from 'path'; | ||
|
||
export type Generate = (source: string, typeName: string) => Promise<unknown>; | ||
export function getGenerate(name: string, generator: Generate): Generate { | ||
return async function generate(source, typeName) { | ||
let schema; | ||
|
||
const getLogMessage = (message: string) => `${name}: ${message}`; | ||
|
||
console.log(getLogMessage(`Generating schema for ${typeName} type.`)); | ||
try { | ||
schema = await generator(source, typeName); | ||
} catch (e) { | ||
console.error( | ||
getLogMessage(`Failed to generate schema for ${typeName}.`) | ||
); | ||
throw e; | ||
} | ||
|
||
const saveName = `${name}-${typeName}.json`; | ||
const schemaString = JSON.stringify(schema, null, 2); | ||
const destination = path.resolve('schemas', saveName); | ||
writeFileSync(destination, schemaString); | ||
console.log( | ||
getLogMessage(`Schema for ${typeName} saved to ${destination}.`) | ||
); | ||
|
||
console.log( | ||
getLogMessage(`Successfully generated schema for ${typeName}.`) | ||
); | ||
}; | ||
} | ||
|
||
export async function runAsync(callback: () => Promise<void>) { | ||
try { | ||
await callback(); | ||
} catch (e) { | ||
console.error(e); | ||
process.exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
quicktype, | ||
InputData, | ||
JSONSchemaInput, | ||
JSONSchemaStore, | ||
JSONSchemaSourceData, | ||
} from 'quicktype-core'; | ||
import { resolve } from 'path'; | ||
import { schemaForTypeScriptSources } from 'quicktype-typescript-input'; | ||
import * as path from 'path'; | ||
import { readFileSync } from 'fs'; | ||
import { getGenerate, runAsync } from './helpers'; | ||
|
||
export interface SchemaTypeSource extends JSONSchemaSourceData { | ||
kind: 'schema'; | ||
} | ||
|
||
function makeTypeScriptSource(fileNames: string[]) { | ||
const sources: { [fileName: string]: string } = {}; | ||
|
||
for (const fileName of fileNames) { | ||
const baseName = path.basename(fileName); | ||
sources[baseName] = readFileSync(fileName, 'utf8'); | ||
} | ||
|
||
return Object.assign( | ||
{ kind: 'schema' }, | ||
schemaForTypeScriptSources(sources) | ||
) as SchemaTypeSource; | ||
} | ||
|
||
async function quicktypeTS( | ||
targetLanguage: string, | ||
typeName: string, | ||
tsSource: string | ||
) { | ||
const tsInput = makeTypeScriptSource([tsSource]); | ||
const inputData = new InputData(); | ||
await inputData.addSource( | ||
'schema', | ||
tsInput, | ||
// @ts-expect-error | ||
() => new JSONSchemaInput(new JSONSchemaStore()) | ||
); | ||
return quicktype({ | ||
inputData, | ||
lang: targetLanguage, | ||
}); | ||
} | ||
|
||
const quicktypeGenerate = getGenerate('quicktype', async (source, typeName) => { | ||
const result = await quicktypeTS('schema', typeName, source); | ||
return JSON.parse(result.lines.join('')); | ||
}); | ||
|
||
runAsync(async () => { | ||
await quicktypeGenerate(resolve('./src/simple.ts'), 'Simple'); | ||
|
||
await quicktypeGenerate(resolve('./src/index.tsx'), 'ExampleProps'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as TSJ from 'ts-json-schema-generator'; | ||
import { resolve } from 'path'; | ||
import { getGenerate, runAsync } from './helpers'; | ||
|
||
// https://github.com/vega/ts-json-schema-generator | ||
|
||
const tsJsonSchemaGeneratorGenerate = getGenerate( | ||
'ts-json-schema-generator', | ||
async (source, typeName) => { | ||
const config = { | ||
path: source, | ||
tsconfig: 'tsconfig.json', | ||
type: typeName, // Or <type-name> if you want to generate schema for that one type only | ||
}; | ||
|
||
return TSJ.createGenerator(config).createSchema(config.type); | ||
} | ||
); | ||
|
||
runAsync(async () => { | ||
await tsJsonSchemaGeneratorGenerate(resolve('./src/simple.ts'), 'Simple'); | ||
|
||
await tsJsonSchemaGeneratorGenerate(resolve('./src/index.tsx'), 'ExampleProps'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { parse } from 'json5'; | ||
import { resolve } from 'path'; | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
|
||
import * as TJS from 'typescript-json-schema'; | ||
import { getGenerate, runAsync } from './helpers'; | ||
|
||
// https://github.com/YousefED/typescript-json-schema | ||
|
||
const tsconfig = parse(readFileSync('./tsconfig.json', 'utf8')); | ||
|
||
// optionally pass argument to schema generator | ||
const settings: TJS.PartialArgs = { | ||
required: true, | ||
include: tsconfig.include, | ||
ignoreErrors: true, | ||
}; | ||
|
||
const compilerOptions: TJS.CompilerOptions = { | ||
...tsconfig.compilerOptions, | ||
}; | ||
|
||
const typescriptJsonSchemaGenerate = getGenerate( | ||
'typescript-json-schema', | ||
async (source, typeName) => { | ||
const program = TJS.getProgramFromFiles([source], compilerOptions); | ||
|
||
return TJS.generateSchema(program, typeName, settings); | ||
} | ||
); | ||
|
||
runAsync(async () => { | ||
await typescriptJsonSchemaGenerate(resolve('./src/simple.ts'), 'Simple'); | ||
|
||
await typescriptJsonSchemaGenerate(resolve('./src/index.tsx'), 'ExampleProps'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{ | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"scripts": { | ||
"start": "tsdx watch", | ||
"build": "tsdx build", | ||
"test": "tsdx test --passWithNoTests", | ||
"lint": "tsdx lint", | ||
"size": "size-limit", | ||
"analyze": "size-limit --why", | ||
"generate:ts-json-schema-generator": "yarn ts-node generators/ts-json-schema-generator.ts", | ||
"generate:typescript-json-schema": "yarn ts-node generators/typescript-json-schema.ts", | ||
"generate:quicktype": "yarn ts-node generators/quicktype.ts" | ||
}, | ||
"peerDependencies": { | ||
"react": ">=16" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "tsdx lint" | ||
} | ||
}, | ||
"prettier": { | ||
"printWidth": 80, | ||
"semi": true, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
}, | ||
"name": "ts-to-json-schema-tests", | ||
"author": "Dariusz Rzepka", | ||
"module": "dist/ts-to-json-schema-tests.esm.js", | ||
"size-limit": [ | ||
{ | ||
"path": "dist/ts-to-json-schema-tests.cjs.production.min.js", | ||
"limit": "10 KB" | ||
}, | ||
{ | ||
"path": "dist/ts-to-json-schema-tests.esm.js", | ||
"limit": "10 KB" | ||
} | ||
], | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^4.6.2", | ||
"@types/fs-extra": "^9.0.2", | ||
"@types/react": "^16.9.53", | ||
"@types/react-dom": "^16.9.8", | ||
"fs-extra": "^9.0.1", | ||
"husky": "^4.3.0", | ||
"quicktype-core": "^6.0.69", | ||
"quicktype-typescript-input": "^0.0.11", | ||
"react": "^17.0.1", | ||
"react-dom": "^17.0.1", | ||
"size-limit": "^4.6.2", | ||
"ts-json-schema-generator": "^0.77.0", | ||
"ts-node": "^9.0.0", | ||
"tsdx": "^0.14.1", | ||
"tslib": "^2.0.3", | ||
"typescript": "^4.0.3", | ||
"typescript-json-schema": "^0.43.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"Simple": { | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"title": "name" | ||
}, | ||
"count": { | ||
"type": "number", | ||
"title": "count" | ||
}, | ||
"optional": { | ||
"type": "number", | ||
"title": "optional" | ||
}, | ||
"callback": { | ||
"type": "object", | ||
"title": "callback" | ||
} | ||
}, | ||
"required": [ | ||
"callback", | ||
"count", | ||
"name" | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.