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 fcb6f73 commit 2e9431d
Show file tree
Hide file tree
Showing 36 changed files with 226 additions and 212 deletions.
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
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.

26 changes: 15 additions & 11 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,15 +55,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 Expand Up @@ -87,7 +91,7 @@
},
"ava": {
"files": [
"./dist/test/test.js"
"./dist-esm/test/test.js"
],
"snapshotDir": "./test/__snapshots__"
},
Expand Down
16 changes: 8 additions & 8 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,18 +150,18 @@ 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'),
}
}

async function readStream(stream: NodeJS.ReadStream): Promise<string> {
const chunks = []
const chunks: Uint8Array[] = []
for await (const chunk of stream) chunks.push(chunk)
return Buffer.concat(chunks).toString('utf8')
}

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
Loading

0 comments on commit 2e9431d

Please sign in to comment.