@interopio/cli
Version:
Interop.io CLI - a command line for creating Interop.io applications
52 lines (51 loc) • 2.22 kB
JavaScript
import fs from "fs-extra/esm";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { CLIENT_TEMPLATE, PLATFORM_TEMPLATE } from "../shared/constants.js";
import locatorService from "./locator.service.js";
export class ValidationService {
currentDir = process.cwd();
async transformVersion(version, product, mode) {
// version must be either "latest" or "3.4"; if not throw error
if (!version || version.length === 0 || version === "latest") {
return "latest";
}
let manifest;
if (mode === "remote") {
manifest = await locatorService.downloaderService.downloadRemoteManifest();
}
if (mode === "offline") {
const { root } = locatorService.configurationService.directoryConfig;
manifest = await fs.readJSON(join(root, "manifest.json"), { throws: false });
}
if (!manifest) {
throw new Error("Could not find manifest file.");
}
const manifestProduct = manifest[product];
if (manifestProduct.latestVersion === version) {
return "latest";
}
if (manifestProduct.versions.includes(version)) {
return version;
}
throw new Error(`Could not find version ${version} for product ${product} in mode ${mode}.`);
}
async validateAppName(appName) {
if (typeof appName !== "string" || appName.length === 0) {
throw new Error("Please prove a valid app name.");
}
const appDirExists = existsSync(join(this.currentDir, appName));
if (appDirExists) {
throw new Error(`A directory with the name ${appName} already exists in the current directory. Please choose a different name.`);
}
}
validateAppTypeAndTemplate(appType, template) {
const errorMsg = `Invalid template ${template} for ${appType} app`;
if (appType === "browser-platform" && !Object.values(PLATFORM_TEMPLATE).includes(template)) {
throw new Error(errorMsg);
}
if (appType === "browser-client" && !Object.values(CLIENT_TEMPLATE).includes(template)) {
throw new Error(errorMsg);
}
}
}