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: 7 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
import { pathToFileURL } from "node:url";
import { generatePrPack } from "./generate.js";

interface CliOptions {
Expand Down Expand Up @@ -103,7 +104,9 @@ export async function run(argv = process.argv.slice(2)): Promise<void> {
}
}

run().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : String(error));
process.exitCode = 1;
});
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
run().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : String(error));
process.exitCode = 1;
});
}
9 changes: 9 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { access, rm } from "node:fs/promises";
import { promisify } from "node:util";
import { describe, it } from "node:test";

Expand All @@ -17,4 +18,12 @@ void describe("cli", () => {
assert.equal(parsed.pack.title, "Add deterministic PR pack generation");
assert.match(parsed.pack.prBody, /Reviewer Checklist/);
});

void it("can be imported without running the CLI", async () => {
await rm("PR_PACK.md", { force: true });
const { stdout } = await execFileAsync(process.execPath, ["--input-type=module", "--eval", "import('./dist/src/cli.js').then((mod) => console.log(typeof mod.run))"]);

assert.equal(stdout.trim(), "function");
await assert.rejects(() => access("PR_PACK.md"));
});
});