Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/command/command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
import "dotenv/config";
import { readFileSync } from "fs";
import yargsFactory from "yargs/yargs";
import { hideBin } from "yargs/helpers";
import importerCommand from "../import/command.js";
Expand All @@ -14,9 +15,19 @@ import { excelExportCommand, excelImportCommand } from "../excel/command.js";
import { checkCommand } from "./helpers.js";

const yargs = yargsFactory(hideBin(process.argv));
let cliVersion = "unknown";
try {
const pkg = JSON.parse(
readFileSync(new URL("../../package.json", import.meta.url), "utf8")
);
cliVersion = pkg?.version ?? cliVersion;
} catch {
// Keep default version when package.json is unavailable/unreadable.
}

const argv = await yargs
.usage("flotiq [command]")
.version(cliVersion)
Comment thread
KarolNet marked this conversation as resolved.
.help()
.alias("help", "h")
.command(exporterCommand)
Expand Down
44 changes: 44 additions & 0 deletions tests/commands/version-command.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { execFileSync } from "child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs";
import { tmpdir } from "os";
import path from "path";
import { fileURLToPath } from "url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const cliBinPath = path.resolve(__dirname, "../../bin/flotiq");
const cliPackageJsonPath = path.resolve(__dirname, "../../package.json");
const cliVersion = JSON.parse(readFileSync(cliPackageJsonPath, "utf8")).version;

function runVersionCommand(cwd) {
return execFileSync(process.execPath, [cliBinPath, "--version"], {
cwd,
encoding: "utf8",
}).trim();
}

describe("CLI version output", () => {
let tempDir;

afterEach(() => {
if (tempDir) {
rmSync(tempDir, { recursive: true, force: true });
tempDir = undefined;
}
});

it("should ignore package.json from the current working directory", () => {
tempDir = mkdtempSync(path.join(tmpdir(), "flotiq-version-"));
writeFileSync(
path.join(tempDir, "package.json"),
JSON.stringify({ name: "foreign-project", version: "4.57.2" })
);

expect(runVersionCommand(tempDir)).toBe(cliVersion);
});

it("should print installed CLI version when cwd has no package.json", () => {
tempDir = mkdtempSync(path.join(tmpdir(), "flotiq-version-"));

expect(runVersionCommand(tempDir)).toBe(cliVersion);
});
});
Loading