Skip to content

Commit

Permalink
feat: add -v flag to display version
Browse files Browse the repository at this point in the history
  • Loading branch information
xl4624 committed Nov 30, 2023
1 parent 9b71973 commit 79d0e08
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/bin/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chalk from "chalk";
import { beforeEach, describe, expect, it, vi } from "vitest";
import z from "zod";

import { getVersionFromPackageJson } from "./packageJson.js";
import { bin } from "./index.js";

Check failure on line 6 in src/bin/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Expected "./index.js" to come before "./packageJson.js"

const mockCancel = vi.fn();
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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);
});
});
13 changes: 10 additions & 3 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 79d0e08

Please sign in to comment.