From e11073dcf8a8499a2a455185349b06d0b01e58ac Mon Sep 17 00:00:00 2001 From: Dennis Meister Date: Thu, 4 Jul 2024 15:52:49 +0200 Subject: [PATCH] feat: improve latest version handling Signed-off-by: Dennis Meister --- src/modules/package.ts | 10 +++++++++ src/modules/semver.ts | 2 -- src/utils/fs-bridge.ts | 22 +++++++++++++++++++ testbench/test-exec/.velocitas.json | 2 +- .../test-init/.velocitasInvalidComponent.json | 5 ++--- testbench/test-init/.velocitasLegacy.json | 8 +++---- testbench/test-init/.velocitasNew.json | 13 +++++------ 7 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/modules/package.ts b/src/modules/package.ts index 29e2250..32ecc5b 100644 --- a/src/modules/package.ts +++ b/src/modules/package.ts @@ -18,6 +18,7 @@ import { TagResult } from 'simple-git'; import { CliFileSystem } from '../utils/fs-bridge'; import { ComponentManifest } from './component'; import { packageDownloader } from './package-downloader'; +import { getLatestVersion } from './semver'; export const MANIFEST_FILE_NAME = 'manifest.json'; @@ -80,7 +81,16 @@ export class PackageConfig { return join(getPackageFolderPath(), this.getPackageName()); } + getPackageDirectoryWithLatestVersion(): string { + const filesAndFolders = CliFileSystem.readdirSync(this.getPackageDirectory()); + const latestVersion = getLatestVersion(filesAndFolders); + return join(this.getPackageDirectory(), latestVersion); + } + getPackageDirectoryWithVersion(): string { + if (this.version === 'latest') { + return this.getPackageDirectoryWithLatestVersion(); + } return join(this.getPackageDirectory(), this.version); } diff --git a/src/modules/semver.ts b/src/modules/semver.ts index 11ce224..7d6c3d6 100644 --- a/src/modules/semver.ts +++ b/src/modules/semver.ts @@ -29,8 +29,6 @@ export function getLatestVersion(versions: string[]): string { latestVersion = currentVersion; } } - } else { - console.log('Not valid'); } } diff --git a/src/utils/fs-bridge.ts b/src/utils/fs-bridge.ts index cc5cd78..1bc6338 100644 --- a/src/utils/fs-bridge.ts +++ b/src/utils/fs-bridge.ts @@ -70,6 +70,12 @@ interface IFileSystem { * @returns The absolute path. */ realpathSync(path: fs.PathLike): string; + + /** + * Reads a directory synchronously. + * @param path A path to a file or directory. + */ + readdirSync(path: fs.PathLike): string[]; } /** @@ -122,6 +128,10 @@ class RealFileSystem implements IFileSystem { realpathSync(path: fs.PathLike): string { return fs.realpathSync(path); } + + readdirSync(path: fs.PathLike): string[] { + return fs.readdirSync(path); + } } export type MockFileSystemObj = Record; @@ -170,6 +180,10 @@ export class MockFileSystem implements IFileSystem, IFileSystemTests { return path.toString(); } + readdirSync(path: fs.PathLike): string[] { + return [this._fileSystemObj[path.toString()]]; + } + async promisesMkdir(path: string): Promise { this._fileSystemObj[path] = ''; } @@ -254,6 +268,14 @@ export class CliFileSystem { return this._impl.realpathSync(path); } + /** + * Reads a directory synchronously. + * @param path A path to a file or directory. + */ + static readdirSync(path: fs.PathLike): string[] { + return this._impl.readdirSync(path); + } + /** * Asynchronously creates a directory. * @param path A path to the directory to create. diff --git a/testbench/test-exec/.velocitas.json b/testbench/test-exec/.velocitas.json index 2eea1ca..5aa090f 100644 --- a/testbench/test-exec/.velocitas.json +++ b/testbench/test-exec/.velocitas.json @@ -1,6 +1,6 @@ { "packages": { - "devenv-runtimes": "v4.0.3", + "devenv-runtimes": "latest", "test-package": "test-version" }, "variables": { diff --git a/testbench/test-init/.velocitasInvalidComponent.json b/testbench/test-init/.velocitasInvalidComponent.json index 560237c..fadb0bf 100644 --- a/testbench/test-init/.velocitasInvalidComponent.json +++ b/testbench/test-init/.velocitasInvalidComponent.json @@ -1,6 +1,6 @@ { "packages": { - "https://github.com/eclipse-velocitas/devenv-devcontainer-setup.git": "v1.5.0" + "https://github.com/eclipse-velocitas/devenv-devcontainer-setup.git": "latest" }, "components": [ "invalid-component" @@ -8,6 +8,5 @@ "variables": { "appManifestPath": "./app/AppManifest.json", "githubRepoId": "" - }, - "cliVersion": "v0.10.0" + } } diff --git a/testbench/test-init/.velocitasLegacy.json b/testbench/test-init/.velocitasLegacy.json index 2e7f5ad..e78c06f 100644 --- a/testbench/test-init/.velocitasLegacy.json +++ b/testbench/test-init/.velocitasLegacy.json @@ -2,19 +2,19 @@ "packages": [ { "repo": "devenv-runtimes", - "version": "v4.0.0" + "version": "latest" }, { "repo": "devenv-github-workflows", - "version": "v6.0.1" + "version": "latest" }, { "repo": "devenv-github-templates", - "version": "v1.0.5" + "version": "latest" }, { "repo": "devenv-devcontainer-setup", - "version": "v2.2.1" + "version": "latest" } ], "variables": { diff --git a/testbench/test-init/.velocitasNew.json b/testbench/test-init/.velocitasNew.json index 368a9dc..cab4c75 100644 --- a/testbench/test-init/.velocitasNew.json +++ b/testbench/test-init/.velocitasNew.json @@ -1,10 +1,10 @@ { "packages": { - "https://github.com/eclipse-velocitas/pkg-velocitas-main.git": "v0.0.2", - "https://github.com/eclipse-velocitas/devenv-devcontainer-setup.git": "v2.4.4", - "https://github.com/eclipse-velocitas/devenv-runtimes.git": "v4.0.3", - "https://github.com/eclipse-velocitas/devenv-github-templates.git": "v1.0.5", - "https://github.com/eclipse-velocitas/devenv-github-workflows.git": "v6.0.4" + "https://github.com/eclipse-velocitas/pkg-velocitas-main.git": "latest", + "https://github.com/eclipse-velocitas/devenv-devcontainer-setup.git": "latest", + "https://github.com/eclipse-velocitas/devenv-runtimes.git": "latest", + "https://github.com/eclipse-velocitas/devenv-github-templates.git": "latest", + "https://github.com/eclipse-velocitas/devenv-github-workflows.git": "latest" }, "components": [ "vapp-core-python", @@ -21,6 +21,5 @@ "variables": { "appManifestPath": "./app/AppManifest.json", "githubRepoId": "" - }, - "cliVersion": "v0.10.0" + } }