Skip to content

Commit

Permalink
Extract and test CLI logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfhandl committed Jan 5, 2024
1 parent 17fde13 commit 06658fb
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env node
"use strict";

//console.dir(argv);

//TODO: what to require?
const csdl = require("odata-csdl");
const lib = require("./csdl2openapi");
const minimist = require("minimist");
const fs = require("fs");
const { parseArgs } = require("./cliParts");
const { stringifyStream } = require("@discoveryjs/json-ext");

var unknown = false;

const args = parseArgs(process.argv);

var argv = minimist(process.argv.slice(2), {
string: [
"basePath",
Expand Down Expand Up @@ -101,7 +101,11 @@ function convert(source) {
skipBatchPath: argv.skipBatchPath,
defaultTitle: argv.title,
defaultDescription: argv.description,
rootResourcesToKeep: Array.isArray(argv.keep) ? argv.keep : argv.keep? [argv.keep]: undefined,
rootResourcesToKeep: Array.isArray(argv.keep)
? argv.keep
: argv.keep
? [argv.keep]
: undefined,
});

stringifyStream(openapi, null, argv.pretty ? 4 : 0).pipe(
Expand Down
85 changes: 85 additions & 0 deletions lib/cliParts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Parse command-line options
*
* Latest version: https://github.com/oasis-tcs/odata-openapi/blob/main/lib/cliOptions.js
*/

const minimist = require("minimist");

module.exports = { parseArgs };

function parseArgs(argv) {
let unknown = false;
const args = minimist(argv.slice(2), {
string: [
"basePath",
"description",
"host",
"keep",
"levels",
"openapi-version",
"scheme",
"target",
"title",
],
boolean: [
"diagram",
"help",
"pretty",
"skipBatchPath",
"used-schemas-only",
],
alias: {
d: "diagram",
h: "help",
k: "keep",
o: "openapi-version",
p: "pretty",
t: "target",
u: "used-schemas-only",
},
default: {
pretty: false,
},
unknown: (arg) => {
if (arg.substring(0, 1) == "-") {
console.error("Unknown option: " + arg);
unknown = true;
return false;
}
},
});

const options = {};

//TODO: options
// scheme: args.scheme,
// host: args.host,
// basePath: args.basePath,
// diagram: args.diagram,
// maxLevels: Number(args.levels),
// openapiVersion: args.o,
// messages: [],
// skipBatchPath: args.skipBatchPath,
// defaultTitle: args.title,
// defaultDescription: args.description,
// rootResourcesToKeep: Array.isArray(args.keep)
// ? args.keep
// : args.keep
// ? [args.keep]
// : undefined,

//TODO: no source provided
const source = args._[0];

return {
source,
target:
args.target || source === undefined
? undefined
: (source.lastIndexOf(".") > 0
? source.substring(0, source.lastIndexOf("."))
: source) + ".openapi3.json",
options,
};
}
13 changes: 13 additions & 0 deletions test/cliParts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const assert = require("assert");

const { parseArgs } = require("../lib/cliParts");

describe("CLI parameters", function () {
it("none", function () {
assert.deepStrictEqual(parseArgs([]), {
source: undefined,
target: undefined,
options: {},
});
});
});

0 comments on commit 06658fb

Please sign in to comment.