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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ preserve that status without reporting it. Usage failures throw for the boundary
## Mutations

Call `mutating(command)` for `send`, `submit`, `set`, and `mark-read` commands. The marker is
included by `commandsJson`. Command actions call `confirm` before making an upstream request:
included by `commandsJson`; a CLI that describes its own command tree reads it with
`isMutating(command)` instead of guessing from command names. Command actions call `confirm` before making an upstream request:

```ts
const shouldApply = await confirm(
Expand Down
2 changes: 2 additions & 0 deletions dist/program.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ export declare function createProgram(meta: {
description: string;
}): Command;
export declare function mutating(command: Command): Command;
/** Whether `mutating` marked this command, for a CLI that describes its own command tree. */
export declare function isMutating(command: Command): boolean;
export declare function commandsJson(program: Command): ProgramDescription;
4 changes: 4 additions & 0 deletions dist/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export function mutating(command) {
mutations.add(command);
return command;
}
/** Whether `mutating` marked this command, for a CLI that describes its own command tree. */
export function isMutating(command) {
return mutations.has(command);
}
export function commandsJson(program) {
return {
name: program.name(),
Expand Down
5 changes: 5 additions & 0 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export function mutating(command: Command): Command {
return command;
}

/** Whether `mutating` marked this command, for a CLI that describes its own command tree. */
export function isMutating(command: Command): boolean {
return mutations.has(command);
}

export function commandsJson(program: Command): ProgramDescription {
return {
name: program.name(),
Expand Down
10 changes: 9 additions & 1 deletion test/program.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";

import { commandsJson, createProgram, mutating } from "../dist/index.js";
import { commandsJson, createProgram, isMutating, mutating } from "../dist/index.js";

test("commandsJson describes commands and marks mutations", () => {
const program = createProgram({ name: "demo", version: "1.2.3", description: "Demo CLI" });
Expand Down Expand Up @@ -136,3 +136,11 @@ test("commander usage failures still throw once", async () => {
return true;
});
});

test("isMutating reads the mark that mutating sets", () => {
const program = createProgram({ name: "demo", version: "1.0.0", description: "Demo" });
const reply = mutating(program.command("reply"));
const show = program.command("show");
assert.equal(isMutating(reply), true);
assert.equal(isMutating(show), false);
});
Loading