import { runProcess, getMtaCommand } from "./process-utils"; type SemVer = { major: number; minor: number; patch: number; }; let MTA_COMMAND_FLAGS = ["-v"]; // This version number should be synchronized with the version in the README.md and package.json const MIN_MTA_VERSION: SemVer = { major: 1, minor: 0, patch: 0, }; let minMtaVersion = MIN_MTA_VERSION; export const MIN_CLOUD_MTA_VERSION = `${MIN_MTA_VERSION.major}.${MIN_MTA_VERSION.minor}.${MIN_MTA_VERSION.patch}`; export function setMtaCommandParamsForTests(testParams: { flags?: string[]; minVersion?: SemVer; }): void { // These values should be synchronized with those above MTA_COMMAND_FLAGS = testParams.flags ?? ["-v"]; minMtaVersion = testParams.minVersion ?? MIN_MTA_VERSION; } const messages = { CLOUD_MTA_NOT_INSTALLED: "Cloud MTA is not installed in your environment. Go to https://github.com/SAP/cloud-mta to install the tool and try again.", CLOUD_MTA_UNKNOWN_VERSION: "Could not determine which Cloud MTA version is installed in your system. Go to https://github.com/SAP/cloud-mta to install the tool and try again.", CLOUD_MTA_VERSION_MISMATCH: `The Cloud MTA version is lower than the minimal supported version (${MIN_CLOUD_MTA_VERSION}). Go to https://github.com/SAP/cloud-mta to install the tool and try again.`, }; export class CloudMtaNotInstalledError extends Error {} export class CloudMtaVersionMismatchError extends Error {} export function createCloudMtaNotInstalledError(): CloudMtaNotInstalledError { return new CloudMtaNotInstalledError(messages.CLOUD_MTA_NOT_INSTALLED); } export async function ensureCloudMtaInstalled(): Promise { return ensureCloudMtaInstalledInPath(await getMtaCommand()); } export async function ensureCloudMtaInstalledInPath( mtaPath: string ): Promise { let output: string; try { output = await runProcess(mtaPath, MTA_COMMAND_FLAGS); } catch (e) { if (e.code === "ENOENT") { throw createCloudMtaNotInstalledError(); } throw new CloudMtaVersionMismatchError(messages.CLOUD_MTA_UNKNOWN_VERSION); } const versionMatch = /(\d+)\.(\d+)\.(\d+)/.exec(output); if (versionMatch === null) { throw new CloudMtaVersionMismatchError(messages.CLOUD_MTA_UNKNOWN_VERSION); } else { const version = { major: Number(versionMatch[1]), minor: Number(versionMatch[2]), patch: Number(versionMatch[3]), }; if (isVersionLower(version, minMtaVersion)) { throw new CloudMtaVersionMismatchError( messages.CLOUD_MTA_VERSION_MISMATCH ); } } } export function isCloudMtaNotInstalledError(err: Error): boolean { return err instanceof CloudMtaNotInstalledError; } function isVersionLower(first: SemVer, second: SemVer): boolean { return ( first.major < second.major || (first.major === second.major && first.minor < second.minor) || (first.major === second.major && first.minor === second.minor && first.patch < second.patch) ); }