UNPKG

@sap/cli-core

Version:

Command-Line Interface (CLI) Core Module

86 lines (85 loc) 3.43 kB
import { satisfies } from "compare-versions"; import fs from "fs-extra"; import path from "path"; import { pathToFileURL } from "url"; import { getPackageName } from "../config/core.js"; import { checkVersion as check, compareEtags as compare, } from "../discovery/index.js"; import { get } from "../logger/index.js"; import { buildCommand, getShortFlagForLongName } from "../utils/commands.js"; import { getBin, getEngines } from "../utils/utils.js"; const getLogger = () => get("dwc.utils"); /** * Helper to resolve index.js or index.ts in a command directory */ async function resolveCommandIndex(commandsFolderPath, file) { const dirPath = path.join(commandsFolderPath, file); if ((await fs.stat(dirPath)).isDirectory() === false) return dirPath; const jsIndex = path.join(dirPath, "index.js"); const tsIndex = path.join(dirPath, "index.ts"); if (await fs.pathExists(jsIndex)) { return jsIndex; } if (await fs.pathExists(tsIndex)) { return tsIndex; } throw new Error(`No index.js or index.ts found in directory: ${dirPath}`); } export const addCommandsFromFolder = async (pathToFolder, program) => { const { error, debug } = getLogger(); for (const file of (await fs.readdir(pathToFolder, { encoding: "utf-8", withFileTypes: false, })).filter((f) => f.includes(".command") && !f.endsWith(".d.ts"))) { try { const importPath = await resolveCommandIndex(pathToFolder, file); const content = await import(pathToFileURL(importPath).href); if (content.addCommands) { debug("adding commands from file %s.addCommands", file); await content.addCommands(program); } else { debug("adding command from file %s.default", file); await buildCommand(program, content.default); } } catch (err) { error("error while adding command from file %s:", file, err.stack); } } }; export function verifyNodeVersion() { const { output } = getLogger(); const { node } = getEngines(); const version = process.version.replace("v", "").trim(); if (!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.`); } } export const checkVersion = async () => { const { output, error } = getLogger(); try { const result = await check(); if (result.status === "OUTDATED") { output(`Your local CLI installation is outdated. Run 'npm install ${getPackageName()}@latest [-g]' to update`); } } catch (err) { error(err.stack); } }; export const compareEtags = async () => { if (!(await compare())) { const { output } = getLogger(); output(`Your local CLI cache is outdated. Run '${getBin()} config cache init' to update`); } }; export const getOptionValueFromArgv = (command, option) => { const shortFlag = 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`); };