@sap/cli-core
Version:
Command-Line Interface (CLI) Core Module
72 lines (71 loc) • 2.85 kB
JavaScript
;
/* eslint-disable no-await-in-loop */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.init = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const constants_1 = require("../../../../constants");
const discovery_1 = require("../../../../discovery");
const handler_1 = require("../../../handler");
const config_1 = require("../../../../config");
const core_1 = require("../../../../config/core");
const ResultHandlerFactory_1 = require("../../../../result/ResultHandlerFactory");
const utils_1 = require("./utils");
const utils_2 = require("../../../handler/utils");
const logger_1 = require("../../../../logger");
const pre = () => {
(0, config_1.set)({
doNotStoreResult: false,
doNotProcessResponseData: true,
});
};
const post = async (discovery) => {
const config = (0, config_1.get)();
await (0, discovery_1.addMetadata)({
tenant: config.publicfqdn,
addedAt: new Date().getTime(),
});
(0, discovery_1.clear)();
const { options } = config;
delete options[constants_1.OPTION_OUTPUT.longName];
(0, config_1.set)({ options });
ResultHandlerFactory_1.ResultHandlerFactory.get().setResult(undefined);
await fs_extra_1.default.writeFile((0, discovery_1.getPathToDiscoveryDocument)(), JSON.stringify(discovery));
};
const init = async () => async () => {
pre();
const { output } = (0, logger_1.get)("init.command");
const discovery = {
info: {
version: constants_1.VERSION,
"x-document-version": constants_1.VERSION,
},
openapi: "3.0.3",
paths: {},
tags: [],
components: {},
};
const paths = (0, core_1.getDiscoveryPaths)();
if (paths.length > 1 &&
(0, utils_2.getAuthenticationMethod)() !== constants_1.AuthenticationMethod.oauth) {
output("Only OAuth authentication is supported");
throw new Error("only oauth authentication is supported");
}
for (const path of paths) {
await (await (0, handler_1.createFetchHandler)("GET", path)({}))();
const newDiscovery = ResultHandlerFactory_1.ResultHandlerFactory.get().getResult();
(0, utils_1.mergeDiscoveries)(discovery, newDiscovery);
}
await post(discovery);
};
exports.init = init;
const initCommand = {
type: "command",
command: "init",
description: "initialize the local CLI cache",
options: [],
handler: (0, handler_1.createNextHandler)("command.config.cache.init", (0, handler_1.createParseArgumentsHandler)(), (0, handler_1.createMandatoryOptionsHandler)(), (0, handler_1.createAuthenticationHandler)(), exports.init),
};
exports.default = initCommand;