better-commits 1.19.0 → 1.20.0-cli-flags

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/.better-commits.json +4 -0
  2. package/.github/workflows/test.yml +27 -0
  3. package/.opencode/package-lock.json +115 -0
  4. package/.opencode/plans/cli-args.md +182 -0
  5. package/.svelte-kit/ambient.d.ts +289 -0
  6. package/.svelte-kit/generated/client/app.js +28 -0
  7. package/.svelte-kit/generated/client/matchers.js +1 -0
  8. package/.svelte-kit/generated/client/nodes/0.js +1 -0
  9. package/.svelte-kit/generated/client/nodes/1.js +1 -0
  10. package/.svelte-kit/tsconfig.json +49 -0
  11. package/0001-feat-branch-124-update-worktrees-feature.patch +316 -0
  12. package/dist/branch.js +27 -1
  13. package/dist/chunk-OFJCRS3N.js +4 -0
  14. package/dist/chunk-SIF4LZUS.js +1 -0
  15. package/dist/index.js +44 -19
  16. package/dist/init.js +1 -1
  17. package/docs/ai-skills.yaml +48 -0
  18. package/docs/clack.md +143 -0
  19. package/docs/valibot.md +228 -0
  20. package/package.json +12 -9
  21. package/readme.md +18 -2
  22. package/src/args.test.ts +102 -0
  23. package/src/args.ts +101 -7
  24. package/src/branch-args.test.ts +72 -0
  25. package/src/branch-args.ts +106 -0
  26. package/src/branch-help.ts +114 -0
  27. package/src/branch.ts +67 -238
  28. package/src/help.ts +131 -0
  29. package/src/index.test.ts +7 -0
  30. package/src/index.ts +73 -492
  31. package/src/prompts/branch-checkout.prompt.ts +36 -0
  32. package/src/prompts/branch-confirm.prompt.ts +134 -0
  33. package/src/prompts/branch-description.prompt.ts +37 -0
  34. package/src/prompts/branch-runnable.ts +13 -0
  35. package/src/prompts/branch-ticket.prompt.ts +41 -0
  36. package/src/prompts/branch-type.prompt.ts +43 -0
  37. package/src/prompts/branch-user.prompt.ts +50 -0
  38. package/src/prompts/branch-version.prompt.ts +41 -0
  39. package/src/prompts/commit-body.prompt.ts +57 -0
  40. package/src/prompts/commit-confirm.prompt.ts +119 -0
  41. package/src/prompts/commit-footer.prompt.ts +195 -0
  42. package/src/prompts/commit-scope.prompt.ts +73 -0
  43. package/src/prompts/commit-status.prompt.ts +75 -0
  44. package/src/prompts/commit-ticket.prompt.ts +82 -0
  45. package/src/prompts/commit-title.prompt.ts +98 -0
  46. package/src/prompts/commit-type.prompt.ts +93 -0
  47. package/src/prompts/runnable.ts +13 -0
  48. package/src/utils/build-branch.test.ts +141 -0
  49. package/src/utils/build-branch.ts +46 -0
  50. package/src/utils/build-commit-string.test.ts +253 -0
  51. package/src/utils/build-commit-string.ts +158 -0
  52. package/src/utils/commit-title-size.ts +24 -0
  53. package/src/utils/infer.test.ts +83 -0
  54. package/src/utils/infer.ts +114 -0
  55. package/src/utils/messages.ts +25 -0
  56. package/src/utils/no-interactive-branch-validation.test.ts +170 -0
  57. package/src/utils/no-interactive-validation.test.ts +174 -0
  58. package/src/utils/no-interactive-validation.ts +190 -0
  59. package/src/utils.ts +59 -66
  60. package/src/valibot-consts.ts +2 -2
  61. package/src/valibot-state.test.ts +48 -0
  62. package/src/valibot-state.ts +133 -130
  63. package/tsconfig.json +3 -2
  64. package/vitest.config.ts +8 -0
  65. package/dist/chunk-K2RPF2JY.js +0 -4
