From c077e493d8256baa33c06789e8186db8a9c2f1d5 Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Wed, 15 May 2024 23:59:14 -0300 Subject: [PATCH 1/6] initial config --- .gitignore | 2 +- bin/ts-json-schema-generator.cjs | 2 ++ package.json | 10 +++++++++- ts-json-schema-generator.ts | 2 +- tsconfig.cjs.json | 13 +++++++++++++ tsconfig.json | 2 +- 6 files changed, 27 insertions(+), 4 deletions(-) create mode 100755 bin/ts-json-schema-generator.cjs create mode 100644 tsconfig.cjs.json diff --git a/.gitignore b/.gitignore index dfb06bc6d..02bd43321 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,10 @@ /*.ts coverage/ dist/ +cjs/ node_modules/ !auto.config.ts /.idea/ # local config for auto .env - diff --git a/bin/ts-json-schema-generator.cjs b/bin/ts-json-schema-generator.cjs new file mode 100755 index 000000000..2b67fd9f8 --- /dev/null +++ b/bin/ts-json-schema-generator.cjs @@ -0,0 +1,2 @@ +#!/usr/bin/env node +require("../cjs/ts-json-schema-generator.js"); diff --git a/package.json b/package.json index 7546422d3..231d6351b 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "files": [ "dist", + "cjs", "src", "factory", "index.*", @@ -44,6 +45,10 @@ "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", @@ -51,6 +56,7 @@ "json5": "^2.2.3", "normalize-path": "^3.0.0", "safe-stable-stringify": "^2.4.3", + "tslib": "^2.6.2", "typescript": "^5.4.5" }, "devDependencies": { @@ -83,7 +89,9 @@ }, "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", "watch": "tsc -w", "lint": "eslint", "format": "eslint --fix", diff --git a/ts-json-schema-generator.ts b/ts-json-schema-generator.ts index 8b28fc575..5ffebf4c2 100644 --- a/ts-json-schema-generator.ts +++ b/ts-json-schema-generator.ts @@ -4,7 +4,7 @@ import { createGenerator } from "./factory/generator.js"; import { 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 pkg from "./package.json"; import { dirname } from "path"; import { mkdirSync, writeFileSync } from "fs"; diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 000000000..15a5e24d4 --- /dev/null +++ b/tsconfig.cjs.json @@ -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"] +} diff --git a/tsconfig.json b/tsconfig.json index 6d82b123a..099aa980b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"] } From f877ec43c8114f7aa66e6c68655dfa5925922b78 Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Thu, 16 May 2024 00:15:56 -0300 Subject: [PATCH 2/6] fix: lint --- bin/ts-json-schema-generator.cjs | 0 eslint.config.js | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 bin/ts-json-schema-generator.cjs diff --git a/bin/ts-json-schema-generator.cjs b/bin/ts-json-schema-generator.cjs old mode 100755 new mode 100644 diff --git a/eslint.config.js b/eslint.config.js index 1963ae3ee..ef657d2b2 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -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", "bin/ts-json-schema-generator.cjs"], }, eslint.configs.recommended, { From 12928233bc619b1b755ca0d5911729b5227f6d4a Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Thu, 16 May 2024 00:16:20 -0300 Subject: [PATCH 3/6] fix --- bin/ts-json-schema-generator.cjs | 2 -- eslint.config.js | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 bin/ts-json-schema-generator.cjs diff --git a/bin/ts-json-schema-generator.cjs b/bin/ts-json-schema-generator.cjs deleted file mode 100644 index 2b67fd9f8..000000000 --- a/bin/ts-json-schema-generator.cjs +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env node -require("../cjs/ts-json-schema-generator.js"); diff --git a/eslint.config.js b/eslint.config.js index ef657d2b2..0534e0a8a 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -6,7 +6,7 @@ import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended" /** @type {import('@types/eslint').Linter.FlatConfig[]} */ export default tseslint.config( { - ignores: ["dist", "cjs", "bin/ts-json-schema-generator.cjs"], + ignores: ["dist", "cjs"], }, eslint.configs.recommended, { From a7c86f2c141f348a01481def3bdf8010f681e103 Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Thu, 16 May 2024 00:55:40 -0300 Subject: [PATCH 4/6] fixed esm/cjs fuckery --- build/pkg-version.js | 18 ++++++++++++++++++ eslint.config.js | 2 +- package.json | 2 ++ ts-json-schema-generator.ts | 12 +++++++----- 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 build/pkg-version.js diff --git a/build/pkg-version.js b/build/pkg-version.js new file mode 100644 index 000000000..82d7d7e0e --- /dev/null +++ b/build/pkg-version.js @@ -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")); diff --git a/eslint.config.js b/eslint.config.js index 0534e0a8a..74c0e841a 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -6,7 +6,7 @@ import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended" /** @type {import('@types/eslint').Linter.FlatConfig[]} */ export default tseslint.config( { - ignores: ["dist", "cjs"], + ignores: ["dist", "cjs", "build"], }, eslint.configs.recommended, { diff --git a/package.json b/package.json index 231d6351b..59f2689b2 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "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": { @@ -92,6 +93,7 @@ "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", diff --git a/ts-json-schema-generator.ts b/ts-json-schema-generator.ts index 5ffebf4c2..d32a7df64 100644 --- a/ts-json-schema-generator.ts +++ b/ts-json-schema-generator.ts @@ -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"; -import { dirname } from "path"; -import { mkdirSync, writeFileSync } from "fs"; + +// This constant gets replaced by the build script +const pkgVersion = "0.0.0"; /* __VERSION__ */ const args = new Command() .option("-p, --path ", "Source file path") @@ -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(); From d3069c884fc2e43058dbf6bb40c7c390802422eb Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Thu, 16 May 2024 15:41:40 -0300 Subject: [PATCH 5/6] fix: removed unused comment --- ts-json-schema-generator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts-json-schema-generator.ts b/ts-json-schema-generator.ts index d32a7df64..9cd456e5a 100644 --- a/ts-json-schema-generator.ts +++ b/ts-json-schema-generator.ts @@ -8,7 +8,7 @@ import { BaseError } from "./src/Error/BaseError.js"; import { formatError } from "./src/Utils/formatError.js"; // This constant gets replaced by the build script -const pkgVersion = "0.0.0"; /* __VERSION__ */ +const pkgVersion = "0.0.0"; const args = new Command() .option("-p, --path ", "Source file path") From 89f0b4aa66d825214c3548359f6adb2d45e50202 Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Sun, 19 May 2024 17:06:34 -0300 Subject: [PATCH 6/6] feat: importHelpers for esm --- tsconfig.cjs.json | 3 +-- tsconfig.json | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index 15a5e24d4..de02fb0c0 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -4,8 +4,7 @@ "target": "ES2022", "module": "CommonJS", "moduleResolution": "Node", - "outDir": "cjs", - "importHelpers": true + "outDir": "cjs" }, "files": ["ts-json-schema-generator.ts", "index.ts"], "include": ["src/**/*.ts", "factory/**/*.ts"], diff --git a/tsconfig.json b/tsconfig.json index 099aa980b..d0f20b4a0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,7 +22,8 @@ "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"],