UNPKG

@sap/cli-core

Version:

Command-Line Interface (CLI) Core Module

81 lines (80 loc) 3.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOptionValueFromArgv = exports.compareEtags = exports.checkVersion = exports.addCommandsFromFolder = void 0; exports.verifyNodeVersion = verifyNodeVersion; const path_1 = __importDefault(require("path")); const fs_extra_1 = __importDefault(require("fs-extra")); const compare_versions_1 = require("compare-versions"); const logger_1 = require("../logger"); const discovery_1 = require("../discovery"); const commands_1 = require("../utils/commands"); const core_1 = require("../config/core"); const utils_1 = require("../utils/utils"); const getLogger = () => (0, logger_1.get)("dwc.utils"); const addCommandsFromFolder = async (pathToFolder, program) => { const { error, debug } = getLogger(); for (const file of (await fs_extra_1.default.readdir(pathToFolder, { encoding: "utf-8", withFileTypes: false, })).filter((f) => f.includes(".command") && !f.endsWith(".d.ts"))) { try { // eslint-disable-next-line import/no-dynamic-require, global-require, @typescript-eslint/no-var-requires const content = require(path_1.default.join(pathToFolder, file)); if (content.addCommands) { debug("adding commands from file %s.addCommands", file); // eslint-disable-next-line no-await-in-loop await content.addCommands(program); } else { debug("adding command from file %s.default", file); // eslint-disable-next-line no-await-in-loop await (0, commands_1.buildCommand)(program, content.default); } } catch (err) { error("error while adding command from file %s:", file, err.stack); } } }; exports.addCommandsFromFolder = addCommandsFromFolder; function verifyNodeVersion() { const { output } = getLogger(); const { node } = (0, utils_1.getEngines)(); const version = process.version.replace("v", "").trim(); if (!(0, compare_versions_1.satisfies)(version, node)) { output(`WARNING: the current node version ${version} does not satisfy the required node version ${node}.` + ` The CLI might not behave as expected. Please make sure to install a matching node version.`); } } const checkVersion = async () => { const { output, error } = getLogger(); try { const result = await (0, discovery_1.checkVersion)(); if (result.status === "OUTDATED") { output(`Your local CLI installation is outdated. Run 'npm install ${(0, core_1.getPackageName)()}@latest [-g]' to update`); } } catch (err) { error(err.stack); } }; exports.checkVersion = checkVersion; const compareEtags = async () => { if (!(await (0, discovery_1.compareEtags)())) { const { output } = getLogger(); output(`Your local CLI cache is outdated. Run '${(0, utils_1.getBin)()} config cache init' to update`); } }; exports.compareEtags = compareEtags; const getOptionValueFromArgv = (command, option) => { const shortFlag = (0, commands_1.getShortFlagForLongName)(command, option); const index = process.argv.findIndex((a) => a === `-${shortFlag}` || a === `--${option.longName}`); if (index > -1) { return process.argv[index + 1]; } throw new Error(`option ${option.longName} not found in argv`); }; exports.getOptionValueFromArgv = getOptionValueFromArgv;