package/src/args.ts CHANGED
@@ -1,12 +1,106 @@
1
+ import { parse } from "@bomb.sh/args";
2
+ import { InferOutput } from "valibot";
3
+ import { CommitState } from "./valibot-state";
4
+
5
+ type CommitStateRuntime = InferOutput<typeof CommitState>;
6
+
7
+ type ParsedRuntimeFlags = {
8
+ help: boolean;
9
+ version: boolean;
10
+ git_args: string;
11
+ no_interactive: boolean;
12
+ dry_run: boolean;
13
+ commit_state: Partial<CommitStateRuntime>;
14
+ };
15
+
16
+ export const COMMIT_OPTIONS = [
17
+ "type",
18
+ "scope",
19
+ "title",
20
+ "body",
21
+ "closes",
22
+ "ticket",
23
+ "trailer",
24
+ "deprecates",
25
+ "breaking-title",
26
+ "breaking-body",
27
+ "deprecates-title",
28
+ "deprecates-body",
29
+ "custom-footer",
30
+ ] as const;
31
+
32
+ export const GIT_OPTIONS = ["git-dir", "work-tree"] as const;
33
+
34
+ export const BOOLEAN_FLAGS = [
35
+ "interactive",
36
+ "dry-run",
37
+ "help",
38
+ "version",
39
+ ] as const;
40
+
1
41
  class Flags {
2
- #git_args: string = "";
3
- constructor() {}
4
- get git_args() {
5
- return this.#git_args;
42
+ #runtime: ParsedRuntimeFlags;
43
+
44
+ constructor(runtime: ParsedRuntimeFlags) {
45
+ this.#runtime = runtime;
46
+ }
47
+
48
+ get git_args(): string {
49
+ return this.#runtime.git_args;
50
+ }
51
+
52
+ get interactive(): boolean {
53
+ return !this.#runtime.no_interactive;
54
+ }
55
+
56
+ get dry_run(): boolean {
57
+ return this.#runtime.dry_run;
58
+ }
59
+
60
+ get help(): boolean {
61
+ return this.#runtime.help;
6
62
  }
7
- set git_args(value: string) {
8
- this.#git_args = value;
63
+
64
+ get version(): boolean {
65
+ return this.#runtime.version;
66
+ }
67
+
68
+ get commit_state(): Partial<CommitStateRuntime> {
69
+ return this.#runtime.commit_state;
9
70
  }
10
71
  }
11
72
 
12
- export const flags = new Flags();
73
+ export const flags = new Flags(parse_runtime_flags(process.argv.slice(2)));
74
+
75
+ export function parse_runtime_flags(argv: string[]): ParsedRuntimeFlags {
76
+ const parsed = parse(argv, {
77
+ alias: { h: "help", v: "version" },
78
+ boolean: BOOLEAN_FLAGS,
79
+ string: [...COMMIT_OPTIONS, ...GIT_OPTIONS],
80
+ });
81
+
82
+ const commit_state: Partial<CommitStateRuntime> = {};
83
+ COMMIT_OPTIONS.forEach((value) => {
84
+ const cli_value = parsed[value];
85
+ if (cli_value) {
86
+ const str = value.replace("-", "_") as keyof CommitStateRuntime;
87
+ commit_state[str] = cli_value;
88
+ }
89
+ });
90
+
91
+ return {
92
+ help: parsed["help"] === true,
93
+ version: parsed["version"] === true,
94
+ git_args: get_git_args(parsed["git-dir"], parsed["work-tree"]),
95
+ no_interactive: parsed.interactive === false,
96
+ dry_run: parsed["dry-run"] === true,
97
+ commit_state,
98
+ };
99
+ }
100
+
101
+ function get_git_args(
102
+ git_dir: string | undefined,
103
+ work_tree: string | undefined,
104
+ ): string {
105
+ return `${git_dir ? `--git-dir=${git_dir}` : ""} ${work_tree ? `--work-tree=${work_tree}` : ""}`.trim();
106
+ }
@@ -0,0 +1,72 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { parse_branch_runtime_flags } from "./branch-args";
3
+
4
+ describe("parse_branch_runtime_flags", () => {
5
+ it("maps branch flags into branch_state keys", () => {
6
+ const parsed = parse_branch_runtime_flags([
7
+ "--user",
8
+ "erik",
9
+ "--type",
10
+ "feat",
11
+ "--ticket",
12
+ "ABC-123",
13
+ "--description",
14
+ "add-parser",
15
+ "--branch-version",
16
+ "1.2.0",
17
+ ]);
18
+
19
+ expect(parsed.branch_state).toEqual({
20
+ user: "erik",
21
+ type: "feat",
22
+ ticket: "ABC-123",
23
+ description: "add-parser",
24
+ version: "1.2.0",
25
+ });
26
+ });
27
+
28
+ it("maps --checkout into checkout", () => {
29
+ const parsed = parse_branch_runtime_flags(["--checkout", "worktree"]);
30
+
31
+ expect(parsed.branch_state).toEqual({
32
+ checkout: "worktree",
33
+ });
34
+ });
35
+
36
+ it("does not set checkout when --checkout is omitted", () => {
37
+ const parsed = parse_branch_runtime_flags([
38
+ "--type",
39
+ "feat",
40
+ "--description",
41
+ "add-parser",
42
+ ]);
43
+
44
+ expect(parsed.branch_state).toEqual({
45
+ type: "feat",
46
+ description: "add-parser",
47
+ });
48
+ });
49
+
50
+ it("honors interactive flag semantics", () => {
51
+ const default_flags = parse_branch_runtime_flags([]);
52
+ const explicit_interactive = parse_branch_runtime_flags(["--interactive"]);
53
+ const no_interactive = parse_branch_runtime_flags(["--no-interactive"]);
54
+
55
+ expect(default_flags.no_interactive).toBe(false);
56
+ expect(explicit_interactive.no_interactive).toBe(false);
57
+ expect(no_interactive.no_interactive).toBe(true);
58
+ });
59
+
60
+ it("parses git-dir and work-tree into git_args", () => {
61
+ const parsed = parse_branch_runtime_flags([
62
+ "--git-dir",
63
+ "/tmp/repo/.git",
64
+ "--work-tree",
65
+ "/tmp/repo",
66
+ ]);
67
+
68
+ expect(parsed.git_args).toBe(
69
+ "--git-dir=/tmp/repo/.git --work-tree=/tmp/repo",
70
+ );
71
+ });
72
+ });
@@ -0,0 +1,106 @@
1
+ import { parse } from "@bomb.sh/args";
2
+ import { InferOutput } from "valibot";
3
+ import { BranchState } from "./valibot-state";
4
+
5
+ type BranchStateRuntime = InferOutput<typeof BranchState>;
6
+
7
+ type ParsedRuntimeFlags = {
8
+ help: boolean;
9
+ version: boolean;
10
+ git_args: string;
11
+ no_interactive: boolean;
12
+ dry_run: boolean;
13
+ branch_state: Partial<BranchStateRuntime>;
14
+ };
15
+
16
+ const BRANCH_OPTIONS = [
17
+ "user",
18
+ "type",
19
+ "description",
20
+ "ticket",
21
+ "branch-version",
22
+ "checkout",
23
+ ] as const;
24
+
25
+ export const GIT_OPTIONS = ["git-dir", "work-tree"] as const;
26
+
27
+ export const BOOLEAN_FLAGS = [
28
+ "interactive",
29
+ "dry-run",
30
+ "help",
31
+ "version",
32
+ ] as const;
33
+
34
+ class BranchFlags {
35
+ #runtime: ParsedRuntimeFlags;
36
+
37
+ constructor(runtime: ParsedRuntimeFlags) {
38
+ this.#runtime = runtime;
39
+ }
40
+
41
+ get interactive(): boolean {
42
+ return !this.#runtime.no_interactive;
43
+ }
44
+
45
+ get dry_run(): boolean {
46
+ return this.#runtime.dry_run;
47
+ }
48
+
49
+ get help(): boolean {
50
+ return this.#runtime.help;
51
+ }
52
+
53
+ get version(): boolean {
54
+ return this.#runtime.version;
55
+ }
56
+
57
+ get git_args(): string {
58
+ return this.#runtime.git_args;
59
+ }
60
+
61
+ get branch_state(): Partial<BranchStateRuntime> {
62
+ return this.#runtime.branch_state;
63
+ }
64
+ }
65
+
66
+ export const branch_flags = new BranchFlags(
67
+ parse_branch_runtime_flags(process.argv.slice(2)),
68
+ );
69
+
70
+ export function parse_branch_runtime_flags(argv: string[]): ParsedRuntimeFlags {
71
+ const parsed = parse(argv, {
72
+ alias: { h: "help", v: "version" },
73
+ boolean: BOOLEAN_FLAGS,
74
+ string: [...BRANCH_OPTIONS, ...GIT_OPTIONS],
75
+ });
76
+
77
+ const branch_state: Partial<BranchStateRuntime> = {};
78
+ BRANCH_OPTIONS.forEach((value) => {
79
+ const cli_value = parsed[value];
80
+ if (cli_value) {
81
+ const str = (value === "branch-version"
82
+ ? "version"
83
+ : value.replace("-", "_")) as keyof BranchStateRuntime;
84
+ if (str === "checkout")
85
+ branch_state[str] =
86
+ (cli_value as "worktree" | "branch" | undefined) ?? "branch";
87
+ else branch_state[str] = cli_value;
88
+ }
89
+ });
90
+
91
+ return {
92
+ help: parsed["help"] === true,
93
+ version: parsed["version"] === true,
94
+ git_args: get_git_args(parsed["git-dir"], parsed["work-tree"]),
95
+ no_interactive: parsed["interactive"] === false,
96
+ dry_run: parsed["dry-run"] === true,
97
+ branch_state: branch_state,
98
+ };
99
+ }
100
+
101
+ function get_git_args(
102
+ git_dir: string | undefined,
103
+ work_tree: string | undefined,
104
+ ): string {
105
+ return `${git_dir ? `--git-dir=${git_dir}` : ""} ${work_tree ? `--work-tree=${work_tree}` : ""}`.trim();
106
+ }
@@ -0,0 +1,114 @@
1
+ import { execSync } from "child_process";
2
+ import { InferOutput } from "valibot";
3
+ import { branch_flags } from "./branch-args";
4
+ import { get_package_version } from "./utils";
5
+ import { infer_ticket_from_git, infer_type_from_git } from "./utils/infer";
6
+ import { Config } from "./valibot-state";
7
+ import color from "picocolors";
8
+
9
+ const CLI_FLAG_DEFINITIONS: Record<string, string> = {
10
+ "--interactive": "Run in interactive prompt mode (default behavior).",
11
+ "--dry-run": "Print branch commands without creating a branch or worktree.",
12
+ "--help": "Show help information and exit.",
13
+ };
14
+
15
+ const BRANCH_FLAG_DEFINITIONS: Record<string, string> = {
16
+ "--user": "Set branch username segment.",
17
+ "--type": "Set branch type (for example feat, fix, docs).",
18
+ "--description": "Set branch description segment.",
19
+ "--ticket": "Set branch ticket/issue segment.",
20
+ "--branch-version": "Set branch version segment.",
21
+ "--checkout": "Choose branch or worktree checkout mode.",
22
+ };
23
+
24
+ const GIT_FLAG_DEFINITIONS: Record<string, string> = {
25
+ "--git-dir": "Set the path to the .git directory.",
26
+ "--work-tree": "Set the path to the working tree root.",
27
+ };
28
+
29
+ function to_definition_lines(definitions: Record<string, string>): string {
30
+ const description_column = 26;
31
+ const minimum_spacing = 2;
32
+ const indent = " ";
33
+
34
+ return Object.entries(definitions)
35
+ .map(([name, description]) => {
36
+ const spaces = Math.max(
37
+ minimum_spacing,
38
+ description_column - name.length,
39
+ );
40
+ return `${indent}${name}${" ".repeat(spaces)}${description}`;
41
+ })
42
+ .join("\n");
43
+ }
44
+
45
+ export function print_help_text(
46
+ config: InferOutput<typeof Config>,
47
+ config_source: "repository" | "global" | "none",
48
+ ) {
49
+ const version = get_package_version();
50
+
51
+ let branch = "(none)";
52
+ try {
53
+ branch =
54
+ execSync(`git ${branch_flags.git_args} branch --show-current`, {
55
+ stdio: "pipe",
56
+ })
57
+ .toString()
58
+ .trim() || "(none)";
59
+ } catch {
60
+ // noop
61
+ }
62
+
63
+ const inferred_type =
64
+ infer_type_from_git(config.commit_type.options, branch_flags.git_args) ||
65
+ "Unknown";
66
+ const inferred_ticket = config.check_ticket.infer_ticket
67
+ ? infer_ticket_from_git(
68
+ {
69
+ append_hashtag: config.check_ticket.append_hashtag,
70
+ prepend_hashtag: config.check_ticket.prepend_hashtag,
71
+ },
72
+ branch_flags.git_args,
73
+ ) || "Unknown"
74
+ : "Infer Disabled";
75
+
76
+ const types = config.commit_type.options
77
+ .map((option) => option.value)
78
+ .join(", ")
79
+ .trim();
80
+ const scopes = config.commit_scope.options
81
+ .map((option) => option.value)
82
+ .join(", ")
83
+ .trim();
84
+ const cli_flags = to_definition_lines(CLI_FLAG_DEFINITIONS);
85
+ const git_flags = to_definition_lines(GIT_FLAG_DEFINITIONS);
86
+ const branch_flags_help = to_definition_lines(BRANCH_FLAG_DEFINITIONS);
87
+
88
+ console.log(`
89
+ ${color.green(" better-branch")} ${color.gray("v" + version)}
90
+
91
+ ${color.gray("BRANCH")}
92
+ ${branch}
93
+ ${color.gray("Type")} ${color.blue(inferred_type)} ${color.gray("·")} ${color.gray("Ticket")} ${color.magenta(inferred_ticket)}
94
+
95
+ ${color.gray("CONFIGURATION")}
96
+ ${config_source}
97
+
98
+ ${color.gray("Types")}
99
+ ${types}
100
+
101
+ ${color.gray("Scopes")}
102
+ ${scopes}
103
+
104
+ ${color.gray("CLI FLAGS")}
105
+ ${cli_flags}
106
+
107
+ ${color.gray("Branch Flags")}
108
+ ${branch_flags_help}
109
+
110
+ ${color.gray("Git Flags (Advanced)")}
111
+ ${git_flags}
112
+
113
+ `);
114
+ }