UNPKG

@sap/cli-core

Version:

Command-Line Interface (CLI) Core Module

65 lines (64 loc) 2.56 kB
/* eslint-disable no-await-in-loop */ import fs from "fs-extra"; import { getDiscoveryPaths } from "../../../../config/core.js"; import { get as getConfig, set as setConfig, } from "../../../../config/index.js"; import { AuthenticationMethod, OPTION_OUTPUT, VERSION, } from "../../../../constants.js"; import { addMetadata as addDiscoveryMetadata, clear, getPathToDiscoveryDocument, } from "../../../../discovery/index.js"; import { get } from "../../../../logger/index.js"; import { ResultHandlerFactory } from "../../../../result/ResultHandlerFactory.js"; import { createAuthenticationHandler, createFetchHandler, createMandatoryOptionsHandler, createNextHandler, createParseArgumentsHandler, } from "../../../handler/index.js"; import { getAuthenticationMethod } from "../../../handler/utils.js"; import { mergeDiscoveries } from "./utils.js"; const pre = () => { setConfig({ doNotStoreResult: false, doNotProcessResponseData: true, }); }; const post = async (discovery) => { const config = getConfig(); await addDiscoveryMetadata({ tenant: config.publicfqdn, addedAt: new Date().getTime(), }); clear(); const { options } = config; delete options[OPTION_OUTPUT.longName]; setConfig({ options }); ResultHandlerFactory.get().setResult(undefined); await fs.writeFile(getPathToDiscoveryDocument(), JSON.stringify(discovery)); }; export const init = async () => async () => { pre(); const { output } = get("init.command"); const discovery = { info: { version: VERSION, "x-document-version": VERSION, }, openapi: "3.0.3", paths: {}, tags: [], components: {}, }; const paths = getDiscoveryPaths(); if (paths.length > 1 && getAuthenticationMethod() !== AuthenticationMethod.oauth) { output("Only OAuth authentication is supported"); throw new Error("only oauth authentication is supported"); } for (const path of paths) { await (await createFetchHandler("GET", path)({}))(); const newDiscovery = ResultHandlerFactory.get().getResult(); mergeDiscoveries(discovery, newDiscovery); } await post(discovery); }; const initCommand = { type: "command", command: "init", description: "initialize the local CLI cache", options: [], handler: createNextHandler("command.config.cache.init", createParseArgumentsHandler(), createMandatoryOptionsHandler(), createAuthenticationHandler(), init), }; export default initCommand;