Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add ES Module support (fix #551) #601

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ npm-debug.log
node_modules
*.js
*.map
dist/
dist_tests/
dist-cjs/
dist-esm/
yarn-error.log
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ node_modules
*.map
example
test
dist/test
dist-esm/test
circle.yml
ARCHITECTURE.md
CONTRIBUTING.md
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/node_modules/.bin/ava ./dist/test/test.js",
"program": "${workspaceRoot}/node_modules/.bin/ava ./dist-esm/test/test.js",
"cwd": "${workspaceRoot}"
},
{
Expand Down
20 changes: 17 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
"name": "json-schema-to-typescript",
"version": "14.0.5",
"description": "compile json schema to typescript typings",
"main": "dist/src/index.js",
"main": "dist-cjs/src/index.js",
"exports": {
"require": "./dist-cjs/src/index.js",
"import": "./dist-esm/src/index.js"
},
"bin": {
"json2ts": "dist/src/cli.js"
"json2ts": "dist-esm/src/cli.js"
},
"typings": "dist/src/index.d.ts",
"type": "module",
"typings": "dist-esm/src/index.d.ts",
"engines": {
"node": ">=16.0.0"
},
"scripts": {
"build": "npm run lint && npm run clean && npm run build:browser && npm run build:server",
"build:browser": "browserify src/index.ts -s jstt -p tsify > dist/bundle.js",
"build:server": "tsc -d",
"clean": "shx rm -rf dist && mkdir dist",
"build:browser": "browserify src/index.ts -s jstt -p tsify > dist-esm/bundle.js",
"build:server": "tsc -p ./tsconfig.cjs.json -d && tsc -p ./tsconfig.esm.json -d",
"clean": "shx rm -rf dist-cjs && shx rm -rf dist-esm",
"format": "prettier \"{src,test}/*.ts\" --write",
"format-check": "prettier \"{src,test}/*.ts\" --check",
"lint": "eslint src/*.ts test/*.ts",
Expand All @@ -23,7 +28,7 @@
"stresstest": "seq 1 10 | xargs -I{} npm test",
"prepublishOnly": "npm test",
"pre-test": "npm run clean && npm run format-check && npm run build:server",
"watch": "tsc -w",
"watch": "tsc -p ./tsconfig.esm.json -w",
"watch:test": "ava -w"
},
"repository": {
Expand All @@ -50,12 +55,12 @@
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^11.5.5",
"@types/json-schema": "^7.0.15",
"@types/lodash": "^4.17.0",
"@types/lodash-es": "^4.17.12",
"cli-color": "^2.0.4",
"glob": "^10.3.12",
"is-glob": "^4.0.3",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"minimist": "^1.2.8",
"mkdirp": "^3.0.1",
"node-fetch": "^3.3.2",
Expand Down Expand Up @@ -85,7 +90,7 @@
},
"ava": {
"files": [
"./dist/test/test.js"
"./dist-esm/test/test.js"
],
"snapshotDir": "./test/__snapshots__"
},
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import * as mkdirp from 'mkdirp'
import {glob} from 'glob'
import isGlob from 'is-glob'
import {join, resolve, dirname} from 'path'
import {compile, DEFAULT_OPTIONS, Options} from './index'
import {pathTransform, error, parseFileAsJSONSchema, justName} from './utils'
import {compile, DEFAULT_OPTIONS, Options} from './index.js'
import {pathTransform, error, parseFileAsJSONSchema, justName} from './utils.js'

main(
minimist(process.argv.slice(2), {
Expand Down Expand Up @@ -161,7 +161,7 @@ async function readStream(stream: NodeJS.ReadStream): Promise<string> {
}

function printHelp() {
const pkg = require('../../package.json')
const pkg = JSON.parse(readFileSync('../../package.json', 'utf8'))

process.stdout.write(
`
Expand Down
2 changes: 1 addition & 1 deletion src/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {format as prettify} from 'prettier'
import {Options} from './'
import {Options} from './index.js'

export async function format(code: string, options: Options): Promise<string> {
if (!options.format) {
Expand Down
8 changes: 4 additions & 4 deletions src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {memoize, omit} from 'lodash'
import {DEFAULT_OPTIONS, Options} from './index'
import {memoize, omit} from 'lodash-es'
import {DEFAULT_OPTIONS, Options} from './index.js'
import {
AST,
ASTWithStandaloneName,
Expand All @@ -13,8 +13,8 @@ import {
TNamedInterface,
TUnion,
T_UNKNOWN,
} from './types/AST'
import {log, toSafeString} from './utils'
} from './types/AST.js'
import {log, toSafeString} from './utils.js'

export function generate(ast: AST, options = DEFAULT_OPTIONS): string {
return (
Expand Down
26 changes: 13 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {readFileSync} from 'fs'
import {JSONSchema4} from 'json-schema'
import {ParserOptions as $RefOptions} from '@apidevtools/json-schema-ref-parser'
import {cloneDeep, endsWith, merge} from 'lodash'
import {cloneDeep, endsWith, merge} from 'lodash-es'
import {dirname} from 'path'
import {Options as PrettierOptions} from 'prettier'
import {format} from './formatter'
import {generate} from './generator'
import {normalize} from './normalizer'
import {optimize} from './optimizer'
import {parse} from './parser'
import {dereference} from './resolver'
import {error, stripExtension, Try, log, parseFileAsJSONSchema} from './utils'
import {validate} from './validator'
import {format} from './formatter.js'
import {generate} from './generator.js'
import {normalize} from './normalizer.js'
import {optimize} from './optimizer.js'
import {parse} from './parser.js'
import {dereference} from './resolver.js'
import {error, stripExtension, Try, log, parseFileAsJSONSchema} from './utils.js'
import {validate} from './validator.js'
import {isDeepStrictEqual} from 'util'
import {link} from './linker'
import {validateOptions} from './optionValidator'
import {JSONSchema as LinkedJSONSchema} from './types/JSONSchema'
import {link} from './linker.js'
import {validateOptions} from './optionValidator.js'
import {JSONSchema as LinkedJSONSchema} from './types/JSONSchema.js'

export {EnumJSONSchema, JSONSchema, NamedEnumJSONSchema, CustomTypeJSONSchema} from './types/JSONSchema'
export {EnumJSONSchema, JSONSchema, NamedEnumJSONSchema, CustomTypeJSONSchema} from './types/JSONSchema.js'

export interface Options {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/linker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {JSONSchema, Parent, LinkedJSONSchema} from './types/JSONSchema'
import {isPlainObject} from 'lodash'
import {JSONSchema, Parent, LinkedJSONSchema} from './types/JSONSchema.js'
import {isPlainObject} from 'lodash-es'
import {JSONSchema4Type} from 'json-schema'

/**
Expand Down
8 changes: 4 additions & 4 deletions src/normalizer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {JSONSchemaTypeName, LinkedJSONSchema, NormalizedJSONSchema, Parent} from './types/JSONSchema'
import {appendToDescription, escapeBlockComment, isSchemaLike, justName, toSafeString, traverse} from './utils'
import {Options} from './'
import {DereferencedPaths} from './resolver'
import {JSONSchemaTypeName, LinkedJSONSchema, NormalizedJSONSchema, Parent} from './types/JSONSchema.js'
import {appendToDescription, escapeBlockComment, isSchemaLike, justName, toSafeString, traverse} from './utils.js'
import {Options} from './index.js'
import {DereferencedPaths} from './resolver.js'
import {isDeepStrictEqual} from 'util'

type Rule = (
Expand Down
10 changes: 5 additions & 5 deletions src/optimizer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {uniqBy} from 'lodash'
import {Options} from '.'
import {generateType} from './generator'
import {AST, T_ANY, T_UNKNOWN} from './types/AST'
import {log} from './utils'
import {uniqBy} from 'lodash-es'
import {Options} from './index.js'
import {generateType} from './generator.js'
import {AST, T_ANY, T_UNKNOWN} from './types/AST.js'
import {log} from './utils.js'

export function optimize(ast: AST, options: Options, processed = new Set<AST>()): AST {
if (processed.has(ast)) {
Expand Down
2 changes: 1 addition & 1 deletion src/optionValidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Options} from '.'
import {Options} from './index.js'

export function validateOptions({maxItems}: Partial<Options>): void {
if (maxItems !== undefined && maxItems < -1) {
Expand Down
12 changes: 6 additions & 6 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {JSONSchema4Type, JSONSchema4TypeName} from 'json-schema'
import {findKey, includes, isPlainObject, map, memoize, omit} from 'lodash'
import {findKey, includes, isPlainObject, map, memoize, omit} from 'lodash-es'
import {format} from 'util'
import {Options} from './'
import {typesOfSchema} from './typesOfSchema'
import {Options} from './index.js'
import {typesOfSchema} from './typesOfSchema.js'
import {
AST,
T_ANY,
Expand All @@ -14,7 +14,7 @@ import {
T_UNKNOWN,
T_UNKNOWN_ADDITIONAL_PROPERTIES,
TIntersection,
} from './types/AST'
} from './types/AST.js'
import {
getRootSchema,
isBoolean,
Expand All @@ -23,8 +23,8 @@ import {
JSONSchemaWithDefinitions,
SchemaSchema,
SchemaType,
} from './types/JSONSchema'
import {generateName, log, maybeStripDefault, maybeStripNameHints} from './utils'
} from './types/JSONSchema.js'
import {generateName, log, maybeStripDefault, maybeStripNameHints} from './utils.js'

export type Processed = Map<LinkedJSONSchema, Map<SchemaType, AST>>

Expand Down
4 changes: 2 additions & 2 deletions src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {$RefParser, ParserOptions as $RefOptions} from '@apidevtools/json-schema-ref-parser'
import {JSONSchema} from './types/JSONSchema'
import {log} from './utils'
import {JSONSchema} from './types/JSONSchema.js'
import {log} from './utils.js'

export type DereferencedPaths = WeakMap<JSONSchema, string>

Expand Down
2 changes: 1 addition & 1 deletion src/types/JSONSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {JSONSchema4, JSONSchema4Type, JSONSchema4TypeName} from 'json-schema'
import {isPlainObject, memoize} from 'lodash'
import {isPlainObject, memoize} from 'lodash-es'

export type SchemaType =
| 'ALL_OF'
Expand Down
4 changes: 2 additions & 2 deletions src/typesOfSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {isPlainObject} from 'lodash'
import {isCompound, JSONSchema, SchemaType} from './types/JSONSchema'
import {isPlainObject} from 'lodash-es'
import {isCompound, JSONSchema, SchemaType} from './types/JSONSchema.js'

/**
* Duck types a JSONSchema schema or property to determine which kind of AST node to parse it into.
Expand Down
36 changes: 20 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {deburr, isPlainObject, trim, upperFirst} from 'lodash'
import {deburr, isPlainObject, trim, upperFirst} from 'lodash-es'
import {basename, dirname, extname, normalize, sep, posix} from 'path'
import {JSONSchema, LinkedJSONSchema, Parent} from './types/JSONSchema'
import {JSONSchema, LinkedJSONSchema, Parent} from './types/JSONSchema.js'
import {JSONSchema4} from 'json-schema'
import yaml from 'js-yaml'

Expand Down Expand Up @@ -208,7 +208,7 @@ export function error(...messages: any[]): void {
if (!process.env.VERBOSE) {
return console.error(messages)
}
console.error(getStyledTextForLogging('red')?.('error'), ...messages)
getStyledTextForLogging('red').then(text => console.error(text?.('error'), ...messages))
}

type LogStyle = 'blue' | 'cyan' | 'green' | 'magenta' | 'red' | 'white' | 'yellow'
Expand All @@ -221,31 +221,35 @@ export function log(style: LogStyle, title: string, ...messages: unknown[]): voi
if (messages.length > 1 && typeof messages[messages.length - 1] !== 'string') {
lastMessage = messages.splice(messages.length - 1, 1)
}
console.info(require('cli-color').whiteBright.bgCyan('debug'), getStyledTextForLogging(style)?.(title), ...messages)
if (lastMessage) {
console.dir(lastMessage, {depth: 6, maxArrayLength: 6})
}
import('cli-color').then(color => {
getStyledTextForLogging(style).then(text => {
console.info(color.whiteBright.bgCyan('debug'), text?.(title), ...messages)
if (lastMessage) {
console.dir(lastMessage, {depth: 6, maxArrayLength: 6})
}
})
})
}

function getStyledTextForLogging(style: LogStyle): ((text: string) => string) | undefined {
function getStyledTextForLogging(style: LogStyle): Promise<((text: string) => string) | undefined> {
if (!process.env.VERBOSE) {
return
return Promise.resolve(undefined)
}
switch (style) {
case 'blue':
return require('cli-color').whiteBright.bgBlue
return import('cli-color').then(color => color.whiteBright.bgBlue)
case 'cyan':
return require('cli-color').whiteBright.bgCyan
return import('cli-color').then(color => color.whiteBright.bgCyan)
case 'green':
return require('cli-color').whiteBright.bgGreen
return import('cli-color').then(color => color.whiteBright.bgGreen)
case 'magenta':
return require('cli-color').whiteBright.bgMagenta
return import('cli-color').then(color => color.whiteBright.bgMagenta)
case 'red':
return require('cli-color').whiteBright.bgRedBright
return import('cli-color').then(color => color.whiteBright.bgRedBright)
case 'white':
return require('cli-color').black.bgWhite
return import('cli-color').then(color => color.black.bgWhite)
case 'yellow':
return require('cli-color').whiteBright.bgYellow
return import('cli-color').then(color => color.whiteBright.bgYellow)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {JSONSchema, LinkedJSONSchema} from './types/JSONSchema'
import {traverse} from './utils'
import {JSONSchema, LinkedJSONSchema} from './types/JSONSchema.js'
import {traverse} from './utils.js'

type Rule = (schema: JSONSchema) => boolean | void
const rules = new Map<string, Rule>()
Expand Down
Loading