UNPKG

@sap/cli-core

Version:

Command-Line Interface (CLI) Core Module

53 lines (52 loc) 2.37 kB
"use strict"; 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 fs_extra_1 = __importDefault(require("fs-extra")); const form_data_1 = __importDefault(require("form-data")); const path_1 = __importDefault(require("path")); const config_1 = require("../../../config"); const logger_1 = require("../../../logger"); const constants_1 = require("../../../constants"); const checkOptionsExistence_1 = require("../checkOptionsExistence"); const next_1 = require("../next"); const options_1 = require("../options"); const utils_1 = require("../../../logger/utils"); const readBodyFromFile = async () => async () => { const config = (0, config_1.get)(); const filePath = config.options[constants_1.OPTION_FILE_PATH.longName]; const logger = (0, logger_1.get)("commands.handler.input.file"); const { debug, trace } = logger; debug("reading request body from %s", filePath); try { if (filePath.endsWith(".json")) { (0, config_1.set)({ data: { type: "json", content: JSON.parse(await fs_extra_1.default.readFile(filePath, "utf-8")), }, }); } else { const form = new form_data_1.default(); form.append("file", fs_extra_1.default.createReadStream(filePath)); (0, config_1.set)({ data: { type: "file", content: form, }, }); } const filename = path_1.default.basename(filePath); (0, config_1.set)({ headers: { [constants_1.X_OUTPUT_FILE_NAME]: filename } }); } catch (err) { (0, utils_1.logVerbose)(logger, `Failed to read content from file ${filePath}. Does the file really exist and does it contain valid JSON?`); trace(`Failed to read content from file ${filePath}`, err); throw err; } }; const create = () => (0, next_1.create)("file.input.handler", (0, checkOptionsExistence_1.create)(constants_1.OPTION_INPUT), (0, options_1.create)(constants_1.OPTION_FILE_PATH, { readEnv: true, readFile: true, readOptions: true }, true, true), readBodyFromFile); exports.create = create;