diff --git a/src/bin/index.test.ts b/src/bin/index.test.ts index 1ae744d91..8ed7fa0a9 100644 --- a/src/bin/index.test.ts +++ b/src/bin/index.test.ts @@ -3,6 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import z from "zod"; import { bin } from "./index.js"; +import { getVersionFromPackageJson } from "./packageJson.js"; const mockCancel = vi.fn(); const mockOutro = vi.fn(); @@ -63,6 +64,7 @@ vi.mock("./promptForMode.js", () => ({ describe("bin", () => { beforeEach(() => { vi.spyOn(console, "clear").mockImplementation(() => undefined); + vi.spyOn(console, "log").mockImplementation(() => undefined); }); it("returns 1 when promptForMode returns an undefined mode", async () => { @@ -190,4 +192,14 @@ describe("bin", () => { ); expect(result).toEqual(code); }); + + it("prints the version when the --version flag is passed", async () => { + const args = ["--version"]; + const version = await getVersionFromPackageJson(); + + const result = await bin(args); + + expect(console.log).toHaveBeenCalledWith(version); + expect(result).toBe(0); + }); }); diff --git a/src/bin/index.ts b/src/bin/index.ts index d5823d358..923727759 100644 --- a/src/bin/index.ts +++ b/src/bin/index.ts @@ -44,17 +44,24 @@ export async function bin(args: string[]) { type: "boolean", }, mode: { type: "string" }, + version: { + short: "v", + type: "boolean", + }, }, strict: false, }); - const help = values.help; - - if (help) { + if (values.help) { logHelpText([introPrompts, ...introWarnings]); return 0; } + if (values.version) { + console.log(version); + return 0; + } + prompts.intro(introPrompts); logLine();