Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*.ts
coverage/
dist/
cjs/
node_modules/
!auto.config.ts
/.idea/

# local config for auto
.env

18 changes: 18 additions & 0 deletions build/pkg-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Having the same source code to work with Node16's ESM and Node's CJS is a pain.
// this script simply copies the packageJson version inside the main ts-json-schema-generator.js file

import fs from "node:fs";
import path from "node:path";
import pkg from "../package.json" with { type: "json" };

function replaceVersion(source, version) {
return source.replace(/const pkgVersion = "0.0.0";/, `const pkgVersion = "${version}";`);
}

function replaceFile(path) {
const source = fs.readFileSync(path, "utf-8");
fs.writeFileSync(path, replaceVersion(source, pkg.version));
}

replaceFile(path.resolve("cjs/ts-json-schema-generator.js"));
replaceFile(path.resolve("dist/ts-json-schema-generator.js"));
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended"
/** @type {import('@types/eslint').Linter.FlatConfig[]} */
export default tseslint.config(
{
ignores: ["dist"],
ignores: ["dist", "cjs", "build"],
},
eslint.configs.recommended,
{
Expand Down
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"version": "2.0.0",
"description": "Generate JSON schema from your Typescript sources",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"bin": {
"ts-json-schema-generator": "./bin/ts-json-schema-generator.js"
},
"files": [
"dist",
"cjs",
"src",
"factory",
"index.*",
Expand Down Expand Up @@ -44,13 +46,18 @@
"engines": {
"node": ">=18.0.0"
},
"exports": {
"import": "./dist/index.js",
"require": "./cjs/index.js"
},
"dependencies": {
"@types/json-schema": "^7.0.15",
"commander": "^12.0.0",
"glob": "^10.3.12",
"json5": "^2.2.3",
"normalize-path": "^3.0.0",
"safe-stable-stringify": "^2.4.3",
"tslib": "^2.6.2",
"typescript": "^5.4.5"
},
"devDependencies": {
Expand Down Expand Up @@ -83,7 +90,10 @@
},
"scripts": {
"prepublishOnly": "yarn build",
"build": "tsc",
"build": "npm run build:cjs && npm run build:esm",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:esm": "tsc -p tsconfig.json",
"postbuild": "node build/pkg-version.js",
"watch": "tsc -w",
"lint": "eslint",
"format": "eslint --fix",
Expand Down
12 changes: 7 additions & 5 deletions ts-json-schema-generator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { mkdirSync, writeFileSync } from "node:fs";
import { dirname } from "node:path";
import { Command, Option } from "commander";
import stableStringify from "safe-stable-stringify";
import { createGenerator } from "./factory/generator.js";
import { Config } from "./src/Config.js";
import type { Config } from "./src/Config.js";
import { BaseError } from "./src/Error/BaseError.js";
import { formatError } from "./src/Utils/formatError.js";
import pkg from "./package.json" with { type: "json" };
import { dirname } from "path";
import { mkdirSync, writeFileSync } from "fs";

// This constant gets replaced by the build script
const pkgVersion = "0.0.0"; /* __VERSION__ */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this as it makes it harder to watch build. The cjs version used to work so can you try to make the import version work here as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried a lot but ran out of choices.

  • import pkg from 'package.json' (or * as pkg) works in cjs but not in ESM as it requires a with {type: 'json'} statement

  • require('package.json') works but not in ESM mode.

  • using const _require = typeof require === 'undefined' ? createRequire(import.meta.url) : require should work but breaks in CJS even if createRequire(import.meta.url) does not gets executed.

  • using fs.readFileSync requires __dirname (which can be created using import.meta.url in esm but breaks just like the above try) and a second build step to manually copy package.json to dist/cjs folders.

  • Trying to bypass import.meta.url using `eval('import.meta.url') does not work.

I'm not sure if there's any other way to fix it without replacing the dist code. Also, as it gets built on CI before publishing, all official releases should work just as fine.


const args = new Command()
.option("-p, --path <path>", "Source file path")
Expand Down Expand Up @@ -49,7 +51,7 @@ const args = new Command()
[],
)
.option("--additional-properties", "Allow additional properties for objects with no index signature", false)
.version(pkg.version)
.version(pkgVersion)
.parse(process.argv)
.opts();

Expand Down
13 changes: 13 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ES2022",
"module": "CommonJS",
"moduleResolution": "Node",
"outDir": "cjs",
"importHelpers": true
},
"files": ["ts-json-schema-generator.ts", "index.ts"],
"include": ["src/**/*.ts", "factory/**/*.ts"],
"exclude": ["node_modules", "dist", "cjs"]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
},
"files": ["ts-json-schema-generator.ts", "index.ts"],
"include": ["src/**/*.ts", "factory/**/*.ts"],
"exclude": ["node_modules", "dist"]
"exclude": ["node_modules", "dist", "cjs"]
}