From 5e474bfddc644acad52f5d0e2d488934d492d103 Mon Sep 17 00:00:00 2001 From: Ralf Handl Date: Wed, 24 Apr 2024 10:35:47 +0200 Subject: [PATCH 1/4] Remove minimist --- tools/test/transform.test.js | 2 +- tools/transform.js | 142 +++++++++++++++++------------------ 2 files changed, 69 insertions(+), 75 deletions(-) mode change 100644 => 100755 tools/transform.js diff --git a/tools/test/transform.test.js b/tools/test/transform.test.js index 3e303215..51a73e05 100644 --- a/tools/test/transform.test.js +++ b/tools/test/transform.test.js @@ -18,7 +18,7 @@ describe("CLI", function () { it("invalid option", async () => { const result = await transform(["-x"]); assert.equal(result.code, 1); - assert.equal(result.stderr, "Unknown option: -x\n"); + assert.match(result.stderr, /Unknown option '-x'/); }); it("non-existing file", async () => { diff --git a/tools/transform.js b/tools/transform.js old mode 100644 new mode 100755 index 5a45ad9c..5353e70b --- a/tools/transform.js +++ b/tools/transform.js @@ -6,11 +6,10 @@ - info-title/description/version? - externalDocs-url/description? */ -//console.dir(argv); "use strict"; -const minimist = require("minimist"); +const { parseArgs } = require("node:util"); const path = require("path"); const fs = require("fs"); const { spawnSync } = require("child_process"); @@ -20,55 +19,42 @@ const toolsPath = path.dirname(require.main.filename) + path.sep; const classPath = `${toolsPath}xalan/xalan.jar${path.delimiter}${toolsPath}xalan/serializer.jar`; let unknown = false; +let argv; + +try { + argv = parseArgs({ + options: { + basePath: { type: "string", default: "/service-root" }, + description: { type: "string" }, + diagram: { type: "boolean", short: "d" }, + help: { type: "boolean", short: "h" }, + host: { type: "string", default: "localhost" }, + keep: { type: "string", short: "k", multiple: true }, + levels: { type: "string" }, + "odata-version": { type: "string", short: "v", default: "4.0" }, + "openapi-version": { type: "string", short: "o", default: "3.0.0" }, + pretty: { type: "boolean", short: "p" }, + references: { type: "boolean", short: "r" }, + scheme: { type: "string", default: "https" }, + skipBatchPath: { type: "boolean" }, + target: { type: "string", short: "t" }, + title: { type: "string" }, + "used-schemas-only": { type: "boolean", short: "u" }, + verbose: { type: "boolean" }, + }, + allowPositionals: true, + }); +} catch (e) { + console.error(e.message); + unknown = true; +} -let argv = minimist(process.argv.slice(2), { - string: ["o", "openapi-version", "t", "target", "scheme", "host", "basePath"], - boolean: [ - "d", - "diagram", - "h", - "help", - "p", - "pretty", - "r", - "references", - "u", - "used-schemas-only", - "verbose", - ], - alias: { - d: "diagram", - h: "help", - o: "openapi-version", - p: "pretty", - r: "references", - t: "target", - u: "used-schemas-only", - v: "odata-version", - }, - default: { - basePath: "/service-root", - diagram: false, - host: "localhost", - "odata-version": "4.0", - "openapi-version": "3.0.0", - pretty: false, - references: false, - scheme: "https", - verbose: false, - }, - unknown: (arg) => { - if (arg.substring(0, 1) == "-") { - console.error(`Unknown option: ${arg}`); - unknown = true; - return false; - } - }, -}); -if (argv.o == "2") argv.o = "2.0"; -if (argv.o == "3") argv.o = "3.0.0"; +if (argv.values["openapi-version"] == "2") + argv.values["openapi-version"] = "2.0"; +if (argv.values["openapi-version"] == "3") + argv.values["openapi-version"] = "3.0.0"; -if (unknown || argv._.length == 0 || argv.h) { +if (unknown || argv.positionals.length == 0 || argv.values.help) { console.log(`Usage: odata-openapi Options: --basePath base path (default: /service-root) @@ -84,8 +70,8 @@ Options: --verbose output additional progress information`); process.exit(1); } else { - for (let i = 0; i < argv._.length; i++) { - transform(argv._[i]); + for (let i = 0; i < argv.positionals.length; i++) { + transform(argv.positionals[i]); } } @@ -95,7 +81,7 @@ function transform(source) { process.exit(1); } - if (argv.verbose) + if (argv.values.verbose) console.log(`Checking OData version used in source file: ${source}`); const result = xalan("OData-Version.xsl", "-IN", source); @@ -107,7 +93,7 @@ function transform(source) { if (result.stderr.length) { console.error(`Source file not XML: ${source}`); - if (argv.verbose) console.log(result.stderr.toString()); + if (argv.values.verbose) console.log(result.stderr.toString()); process.exit(1); } @@ -118,10 +104,12 @@ function transform(source) { } if (version == "2.0" || version == "3.0") { - if (argv.verbose) console.log(`Source file is OData version: ${version}`); + if (argv.values.verbose) + console.log(`Source file is OData version: ${version}`); transformV2V3(source, version); } else { - if (argv.verbose) console.log(`Source file is OData version: ${version}`); + if (argv.values.verbose) + console.log(`Source file is OData version: ${version}`); transformV4(source, version, false); } } @@ -140,7 +128,7 @@ function xalan(xslt, ...args) { function transformV2V3(source, version) { const target = source.substring(0, source.lastIndexOf(".") + 1) + "tmp"; - if (argv.verbose) + if (argv.values.verbose) console.log(`Transforming ${source} to OData V4, target file: ${target}`); const result = xalan("V2-to-V4-CSDL.xsl", "-IN", source, "-OUT", target); @@ -153,52 +141,58 @@ function transformV2V3(source, version) { function transformV4(source, version, deleteSource) { const target = - argv.t || source.substring(0, source.lastIndexOf(".") + 1) + "openapi.json"; + argv.values.target || + source.substring(0, source.lastIndexOf(".") + 1) + "openapi.json"; - if (argv.verbose) + if (argv.values.verbose) console.log( - `Transforming ${source} to OpenAPI ${argv.o}, target file: ${target}`, + `Transforming ${source} to OpenAPI ${argv.values["openapi-version"]}, target file: ${target}`, ); const params = ["-IN", source]; - if (!argv.u && !argv.pretty) params.push("-OUT", target); - if (argv.basePath) params.push("-PARAM", "basePath", argv.basePath); - if (argv.diagram) params.push("-PARAM", "diagram", argv.diagram); - if (argv.host) params.push("-PARAM", "host", argv.host); + if (!argv.values["used-schemas-only"] && !argv.values.pretty) + params.push("-OUT", target); + if (argv.values.basePath) + params.push("-PARAM", "basePath", argv.values.basePath); + if (argv.values.diagram) + params.push("-PARAM", "diagram", argv.values.diagram); + if (argv.values.host) params.push("-PARAM", "host", argv.values.host); params.push("-PARAM", "odata-version", version); - params.push("-PARAM", "openapi-version", argv.o); - if (argv.references) params.push("-PARAM", "references", argv.references); - if (argv.scheme) params.push("-PARAM", "scheme", argv.scheme); + params.push("-PARAM", "openapi-version", argv.values["openapi-version"]); + if (argv.values.references) + params.push("-PARAM", "references", argv.values.references); + if (argv.values.scheme) params.push("-PARAM", "scheme", argv.values.scheme); const result = xalan("V4-CSDL-to-OpenAPI.xsl", ...params); if (result.stderr.length) console.error(result.stderr.toString()); if (result.status !== 0) process.exit(1); - if (argv.pretty || argv.u) { + if (argv.values.pretty || argv.values["used-schemas-only"]) { try { let openapi = JSON.parse(result.stdout); - if (argv.u) { - if (argv.verbose) console.log("Deleting unused schemas"); + if (argv.values["used-schemas-only"]) { + if (argv.values.verbose) console.log("Deleting unused schemas"); deleteUnusedSchemas(openapi); } - if (argv.verbose) console.log(`Writing target file: ${target}`); + if (argv.values.verbose) console.log(`Writing target file: ${target}`); fs.writeFileSync( target, - JSON.stringify(openapi, null, argv.pretty ? 4 : 0), + JSON.stringify(openapi, null, argv.values.pretty ? 4 : 0), ); } catch (e) { - if (argv.verbose) console.log("Ooops, something went wrong: "); + if (argv.values.verbose) console.log("Ooops, something went wrong: "); console.log(e); fs.writeFileSync(target, result.stdout); } } if (deleteSource) { - if (argv.verbose) console.log(`Removing intermediate file: ${source}`); + if (argv.values.verbose) + console.log(`Removing intermediate file: ${source}`); fs.unlinkSync(source); } - if (argv.verbose) console.log("Done."); + if (argv.values.verbose) console.log("Done."); } From 10fa3984ed79e8db02dc979662f168c64e7ce60b Mon Sep 17 00:00:00 2001 From: Ralf Handl Date: Wed, 24 Apr 2024 10:58:25 +0200 Subject: [PATCH 2/4] Cleanup --- tools/package-lock.json | 44 +++++++++++++++++------------------------ tools/package.json | 3 --- tools/transform.js | 6 +----- 3 files changed, 19 insertions(+), 34 deletions(-) diff --git a/tools/package-lock.json b/tools/package-lock.json index e5c79df9..3ca1671f 100644 --- a/tools/package-lock.json +++ b/tools/package-lock.json @@ -8,9 +8,6 @@ "name": "odata-openapi-xslt", "version": "0.8.1", "license": "SEE LICENSE IN ../LICENSE.md", - "dependencies": { - "minimist": "^1.2.8" - }, "bin": { "odata-openapi": "transform.js" }, @@ -48,9 +45,9 @@ } }, "node_modules/@jridgewell/resolve-uri": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.1.tgz", - "integrity": "sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", "dev": true, "engines": { "node": ">=6.0.0" @@ -63,9 +60,9 @@ "dev": true }, "node_modules/@jridgewell/trace-mapping": { - "version": "0.3.22", - "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.22.tgz", - "integrity": "sha512-Wf963MzWtA2sjrNt+g18IAln9lKnlRp+K2eH4jjIoF1wYeq3aMREpG09xhlhdzS0EjwU7qmUJYangWa+151vZw==", + "version": "0.3.25", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.25.tgz", + "integrity": "sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==", "dev": true, "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", @@ -184,12 +181,15 @@ "dev": true }, "node_modules/binary-extensions": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", - "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", + "integrity": "sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==", "dev": true, "engines": { "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/brace-expansion": { @@ -689,9 +689,9 @@ } }, "node_modules/istanbul-reports": { - "version": "3.1.6", - "resolved": "https://registry.npmjs.org/istanbul-reports/-/istanbul-reports-3.1.6.tgz", - "integrity": "sha512-TLgnMkKg3iTDsQ9PbPTdpfAK2DzjF9mqUG7RMgcQl8oFjad8ob4laGxv5XV5U9MAfx8D6tSJiUyuAwzLicaxlg==", + "version": "3.1.7", + "resolved": "https://registry.npmjs.org/istanbul-reports/-/istanbul-reports-3.1.7.tgz", + "integrity": "sha512-BewmUXImeuRk2YY0PVbxgKAysvhRPUQE0h5QRM++nVWyubKGV0l8qQ5op8+B2DOmwSe63Jivj0BjkPQVf8fP5g==", "dev": true, "dependencies": { "html-escaper": "^2.0.0", @@ -789,18 +789,10 @@ "node": ">=10" } }, - "node_modules/minimist": { - "version": "1.2.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", - "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/mocha": { - "version": "10.3.0", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-10.3.0.tgz", - "integrity": "sha512-uF2XJs+7xSLsrmIvn37i/wnc91nw7XjOQB8ccyx5aEgdnohr7n+rEiZP23WkCYHjilR6+EboEnbq/ZQDz4LSbg==", + "version": "10.4.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-10.4.0.tgz", + "integrity": "sha512-eqhGB8JKapEYcC4ytX/xrzKforgEc3j1pGlAXVy3eRwrtAy5/nIfT1SvgGzfN0XZZxeLq0aQWkOUAmqIJiv+bA==", "dev": true, "dependencies": { "ansi-colors": "4.1.1", diff --git a/tools/package.json b/tools/package.json index 0c73f37b..73a9cb90 100644 --- a/tools/package.json +++ b/tools/package.json @@ -20,9 +20,6 @@ "c8": "^9.1.0", "mocha": "^10.3.0" }, - "dependencies": { - "minimist": "^1.2.8" - }, "scripts": { "build": "pwsh ./build.ps1 && npm run validate", "test": "c8 mocha", diff --git a/tools/transform.js b/tools/transform.js index 5353e70b..7496ed1a 100755 --- a/tools/transform.js +++ b/tools/transform.js @@ -1,7 +1,7 @@ #!/usr/bin/env node /* TODO: -- glob for source file patterns +- glob for source file patterns - arguments for - info-title/description/version? - externalDocs-url/description? @@ -25,12 +25,9 @@ try { argv = parseArgs({ options: { basePath: { type: "string", default: "/service-root" }, - description: { type: "string" }, diagram: { type: "boolean", short: "d" }, help: { type: "boolean", short: "h" }, host: { type: "string", default: "localhost" }, - keep: { type: "string", short: "k", multiple: true }, - levels: { type: "string" }, "odata-version": { type: "string", short: "v", default: "4.0" }, "openapi-version": { type: "string", short: "o", default: "3.0.0" }, pretty: { type: "boolean", short: "p" }, @@ -38,7 +35,6 @@ try { scheme: { type: "string", default: "https" }, skipBatchPath: { type: "boolean" }, target: { type: "string", short: "t" }, - title: { type: "string" }, "used-schemas-only": { type: "boolean", short: "u" }, verbose: { type: "boolean" }, }, From 7599e225bfd8898d1d7c1e81adea7d25293dddd1 Mon Sep 17 00:00:00 2001 From: Ralf Handl Date: Wed, 24 Apr 2024 11:02:41 +0200 Subject: [PATCH 3/4] Update transform.js --- tools/transform.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/transform.js b/tools/transform.js index 7496ed1a..13268f9b 100755 --- a/tools/transform.js +++ b/tools/transform.js @@ -28,7 +28,6 @@ try { diagram: { type: "boolean", short: "d" }, help: { type: "boolean", short: "h" }, host: { type: "string", default: "localhost" }, - "odata-version": { type: "string", short: "v", default: "4.0" }, "openapi-version": { type: "string", short: "o", default: "3.0.0" }, pretty: { type: "boolean", short: "p" }, references: { type: "boolean", short: "r" }, From 97ed60312c71c209cd14797855ca62a9a55b3573 Mon Sep 17 00:00:00 2001 From: Ralf Handl Date: Wed, 24 Apr 2024 11:37:47 +0200 Subject: [PATCH 4/4] Bump version --- tools/package-lock.json | 4 ++-- tools/package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/package-lock.json b/tools/package-lock.json index 3ca1671f..579642c3 100644 --- a/tools/package-lock.json +++ b/tools/package-lock.json @@ -1,12 +1,12 @@ { "name": "odata-openapi-xslt", - "version": "0.8.1", + "version": "0.9.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "odata-openapi-xslt", - "version": "0.8.1", + "version": "0.9.1", "license": "SEE LICENSE IN ../LICENSE.md", "bin": { "odata-openapi": "transform.js" diff --git a/tools/package.json b/tools/package.json index 73a9cb90..e06e3407 100644 --- a/tools/package.json +++ b/tools/package.json @@ -1,6 +1,6 @@ { "name": "odata-openapi-xslt", - "version": "0.8.1", + "version": "0.9.1", "description": "OData to OpenAPI transformer", "homepage": "https://github.com/oasis-tcs/odata-openapi/tree/master/tools#transformjs-for-nodejs", "bugs": "https://github.com/oasis-tcs/odata-csdl-schemas/issues",