@sap/cli-core
Version:
Command-Line Interface (CLI) Core Module
99 lines (98 loc) • 4.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addCommands = void 0;
const discovery_1 = require("../../discovery");
const logger_1 = require("../../logger");
const constants_1 = require("../../constants");
const handler_1 = require("../handler");
const commands_1 = require("../../utils/commands");
const utils_1 = require("./utils");
const getLogger = () => (0, logger_1.get)("commands.openAPI");
const getCommandDescription = (document, segments, index) => {
const segment = segments
.slice(0, index + 1)
.reduce((prev, curr) => (prev !== "" ? `${prev} ${curr}` : curr), "");
return document.tags.find((t) => t.name === segment)?.description ?? "";
};
const addCommandToArray = (document, commands, segments, index) => {
const segment = segments[index];
let command = commands.find((c) => c.command === segment);
if (command?.type === "topCommand") {
return addCommandToArray(document, command.subCommands, segments, index + 1);
}
if (index + 1 < segments.length) {
command = {
type: "topCommand",
description: getCommandDescription(document, segments, index),
command: segment,
subCommands: [],
options: [constants_1.OPTION_HOST],
};
commands.push(command);
return addCommandToArray(document, command.subCommands, segments, index + 1);
}
// @ts-expect-error ts(2322) handler is missing - OK
command = {
type: "command",
description: getCommandDescription(document, segments, index),
command: segment,
options: [],
};
// @ts-expect-error ts(2345) handler is missing - OK
commands.push(command);
// @ts-expect-error ts(2345) handler is missing - OK
return { command, remove: () => commands.splice(commands.length - 1, 1) };
};
const setHandler = (command, method, path, parameterMappings, readPathResponseHandler, ...handler) => {
// eslint-disable-next-line no-param-reassign
command.handler = (0, handler_1.createNextHandler)("openAPI.command", (0, handler_1.createParseArgumentsHandler)(), (0, handler_1.createMandatoryOptionsHandler)(), (0, handler_1.createAuthenticationHandler)(), ...handler, (0, handler_1.createFetchHandler)(method, path, parameterMappings, readPathResponseHandler));
};
const addCommands = async (program) => {
const { error, debug, trace } = getLogger();
try {
const doc = await (0, discovery_1.init)();
const commands = [];
for (const path of Object.keys(doc.paths)) {
const pathItem = doc.paths[path];
for (const method of Object.keys(pathItem).filter((i) => i !== "parameters")) {
const handler = [];
let remove;
try {
const parameterMappings = [];
const operation = pathItem[method];
const cmd = addCommandToArray(doc, commands, operation.operationId.split(" "), 0);
remove = cmd.remove;
const { command } = cmd;
command.description = (0, utils_1.getDescriptionForCommand)(command, operation);
(0, utils_1.handleRequestBody)(operation, handler, doc, parameterMappings, command);
(0, utils_1.handleForceOption)(operation, handler);
(0, utils_1.handleResponses)(operation, command);
(0, utils_1.handleParameters)(operation, doc, parameterMappings, command, (0, utils_1.getSchema)(pathItem.parameters || [], doc));
(0, utils_1.handleDeprecationNotice)(operation, command);
const readPathResponseHandler = (0, utils_1.handleReadPathHandler)(doc, operation);
setHandler(command, method, (0, utils_1.updatePath)(doc, path), parameterMappings, readPathResponseHandler, ...[(0, handler_1.createOptionsHandler)(command.options), ...handler]);
delete command.options;
}
catch (err) {
error("cannot add command", err.stack);
remove();
}
}
}
for (const command of commands) {
trace("adding command %s", JSON.stringify(command));
try {
// eslint-disable-next-line no-await-in-loop
await (0, commands_1.buildCommand)(program, command);
}
catch (err) {
error("error while adding command %s", JSON.stringify(command, null, 2), err.stack);
}
}
}
catch (err) {
debug("error while initializing discovery", err.stack);
throw err;
}
};
exports.addCommands = addCommands;