Skip to content
Closed
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
12 changes: 12 additions & 0 deletions bin/codepact.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Options:
--write-baseline <file> Record current findings as accepted.
--baseline <file> Report and fail only on findings not in the baseline.
-h, --help Show this message.
-v, --version Show the package version.

Exit codes:
0 no errors (and no warnings when --strict is set)
Expand All @@ -43,6 +44,7 @@ function parseArguments(argv) {
baseline: null,
writeBaseline: null,
help: false,
version: false,
}

for (let i = 0; i < argv.length; i++) {
Expand All @@ -52,6 +54,10 @@ function parseArguments(argv) {
case "--help":
options.help = true
break
case "-v":
case "--version":
options.version = true
break
case "--json":
options.json = true
break
Expand Down Expand Up @@ -136,6 +142,12 @@ function main() {
return 0
}

if (options.version) {
const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"))
process.stdout.write(`${version}\n`)
return 0
}

const config = options.config ? JSON.parse(readFileSync(options.config, "utf8")) : {}

const enums = []
Expand Down
13 changes: 12 additions & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from "node:test"
import assert from "node:assert/strict"
import { mkdtempSync, rmSync, writeFileSync } from "node:fs"
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { spawnSync } from "node:child_process"
Expand All @@ -24,3 +24,14 @@ test("CLI scans qualified attributes and detects collisions", () => {
rmSync(dir, { recursive: true, force: true })
}
})

for (const flag of ["--version", "-v"]) {
test(`CLI ${flag} prints the package version without a scan path`, () => {
const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"))
const cwd = fileURLToPath(new URL("../examples/broken/", import.meta.url))
const result = spawnSync(process.execPath, [cli, flag], { cwd, encoding: "utf8" })
assert.equal(result.status, 0, result.stderr)
assert.equal(result.stdout, `${version}\n`)
assert.equal(result.stderr, "")
})
}