Skip to content

Commit e1873a6

Browse files
authored
BREAKING CHANGE: #51 Bump versions, change build to only be cjs, add cli (#68)
* BREAKING CHANGE: #51 Bump versions, change build to only be cjs, add cli * remove present * add readme
1 parent abd2e4a commit e1873a6

File tree

9 files changed

+1328
-1172
lines changed

9 files changed

+1328
-1172
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: yarn install, build, and test
2020
run: |
2121
yarn --frozen-lockfile
22-
yarn build --if-present
22+
yarn build
2323
yarn lint
2424
yarn test
2525
env:

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ If you need to do the conversion in reverse, checkout [json-schema-to-openapi-sc
3333
npm install --save @openapi-contrib/openapi-schema-to-json-schema
3434
```
3535

36+
### CLI
37+
38+
```bash
39+
npx openapi-schema-to-json-schema --input openapi.json --output json-schema.json
40+
```
41+
3642
## Converting OpenAPI schema
3743

3844
Here's a small example to get the idea:

package.json

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,55 @@
22
"name": "@openapi-contrib/openapi-schema-to-json-schema",
33
"version": "0.0.0-development",
44
"description": "Converts OpenAPI Schema Object to JSON Schema",
5-
"types": "dist/mjs/index.d.ts",
5+
"types": "dist/index.d.ts",
66
"files": [
7-
"/dist"
7+
"dist",
8+
"src",
9+
"package.json",
10+
"tsconfig.json"
811
],
9-
"main": "dist/cjs/index.js",
10-
"module": "dist/mjs/index.js",
11-
"exports": {
12-
".": {
13-
"import": "./dist/mjs/index.js",
14-
"require": "./dist/cjs/index.js"
15-
}
16-
},
12+
"main": "dist/index.js",
1713
"scripts": {
18-
"build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && node scripts/fixup.cjs",
14+
"build": "rimraf dist && tsc -p tsconfig.json",
1915
"test": "vitest",
2016
"coverage": "vitest --coverage",
2117
"lint": "eslint . && prettier -c src",
2218
"typecheck": "tsc --noEmit",
2319
"lint:fix": "eslint . --fix && prettier -c src -w"
2420
},
25-
"repository": "github:openapi-contrib/openapi-schema-to-json-schema",
21+
"repository": "https://github.com/openapi-contrib/openapi-schema-to-json-schema",
2622
"author": "OpenAPI Contrib",
2723
"license": "MIT",
24+
"bin": "dist/bin.js",
2825
"dependencies": {
29-
"@types/node": "^20.2.5",
3026
"@types/json-schema": "^7.0.12",
27+
"@types/node": "^20.4.1",
3128
"fast-deep-equal": "^3.1.3",
32-
"openapi-typescript": "^5.4.1"
29+
"openapi-typescript": "^5.4.1",
30+
"yargs": "^17.7.2"
3331
},
3432
"devDependencies": {
35-
"@typescript-eslint/eslint-plugin": "^5.59.8",
36-
"@typescript-eslint/parser": "^5.59.8",
37-
"c8": "^7.14.0",
38-
"eslint": "^8.42.0",
33+
"@types/yargs": "^17.0.24",
34+
"@typescript-eslint/eslint-plugin": "^6.0.0",
35+
"@typescript-eslint/parser": "^6.0.0",
36+
"c8": "^8.0.0",
37+
"eslint": "^8.44.0",
3938
"eslint-config-prettier": "^8.8.0",
40-
"eslint-plugin-prettier": "^4.2.1",
41-
"eslint-plugin-unused-imports": "^2.0.0",
42-
"prettier": "^2.8.8",
43-
"semantic-release": "^21.0.3",
44-
"typescript": "^5.1.3",
45-
"vitest": "^0.31.4"
39+
"eslint-plugin-prettier": "^5.0.0",
40+
"eslint-plugin-unused-imports": "^3.0.0",
41+
"prettier": "^3.0.0",
42+
"rimraf": "^5.0.1",
43+
"semantic-release": "^21.0.7",
44+
"typescript": "^5.1.6",
45+
"vitest": "^0.33.0"
4646
},
4747
"prettier": {
4848
"printWidth": 120,
4949
"useTabs": false,
5050
"arrowParens": "always",
5151
"trailingComma": "all"
52+
},
53+
"engines": {
54+
"node": ">=14.0.0"
5255
}
5356
}

scripts/fixup.cjs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/bin.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
3+
import yargs from "yargs";
4+
import { hideBin } from "yargs/helpers";
5+
import { openapiSchemaToJsonSchema } from "./index.js";
6+
import fs from "fs/promises";
7+
import * as process from "process";
8+
import type { AcceptibleInputSchema } from "./openapi-schema-types";
9+
const args = yargs(hideBin(process.argv))
10+
.options({
11+
input: { type: "string", alias: "f", demandOption: true },
12+
output: { type: "string", alias: "o" },
13+
})
14+
.parseSync();
15+
16+
const { input, output } = args;
17+
18+
const getFileContents = async () => {
19+
try {
20+
const fileContents = await fs.readFile(input, "utf-8");
21+
return JSON.parse(fileContents) as AcceptibleInputSchema;
22+
} catch (e: any) {
23+
console.error(`Error: ${e.message}`);
24+
process.exit(1);
25+
}
26+
};
27+
28+
(async () => {
29+
try {
30+
const fileContents = await getFileContents();
31+
const convertedSchema = await openapiSchemaToJsonSchema(fileContents);
32+
const outputFile = output || input.replace(/\.json$/, "-converted.json");
33+
await fs.writeFile(outputFile, JSON.stringify(convertedSchema, null, 2));
34+
} catch (e: any) {
35+
console.error(`Error: ${e.message}`);
36+
}
37+
})();

src/lib/utils/cloneDeep.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// From https://dev.to/salyadav/deep-clone-of-js-objects-with-circular-dependency-4if7
2-
const isArray = (val) => {
2+
const isArray = (val: unknown) => {
33
return Array.isArray(val);
44
};
55

6-
const isObject = (val) => {
6+
const isObject = (val: unknown) => {
77
return {}.toString.call(val) === "[object Object]" && !isArray(val);
88
};
99

10-
export const cloneDeep = (val, history = new Set()) => {
10+
export const cloneDeep = (val: unknown, history = new Set()) => {
1111
const stack = history || new Set();
1212

1313
if (stack.has(val)) {

tsconfig-cjs.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

tsconfig.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
{
22
"compilerOptions": {
3-
"module": "esnext",
4-
"outDir": "dist/mjs",
3+
"module": "CommonJS",
4+
"outDir": "dist",
55
"target": "es2020",
66
"allowJs": true,
77
"allowSyntheticDefaultImports": true,
88
"baseUrl": "src",
99
"declaration": true,
1010
"esModuleInterop": true,
1111
"inlineSourceMap": false,
12-
"lib": ["esnext"],
12+
"lib": ["ES2020"],
1313
"listEmittedFiles": false,
1414
"listFiles": false,
1515
"moduleResolution": "node",
1616
"noFallthroughCasesInSwitch": true,
1717
"pretty": true,
1818
"resolveJsonModule": true,
1919
"rootDir": "src",
20-
"skipLibCheck": true,
21-
"noImplicitAny": false,
2220
"strict": true,
2321
"noUnusedParameters": true,
22+
"noImplicitAny": false,
2423
"sourceMap": true
2524
},
2625
"compileOnSave": false,

0 commit comments

Comments
 (0)