diff --git a/src/util/extract-tar-gz.ts b/src/util/extract-tar-gz.ts index c87000b..d122903 100644 --- a/src/util/extract-tar-gz.ts +++ b/src/util/extract-tar-gz.ts @@ -5,6 +5,7 @@ import { existsSync, mkdirSync } from "fs"; const requiredFiles = ["index.html", "nomo_icon.svg", "nomo_manifest.json"]; const cacheDirectory = "./cache"; +const cacheOutDirectory = "./cache/out"; export async function extractAndCache(args: { tarFilePath: string; @@ -36,7 +37,6 @@ export async function extractAndCache(args: { const missingFiles = requiredFiles.filter((file) => { const filePath = join(resolve(destinationDir), "/out/", file); - console.log(filePath); return !existsSync(filePath); }); if (missingFiles.length > 0) { @@ -51,3 +51,27 @@ export async function extractAndCache(args: { logFatal(`Error extracting tar.gz file: ${error}`); } } + +export function getCachedIndexHtmlPath(): string { + const path = join(resolve(cacheOutDirectory), "index.html"); + if (!existsSync(path)) { + logFatal(`Error: ${path} is missing.`); + } + return path; +} + +export function getCachedNomoIconPath(): string { + const path = join(resolve(cacheOutDirectory), "nomo_icon.svg"); + if (!existsSync(path)) { + logFatal(`Error: ${path} is missing.`); + } + return path; +} + +export function getCachedNomoManifestPath(): string { + const path = join(resolve(cacheOutDirectory), "nomo_manifest.json"); + if (!existsSync(path)) { + logFatal(`Error: ${path} is missing.`); + } + return path; +} diff --git a/src/util/ssh-manager.ts b/src/util/ssh-manager.ts index 45c00d9..326c2a1 100644 --- a/src/util/ssh-manager.ts +++ b/src/util/ssh-manager.ts @@ -1,5 +1,13 @@ import { logFatal, readCliConfig, runCommandsSequentially } from "../util/util"; -import { extractAndCache } from "../util/extract-tar-gz"; +import { + extractAndCache, + getCachedIndexHtmlPath, + getCachedNomoIconPath, + getCachedNomoManifestPath, +} from "../util/extract-tar-gz"; +import { NomoManifest } from "../init/interface"; +import * as fs from "fs"; +import { validateManifest } from "../util/validate-manifest"; let sshConnect = ""; @@ -25,9 +33,13 @@ export async function connectToSSH(args: { await extractAndCache({ tarFilePath: archive, }); + const manifestPath = getCachedNomoManifestPath(); + manifestChecks(manifestPath); const commands = [ls(), checkCreateDir(sshBaseDir)]; await runCommandsSequentially(commands); + + const iconPath = getCachedNomoIconPath(); } function ls(): string { @@ -38,3 +50,9 @@ function checkCreateDir(sshBaseDir: string): string { const mkdirCommand = `if [ ! -d ${sshBaseDir} ]; then mkdir -p ${sshBaseDir} && echo "Directory created"; else echo "Directory already exists"; fi`; return `${sshConnect} "${mkdirCommand}"`; } + +function manifestChecks(manifestFilePath: string) { + const nomoManifestContent = fs.readFileSync(manifestFilePath, "utf-8"); + const nomoManifest: NomoManifest = JSON.parse(nomoManifestContent); + validateManifest(nomoManifest); +} diff --git a/src/util/validate-manifest.ts b/src/util/validate-manifest.ts index 857f146..eb79700 100644 --- a/src/util/validate-manifest.ts +++ b/src/util/validate-manifest.ts @@ -1,18 +1,17 @@ import { NomoManifest } from "../init/interface"; +import { logFatal } from "../util/util"; class WebOnError extends Error { constructor(message: string) { super(message); this.name = "WebOnError"; + logFatal(this.message); } } -async function validateManifest( - manifest: NomoManifest, - webonUrl: string, - { devMode }: { devMode: boolean } -): Promise { +export async function validateManifest(manifest: NomoManifest): Promise { const webonVersion = manifest.webon_version; + if (!_isValidSemanticVersion(webonVersion)) { throw new WebOnError( `webon_version ${webonVersion} does not comply with semantic versioning regexp` @@ -36,22 +35,30 @@ async function validateManifest( } const minNomoVersion = manifest.min_nomo_version; - if (minNomoVersion != null) { - if (!_isValidSemanticVersion(minNomoVersion)) { - throw new WebOnError( - `min_nomo_version ${minNomoVersion} does not comply with semantic versioning regexp` - ); - } - // Assume you have a function similar to versionTwoGreaterThanVersionOne - const currentVersion = "1.2.0"; // You need to replace this with the actual version - if (versionTwoGreaterThanVersionOne(currentVersion, minNomoVersion)) { - throw new WebOnError( - `Nomo App outdated! This WebOn requires ${minNomoVersion}, but the current version is ${currentVersion}` - ); - } + const webOnVersion = manifest.webon_version; + + //if (minNomoVersion != null) { + // if (!_isValidSemanticVersion(minNomoVersion)) { + // throw new WebOnError( + // `min_nomo_version ${minNomoVersion} does not comply with semantic versioning regexp` + // ); + // } + // Assume you have a function similar to versionTwoGreaterThanVersionOne + const currentVersion = "0.1.0"; + // TODO: set the currentVersion to manifest.webon_version and compare it to the manifest version from server + console.log("currentVersion: " + currentVersion); + console.log("webOnversion" + webOnVersion); + if (versionTwoGreaterThanVersionOne(currentVersion, webOnVersion)) { + throw new WebOnError( + `Your WebOn is outdated! This WebOn requires ${webOnVersion}, but the current version is ${currentVersion}` + ); + } else if (currentVersion === webOnVersion) { + throw new WebOnError( + `Your webOn version is equal to the version your already uploaded: ${webOnVersion}, please update your webOn_version in nomo_manifest.json.` + ); } } - +//} function _isValidSemanticVersion(version: string): boolean { const pattern = /^(\d+)\.(\d+)\.(\d+)$/; @@ -59,17 +66,29 @@ function _isValidSemanticVersion(version: string): boolean { return regex.test(version); } -// Assuming versionTwoGreaterThanVersionOne is a function you have implemented function versionTwoGreaterThanVersionOne( versionTwo: string, versionOne: string ): boolean { - // Implement the comparison logic here + const v1Components = versionOne.split("."); + const v2Components = versionTwo.split("."); + + for (let i = 0; i < Math.max(v1Components.length, v2Components.length); i++) { + const v1 = parseInt(v1Components[i] || "0", 10); + const v2 = parseInt(v2Components[i] || "0", 10); + + if (v1 > v2) { + return false; + } else if (v1 < v2) { + return true; + } + } + return false; } export function isValidWebOnId(webon_id: string): boolean { - const webonIdRegExp = - /^(?:[a-zA-Z0-9_-]+\.)*[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)+$/; - return webonIdRegExp.test(webon_id); - } \ No newline at end of file + const webonIdRegExp = + /^(?:[a-zA-Z0-9_-]+\.)*[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)+$/; + return webonIdRegExp.test(webon_id); +}