@sap/cli-core
Version:
Command-Line Interface (CLI) Core Module
48 lines (47 loc) • 1.85 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.create = void 0;
const prompts_1 = __importDefault(require("prompts"));
const config_1 = require("../../../config");
const constants_1 = require("../../../constants");
const logger_1 = require("../../../logger");
const commands_1 = require("../../../utils/commands");
const utils_1 = require("../../../logger/utils");
const create = () => async (command) => {
command.addOption(await (0, commands_1.buildOption)(command, constants_1.OPTION_INPUT));
return async () => {
const config = (0, config_1.get)();
const logger = (0, logger_1.get)("handler.input.input");
if (config.options[constants_1.OPTION_FILE_PATH.longName]) {
throw new Error(`input provided through option --${constants_1.OPTION_FILE_PATH.longName}`);
}
let input = config.options[constants_1.OPTION_INPUT.longName];
if (!input) {
input = (await prompts_1.default.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 {
(0, config_1.set)({
data: {
type: "json",
content: JSON.parse(input),
},
});
}
catch (err) {
(0, utils_1.logVerbose)(logger, "Failed to parse input data. Is it a valid escaped JSON string?");
throw err;
}
};
};
exports.create = create;