@sap/cli-core
Version:
Command-Line Interface (CLI) Core Module
41 lines (40 loc) • 1.51 kB
JavaScript
import prompts from "prompts";
import { get as getConfig, set as setConfig } from "../../../config/index.js";
import { OPTION_FILE_PATH, OPTION_INPUT } from "../../../constants.js";
import { get } from "../../../logger/index.js";
import { buildOption } from "../../../utils/commands.js";
import { logVerbose } from "../../../logger/utils.js";
export const create = () => async (command) => {
command.addOption(await buildOption(command, OPTION_INPUT));
return async () => {
const config = getConfig();
const logger = get("handler.input.input");
if (config.options[OPTION_FILE_PATH.longName]) {
throw new Error(`input provided through option --${OPTION_FILE_PATH.longName}`);
}
let input = config.options[OPTION_INPUT.longName];
if (!input) {
input = (await prompts.prompt({
type: "text",
message: `Please provide input as a string to use for the command:`,
name: "value",
})).value;
}
if (!input) {
throw new Error("no input");
}
logger.debug("reading request body from data");
try {
setConfig({
data: {
type: "json",
content: JSON.parse(input),
},
});
}
catch (err) {
logVerbose(logger, "Failed to parse input data. Is it a valid escaped JSON string?");
throw err;
}
};
};