Skip to content

Commit

Permalink
Migrate from CommonJS -> ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
bcherny committed Jun 20, 2024
1 parent 89a7b32 commit 5685c32
Show file tree
Hide file tree
Showing 33 changed files with 898,473 additions and 1,148 deletions.
783 changes: 0 additions & 783 deletions .yarn/releases/yarn-3.2.2.cjs

This file was deleted.

58 changes: 22 additions & 36 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"bin": {
"json2ts": "dist/src/cli.js"
},
"type": "module",
"typings": "dist/src/index.d.ts",
"engines": {
"node": ">=16.0.0"
Expand Down Expand Up @@ -50,15 +51,14 @@
"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",
"mz": "^2.7.0",
"node-fetch": "^3.3.2",
"prettier": "^3.2.5"
},
Expand Down
14 changes: 7 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env node

import minimist from 'minimist'
import {readFile, writeFile, existsSync, lstatSync, readdirSync} from 'mz/fs'
import {existsSync, readdirSync, readFileSync, lstatSync, writeFileSync} from 'fs'
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 @@ -114,14 +114,14 @@ async function processDir(argIn: string, argOut: string | undefined, argv: Parti
)
}

async function outputResult(result: string, outputPath: string | undefined): Promise<void> {
function outputResult(result: string, outputPath: string | undefined): void {
if (!outputPath) {
process.stdout.write(result)
} else {
if (!isDir(dirname(outputPath))) {
mkdirp.sync(dirname(outputPath))
}
return await writeFile(outputPath, result)
return writeFileSync(outputPath, result)
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ async function readInput(argIn?: string): Promise<{filename: string | null; cont
}
return {
filename: argIn,
contents: await readFile(resolve(process.cwd(), argIn), 'utf-8'),
contents: readFileSync(resolve(process.cwd(), argIn), 'utf-8'),
}
}

Expand All @@ -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
Loading

0 comments on commit 5685c32

Please sign in to comment.