From 492b12de40709918eb31aac32e561b8f1da4b1a7 Mon Sep 17 00:00:00 2001 From: Chapman Pendery Date: Sun, 8 Oct 2023 09:55:50 -0700 Subject: [PATCH] fix: have version flag track package.json version Signed-off-by: Chapman Pendery --- src/index.ts | 3 ++- src/utils/version.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/utils/version.ts diff --git a/src/index.ts b/src/index.ts index 8172321..49078fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,13 +9,14 @@ import { Command } from "commander"; import bind from "./commands/bind.js"; import { action, supportedShells } from "./commands/root.js"; +import { getVersion } from "./utils/version.js"; const program = new Command(); program .name("inshellisense") .description("IDE style command line auto complete") - .version("0.0.0", "-v, --version", "output the current version") + .version(await getVersion(), "-v, --version", "output the current version") .option("-s, --shell ", `shell to use for command execution, supported shells: ${supportedShells}`) .option("-c, --command ", "command to use as initial input") .option("--history", "get the last command execute") diff --git a/src/utils/version.ts b/src/utils/version.ts new file mode 100644 index 0000000..0725f7f --- /dev/null +++ b/src/utils/version.ts @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import url from "node:url"; +import path from "node:path"; +import fsAsync from "node:fs/promises"; + +const __filename = url.fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +export const getVersion = async (): Promise => { + const packageJsonPath = path.join(__dirname, "..", "..", "package.json"); + const packageJson = await fsAsync.readFile(packageJsonPath, { encoding: "utf-8" }); + const packageJsonParsed = JSON.parse(packageJson); + return packageJsonParsed.version; +};