From b81675b998b351c47642a8f4bfe836b1e7393fc9 Mon Sep 17 00:00:00 2001 From: Richard Herman Date: Fri, 28 Jun 2024 17:01:15 +0100 Subject: [PATCH] fix: -v incorrectly using previous version --- package.json | 3 ++- src/cli.ts | 2 +- src/config.ts | 4 ++-- src/package-manager.ts | 23 +++++++++++++++++++---- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index c8d0ab7..645e180 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "watch:exports": "tsup --watch", "lint": "eslint . --ext .ts --max-warnings 0", "lint:fix": "prettier \"./src/**/*.ts\" --write", - "preversion": "npm run build && npm run lint" + "preversion": "npm run lint", + "version": "npm run build" }, "prettier": "@elgato/prettier-config", "repository": { diff --git a/src/cli.ts b/src/cli.ts index aec3efd..1dce67f 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -3,7 +3,7 @@ import { program } from "commander"; import { config, create, link, pack, restart, setDeveloperMode, stop, validate } from "./commands"; import { packageManager } from "./package-manager"; -program.version(packageManager.getVersion(), "-v", "display CLI version"); +program.version(packageManager.getVersion({ checkEnvironment: true }), "-v", "display CLI version"); program .command("create") diff --git a/src/config.ts b/src/config.ts index b8b9b37..f2a8ef4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,7 +6,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { homedir, platform } from "node:os"; import { dirname, join } from "node:path"; -import { relative } from "./system/path"; +import { packageManager } from "./package-manager"; let __config: Config | undefined = undefined; @@ -17,7 +17,7 @@ export const defaultConfig: Config = Object.freeze({ npm: { cli: { mode: "prod" as const, - version: `^${JSON.parse(readFileSync(relative("../package.json"), { encoding: "utf-8" })).version}`, + version: `^${packageManager.getVersion()}`, }, streamDeck: { mode: "prod" as const, diff --git a/src/package-manager.ts b/src/package-manager.ts index fc6e75c..8f563e2 100644 --- a/src/package-manager.ts +++ b/src/package-manager.ts @@ -42,11 +42,16 @@ class PackageManager { } /** - * Gets the version of the current package; when installed as part of a development environment, the version is suffixed appropriately. - * @returns Version. + * Gets the current version of the CLI from the package JSON file. + * @param opts Version format options. + * @returns The version, for example `0.3.0`. */ - public getVersion(): string { - return existsSync(relative("../src")) ? `${version} (dev)` : version; + public getVersion(opts: GetVersionOptions = {}): string { + if (opts.checkEnvironment && existsSync(relative("../src"))) { + return `${version} (dev)`; + } + + return version; } /** @@ -251,3 +256,13 @@ export type PackageMetadataVersion = { tarball: string; }; }; + +/** + * Options for {@link PackageManager.getVersion}. + */ +type GetVersionOptions = { + /** + * Determines whether to check the if the CLI is in development mode; when `true` the version will include a suffix of `(dev)`. + */ + checkEnvironment?: boolean; +};