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

Added back CommonsJS output under /cjs #1956

Closed
wants to merge 7 commits into from
Closed
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*.ts
coverage/
dist/
cjs/
node_modules/
!auto.config.ts
/.idea/
Expand All @@ -10,4 +11,4 @@ node_modules/

# Other package managers
pnpm-lock.yaml
package-lock.json
package-lock.json
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": {
domoritz marked this conversation as resolved.
Show resolved Hide resolved
"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",
arthurfiorette marked this conversation as resolved.
Show resolved Hide resolved
"typescript": "^5.4.5"
},
"devDependencies": {
Expand Down Expand Up @@ -84,7 +91,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";

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
12 changes: 12 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ES2022",
"module": "CommonJS",
"moduleResolution": "Node",
"outDir": "cjs"
},
"files": ["ts-json-schema-generator.ts", "index.ts"],
"include": ["src/**/*.ts", "factory/**/*.ts"],
"exclude": ["node_modules", "dist", "cjs"]
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
"pretty": true,
"typeRoots": ["node_modules/@types"],
"outDir": "dist",
"incremental": true
"incremental": true,
"importHelpers": true
},
"files": ["ts-json-schema-generator.ts", "index.ts"],
"include": ["src/**/*.ts", "factory/**/*.ts"],
"exclude": ["node_modules", "dist"]
"exclude": ["node_modules", "dist", "cjs"]
}
Loading