better-commits 1.15.3 → 1.15.4

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.
package/dist/branch.js CHANGED
@@ -1,203 +1,2 @@
1
1
  #! /usr/bin/env node
2
- import {
3
- BRANCH_ACTION_OPTIONS,
4
- BranchState,
5
- CACHE_PROMPT,
6
- OPTIONAL_PROMPT,
7
- load_setup
8
- } from "./chunk-ZHN4TXXW.js";
9
-
10
- // src/branch.ts
11
- import * as p from "@clack/prompts";
12
- import { execSync } from "child_process";
13
- import Configstore from "configstore";
14
- import color from "picocolors";
15
- import { chdir } from "process";
16
- import { parse } from "valibot";
17
- main(load_setup(" better-branch "));
18
- async function main(config) {
19
- const branch_state = parse(BranchState, {});
20
- let checkout_type = "branch";
21
- if (config.enable_worktrees) {
22
- const branch_or_worktree = await p.select({
23
- message: `Checkout a branch or create a worktree?`,
24
- initialValue: config.branch_action_default,
25
- options: BRANCH_ACTION_OPTIONS
26
- });
27
- if (p.isCancel(branch_or_worktree))
28
- process.exit();
29
- checkout_type = branch_or_worktree;
30
- }
31
- if (config.branch_user.enable) {
32
- const cache_user_name = get_user_from_cache();
33
- const user_name_required = config.branch_user.required;
34
- const user_name = await p.text({
35
- message: `Type your git username ${user_name_required ? "" : OPTIONAL_PROMPT} ${CACHE_PROMPT}`.trim(),
36
- placeholder: "",
37
- initialValue: cache_user_name,
38
- validate: (val) => {
39
- if (user_name_required && !val)
40
- return "Please enter a username";
41
- }
42
- });
43
- if (p.isCancel(user_name))
44
- process.exit(0);
45
- branch_state.user = user_name?.replace(/\s+/g, "-")?.toLowerCase() ?? "";
46
- set_user_cache(branch_state.user);
47
- }
48
- if (config.branch_type.enable) {
49
- let initial_value = config.commit_type.initial_value;
50
- const commit_type = await p.select({
51
- message: `Select a branch type`,
52
- initialValue: initial_value,
53
- options: config.commit_type.options
54
- });
55
- if (p.isCancel(commit_type))
56
- process.exit(0);
57
- branch_state.type = commit_type;
58
- }
59
- if (config.branch_ticket.enable) {
60
- const ticked_required = config.branch_ticket.required;
61
- const ticket = await p.text({
62
- message: `Type ticket / issue number ${ticked_required ? "" : OPTIONAL_PROMPT}`.trim(),
63
- placeholder: "",
64
- validate: (val) => {
65
- if (ticked_required && !val)
66
- return "Please enter a ticket / issue";
67
- }
68
- });
69
- if (p.isCancel(ticket))
70
- process.exit(0);
71
- branch_state.ticket = ticket;
72
- }
73
- if (config.branch_version.enable) {
74
- const version_required = config.branch_version.required;
75
- const version = await p.text({
76
- message: `Type version number ${version_required ? "" : OPTIONAL_PROMPT}`.trim(),
77
- placeholder: "",
78
- validate: (val) => {
79
- if (version_required && !val)
80
- return "Please enter a version";
81
- }
82
- });
83
- if (p.isCancel(version))
84
- process.exit(0);
85
- branch_state.version = version;
86
- }
87
- const description_max_length = config.branch_description.max_length;
88
- const description = await p.text({
89
- message: "Type a short description",
90
- placeholder: "",
91
- validate: (value) => {
92
- if (!value)
93
- return "Please enter a description";
94
- if (value.length > description_max_length)
95
- return `Exceeded max length. Description max [${description_max_length}]`;
96
- }
97
- });
98
- if (p.isCancel(description))
99
- process.exit(0);
100
- branch_state.description = description?.replace(/\s+/g, "-")?.toLowerCase() ?? "";
101
- const pre_commands = checkout_type === "worktree" ? config.worktree_pre_commands : config.branch_pre_commands;
102
- pre_commands.forEach((command) => {
103
- try {
104
- execSync(command, { stdio: "inherit" });
105
- } catch (err) {
106
- p.log.error("Something went wrong when executing pre-commands: " + err);
107
- process.exit(0);
108
- }
109
- });
110
- const branch_name = build_branch(branch_state, config);
111
- const branch_flag = verify_branch_name(branch_name);
112
- if (checkout_type === "branch") {
113
- try {
114
- execSync(`git checkout ${branch_flag} ${branch_name}`, {
115
- stdio: "inherit"
116
- });
117
- p.log.info(
118
- `Switched to a new branch '${color.bgGreen(
119
- " " + color.black(branch_name) + " "
120
- )}'`
121
- );
122
- } catch (err) {
123
- process.exit(0);
124
- }
125
- } else {
126
- try {
127
- const ticket = branch_state.ticket ? `${branch_state.ticket}-` : "";
128
- const worktree_name = `${ticket}${branch_state.description}`;
129
- execSync(
130
- `git worktree add ${worktree_name} ${branch_flag} ${branch_name}`,
131
- {
132
- stdio: "inherit"
133
- }
134
- );
135
- p.log.info(
136
- `Created a new worktree ${color.bgGreen(
137
- +" " + color.black(worktree_name) + " "
138
- )}, checked out branch ${color.bgGreen(
139
- " " + color.black(branch_name) + " "
140
- )}`
141
- );
142
- p.log.info(
143
- color.bgMagenta(color.black(` cd ${worktree_name} `)) + " to navigate to your new worktree"
144
- );
145
- chdir(worktree_name);
146
- } catch (err) {
147
- process.exit(0);
148
- }
149
- }
150
- const post_commands = checkout_type === "worktree" ? config.worktree_post_commands : config.branch_post_commands;
151
- post_commands.forEach((command) => {
152
- try {
153
- execSync(command, { stdio: "inherit" });
154
- } catch (err) {
155
- p.log.error("Something went wrong when executing post-commands: " + err);
156
- process.exit(0);
157
- }
158
- });
159
- }
160
- function build_branch(branch, config) {
161
- let res = "";
162
- config.branch_order.forEach((b) => {
163
- const config_key = `branch_${b}`;
164
- if (branch[b])
165
- res += branch[b] + config[config_key].separator;
166
- });
167
- if (res.endsWith("-") || res.endsWith("/") || res.endsWith("_")) {
168
- return res.slice(0, -1).trim();
169
- }
170
- return res.trim();
171
- }
172
- function get_user_from_cache() {
173
- try {
174
- const config_store = new Configstore("better-commits");
175
- return config_store.get("username") ?? "";
176
- } catch (err) {
177
- p.log.warn(
178
- 'There was an issue accessing username from cache. Check that the folder "~/.config" exists'
179
- );
180
- }
181
- return "";
182
- }
183
- function verify_branch_name(branch_name) {
184
- let branch_flag = "";
185
- try {
186
- execSync(`git show-ref ${branch_name}`, { encoding: "utf-8" });
187
- p.log.warning(
188
- color.yellow(
189
- `${branch_name} already exists! Checking out existing branch.`
190
- )
191
- );
192
- } catch (err) {
193
- branch_flag = "-b";
194
- }
195
- return branch_flag;
196
- }
197
- function set_user_cache(val) {
198
- try {
199
- const config_store = new Configstore("better-commits");
200
- config_store.set("username", val);
201
- } catch (err) {
202
- }
203
- }
2
+ import{d as u,g as h,h as d,p as f,q as b}from"./chunk-KFU55MTN.js";import*as t from"@clack/prompts";import{execSync as m}from"child_process";import g from"configstore";import a from"picocolors";import{chdir as k}from"process";import{parse as y}from"valibot";C(b(" better-branch "));async function C(e){let i=y(u,{}),o="branch";if(e.enable_worktrees){let r=await t.select({message:"Checkout a branch or create a worktree?",initialValue:e.branch_action_default,options:f});t.isCancel(r)&&process.exit(),o=r}if(e.branch_user.enable){let r=$(),n=e.branch_user.required,c=await t.text({message:`Type your git username ${n?"":h} ${d}`.trim(),placeholder:"",initialValue:r,validate:w=>{if(n&&!w)return"Please enter a username"}});t.isCancel(c)&&process.exit(0),i.user=c?.replace(/\s+/g,"-")?.toLowerCase()??"",v(i.user)}if(e.branch_type.enable){let r=e.commit_type.initial_value,n=await t.select({message:"Select a branch type",initialValue:r,options:e.commit_type.options});t.isCancel(n)&&process.exit(0),i.type=n}if(e.branch_ticket.enable){let r=e.branch_ticket.required,n=await t.text({message:`Type ticket / issue number ${r?"":h}`.trim(),placeholder:"",validate:c=>{if(r&&!c)return"Please enter a ticket / issue"}});t.isCancel(n)&&process.exit(0),i.ticket=n}if(e.branch_version.enable){let r=e.branch_version.required,n=await t.text({message:`Type version number ${r?"":h}`.trim(),placeholder:"",validate:c=>{if(r&&!c)return"Please enter a version"}});t.isCancel(n)&&process.exit(0),i.version=n}let s=e.branch_description.max_length,_=await t.text({message:"Type a short description",placeholder:"",validate:r=>{if(!r)return"Please enter a description";if(r.length>s)return`Exceeded max length. Description max [${s}]`}});t.isCancel(_)&&process.exit(0),i.description=_?.replace(/\s+/g,"-")?.toLowerCase()??"",(o==="worktree"?e.worktree_pre_commands:e.branch_pre_commands).forEach(r=>{try{m(r,{stdio:"inherit"})}catch(n){t.log.error("Something went wrong when executing pre-commands: "+n),process.exit(0)}});let p=x(i,e),l=O(p);if(o==="branch")try{m(`git checkout ${l} ${p}`,{stdio:"inherit"}),t.log.info(`Switched to a new branch '${a.bgGreen(" "+a.black(p)+" ")}'`)}catch{process.exit(0)}else try{let n=`${i.ticket?`${i.ticket}-`:""}${i.description}`;m(`git worktree add ${n} ${l} ${p}`,{stdio:"inherit"}),t.log.info(`Created a new worktree ${a.bgGreen(+" "+a.black(n)+" ")}, checked out branch ${a.bgGreen(" "+a.black(p)+" ")}`),t.log.info(a.bgMagenta(a.black(` cd ${n} `))+" to navigate to your new worktree"),k(n)}catch{process.exit(0)}(o==="worktree"?e.worktree_post_commands:e.branch_post_commands).forEach(r=>{try{m(r,{stdio:"inherit"})}catch(n){t.log.error("Something went wrong when executing post-commands: "+n),process.exit(0)}})}function x(e,i){let o="";return i.branch_order.forEach(s=>{let _=`branch_${s}`;e[s]&&(o+=e[s]+i[_].separator)}),o.endsWith("-")||o.endsWith("/")||o.endsWith("_")?o.slice(0,-1).trim():o.trim()}function $(){try{return new g("better-commits").get("username")??""}catch{t.log.warn('There was an issue accessing username from cache. Check that the folder "~/.config" exists')}return""}function O(e){let i="";try{m(`git show-ref ${e}`,{encoding:"utf-8"}),t.log.warning(a.yellow(`${e} already exists! Checking out existing branch.`))}catch{i="-b"}return i}function v(e){try{new g("better-commits").set("username",e)}catch{}}
package/dist/index.js CHANGED
@@ -1,436 +1,31 @@
1
1
  #! /usr/bin/env node
2
- import {
3
- COMMIT_FOOTER_OPTIONS,
4
- CUSTOM_SCOPE_KEY,
5
- CommitState,
6
- OPTIONAL_PROMPT,
7
- REGEX_SLASH_NUM,
8
- REGEX_SLASH_TAG,
9
- REGEX_SLASH_UND,
10
- REGEX_START_NUM,
11
- REGEX_START_TAG,
12
- REGEX_START_UND,
13
- SPACE_TO_SELECT,
14
- addNewLine,
15
- clean_commit_title,
16
- get_git_root,
17
- infer_type_from_branch,
18
- load_setup
19
- } from "./chunk-ZHN4TXXW.js";
2
+ import{a as w,c as S,f as b,g as f,i as T,j as A,k as O,l as R,m as v,n as P,o as G,q as N,r as D,s as W,t as y,u as j}from"./chunk-KFU55MTN.js";import*as r from"@clack/prompts";import a from"picocolors";import{execSync as C}from"child_process";import{chdir as V}from"process";import{parse as X}from"valibot";import{execSync as M}from"child_process";import*as g from"@clack/prompts";import x from"picocolors";var I=["M","T","R","D","A","C"];function $(){let e="";try{e=M("git status --porcelain",{stdio:"pipe"}).toString()}catch{return g.log.error(x.red("Failed to git status")),{index:[],work_tree:[]}}let i=e.split(`
3
+ `),o=[],m=[];return i.forEach(s=>{let t=s.trimEnd();if(!t)return;let n=t.substring(2).trim(),c=t.charAt(0).trim(),d=t.charAt(1).trim();(c==="?"||d==="?")&&o.push(n),I.includes(c)&&m.push(n),I.includes(d)&&o.push(n)}),{index:m,work_tree:o}}function L(e){let i=e.join(" ");if(i)try{M(`git add ${i}`,{stdio:"pipe"}).toString(),g.log.success(x.green("Changes successfully staged"))}catch{g.log.error(x.red("Failed to stage changes"))}}F(N());async function F(e){let i=X(S,{});if(V(W()),e.check_status){let{index:s,work_tree:t}=$();r.log.step(a.black(a.bgGreen(" Checking Git Status ")));let n=s.reduce((d,p,_)=>a.green(d+p+y(s,_)),"");if(r.log.success(`Changes to be committed:
4
+ `+n),t.length){let d=t.reduce((_,k,l)=>a.red(_+k+y(t,l)),"");r.log.error(`Changes not staged for commit:
5
+ `+d);let p=await r.multiselect({message:`Some files have not been staged, would you like to add them now? ${b}`,options:[{value:".",label:"."},...t.map(_=>({value:_,label:_}))],required:!1});r.isCancel(p)&&process.exit(0),L(p)}$().index.length||(r.log.error(a.red('no changes added to commit (use "git add" and/or "git commit -a")')),process.exit(0))}if(e.commit_type.enable){let s="Select a commit type",t=e.commit_type.initial_value;if(e.commit_type.infer_type_from_branch){let d=e.commit_type.options.map(_=>_.value),p=D(d);p&&(s=`Commit type inferred from branch ${a.dim("(confirm / edit)")}`,t=p)}let n=e.commit_type.options.reduce((d,p)=>({...d,[p.value]:{emoji:p.emoji??"",trailer:p.trailer??""}}),{}),c=await r.select({message:s,initialValue:t,options:e.commit_type.options});r.isCancel(c)&&process.exit(0),i.trailer=n[c].trailer,i.type=e.commit_type.append_emoji_to_commit?`${n[c].emoji} ${c}`.trim():c}if(e.commit_scope.enable){let s=await r.select({message:"Select a commit scope",initialValue:e.commit_scope.initial_value,options:e.commit_scope.options});r.isCancel(s)&&process.exit(0),s===w&&e.commit_scope.custom_scope&&(s=await r.text({message:"Write a custom scope",placeholder:""}),r.isCancel(s)&&process.exit(0)),i.scope=s}if(e.check_ticket.infer_ticket)try{let s=C("git branch --show-current",{stdio:"pipe"}).toString(),t=[s.match(O),s.match(R),s.match(T),s.match(v),s.match(A),s.match(P)].filter(n=>n!=null).map(n=>n&&n.length>=2?n[1]:"");t.length&&t[0]&&(i.ticket=e.check_ticket.append_hashtag||e.check_ticket.prepend_hashtag==="Prompt"?"#"+t[0]:t[0])}catch{}if(e.check_ticket.confirm_ticket){let s=await r.text({message:i.ticket?`Ticket / issue inferred from branch ${a.dim("(confirm / edit)")}`:`Add ticket / issue ${f}`,placeholder:"",initialValue:i.ticket});r.isCancel(s)&&process.exit(0),i.ticket=s??""}e.check_ticket.prepend_hashtag==="Always"&&i.ticket&&!i.ticket.startsWith("#")&&(i.ticket="#"+i.ticket);let o=await r.text({message:"Write a brief title describing the commit",placeholder:"",validate:s=>{if(!s)return"Please enter a title";let t=i.scope?i.scope.length+2:0,n=i.type.length,c=e.check_ticket.add_to_title?i.ticket.length:0;if(t+n+c+s.length>e.commit_title.max_size)return`Exceeded max length. Title max [${e.commit_title.max_size}]`}});if(r.isCancel(o)&&process.exit(0),i.title=j(o),e.commit_body.enable){let s=await r.text({message:`Write a detailed description of the changes ${f}`,placeholder:"",validate:t=>{if(e.commit_body.required&&!t)return"Please enter a description"}});r.isCancel(s)&&process.exit(0),i.body=s??""}if(e.commit_footer.enable){let s=await r.multiselect({message:`Select optional footers ${b}`,initialValues:e.commit_footer.initial_value,options:G,required:!1});if(r.isCancel(s)&&process.exit(0),s.includes("breaking-change")){let t=await r.text({message:"Breaking changes: Write a short title / summary",placeholder:"",validate:c=>{if(!c)return"Please enter a title / summary"}});r.isCancel(t)&&process.exit(0);let n=await r.text({message:`Breaking Changes: Write a description & migration instructions ${f}`,placeholder:""});r.isCancel(n)&&process.exit(0),i.breaking_title=t,i.breaking_body=n}if(s.includes("deprecated")){let t=await r.text({message:"Deprecated: Write a short title / summary",placeholder:"",validate:c=>{if(!c)return"Please enter a title / summary"}});r.isCancel(t)&&process.exit(0);let n=await r.text({message:`Deprecated: Write a description ${f}`,placeholder:""});r.isCancel(n)&&process.exit(0),i.deprecates_body=n,i.deprecates_title=t}if(s.includes("closes")&&(i.closes="Closes:"),s.includes("custom")){let t=await r.text({message:"Write a custom footer",placeholder:""});r.isCancel(t)&&process.exit(0),i.custom_footer=t}s.includes("trailer")||(i.trailer="")}if(e.confirm_with_editor){let s=e.overrides.shell?{shell:e.overrides.shell,stdio:"inherit"}:{stdio:"inherit"},t=i.trailer?`--trailer="${i.trailer}"`:"";C(`git commit -m "${E(i,e,!1,!0,!1)}" ${t} --edit`,s),process.exit(0)}let m=!0;r.note(E(i,e,!0,!1,!0),"Commit Preview"),e.confirm_commit&&(m=await r.confirm({message:"Confirm Commit?"}),r.isCancel(m)&&process.exit(0)),m||(r.log.info("Exiting without commit"),process.exit(0));try{let s=e.overrides.shell?{shell:e.overrides.shell}:{},t=i.trailer?`--trailer="${i.trailer}"`:"",n=C(`git commit -m "${E(i,e,!1,!0,!1)}" ${t}`,s).toString().trim();e.print_commit_output&&r.log.info(n)}catch(s){r.log.error("Something went wrong when committing: "+s)}}function E(e,i,o=!1,m=!1,s=!1){let t="";if(e.type&&(t+=o?a.blue(e.type):e.type),e.scope){let l=o?a.cyan(e.scope):e.scope;t+=`(${l})`}let n=e.ticket,c=i.check_ticket.surround;if(e.ticket&&c){let l=c.charAt(0),u=c.charAt(1);n=`${l}${e.ticket}${u}`}let d=i.check_ticket.title_position==="beginning";n&&i.check_ticket.add_to_title&&d&&(t=`${o?a.magenta(n):n} ${t}`);let p=i.check_ticket.title_position==="before-colon";if(n&&i.check_ticket.add_to_title&&p){let l=e.scope||e.type&&!i.check_ticket.surround?" ":"";t+=o?a.magenta(l+n):l+n}e.breaking_title&&i.breaking_change.add_exclamation_to_title&&(t+=o?a.red("!"):"!"),(e.scope||e.type||n&&p)&&(t+=": ");let _=i.check_ticket.title_position==="start",k=i.check_ticket.title_position==="end";if(n&&i.check_ticket.add_to_title&&_&&(t+=o?a.magenta(n)+" ":n+" "),e.title&&(t+=o?a.reset(e.title):e.title),n&&i.check_ticket.add_to_title&&k&&(t+=" "+(o?a.magenta(n):n)),e.body){let u=e.body.split("\\n").map(h=>o?a.reset(h.trim()):h.trim()).join(`
6
+ `);t+=o?`
20
7
 
21
- // src/index.ts
22
- import * as p2 from "@clack/prompts";
23
- import color2 from "picocolors";
24
- import { execSync as execSync2 } from "child_process";
25
- import { chdir } from "process";
26
- import { parse } from "valibot";
8
+ ${u}`:`
27
9
 
28
- // src/git.ts
29
- import { execSync } from "child_process";
30
- import * as p from "@clack/prompts";
31
- import color from "picocolors";
32
- var porcelain_states = ["M", "T", "R", "D", "A", "C"];
33
- function git_status() {
34
- let status = "";
35
- try {
36
- status = execSync("git status --porcelain", { stdio: "pipe" }).toString();
37
- } catch (err) {
38
- p.log.error(color.red("Failed to git status"));
39
- return { index: [], work_tree: [] };
40
- }
41
- const lines = status.split("\n");
42
- const work_tree = [];
43
- const index = [];
44
- lines.forEach((v) => {
45
- const line = v.trimEnd();
46
- if (!line)
47
- return;
48
- const path_plus_file = line.substring(2).trim();
49
- const first_char = line.charAt(0).trim();
50
- const second_char = line.charAt(1).trim();
51
- if (first_char === "?" || second_char === "?") {
52
- work_tree.push(path_plus_file);
53
- }
54
- if (porcelain_states.includes(first_char)) {
55
- index.push(path_plus_file);
56
- }
57
- if (porcelain_states.includes(second_char)) {
58
- work_tree.push(path_plus_file);
59
- }
60
- });
61
- return { index, work_tree };
62
- }
63
- function git_add(files) {
64
- const space_delimited_files = files.join(" ");
65
- if (space_delimited_files) {
66
- try {
67
- execSync(`git add ${space_delimited_files}`, {
68
- stdio: "pipe"
69
- }).toString();
70
- p.log.success(color.green("Changes successfully staged"));
71
- } catch (err) {
72
- p.log.error(color.red("Failed to stage changes"));
73
- }
74
- }
75
- }
10
+ ${u}`}if(e.breaking_title){let l=o?a.red(`BREAKING CHANGE: ${e.breaking_title}`):`BREAKING CHANGE: ${e.breaking_title}`;t+=`
76
11
 
77
- // src/index.ts
78
- main(load_setup());
79
- async function main(config) {
80
- let commit_state = parse(CommitState, {});
81
- chdir(get_git_root());
82
- if (config.check_status) {
83
- let { index, work_tree } = git_status();
84
- p2.log.step(color2.black(color2.bgGreen(" Checking Git Status ")));
85
- const staged_files = index.reduce(
86
- (acc, curr, i) => color2.green(acc + curr + addNewLine(index, i)),
87
- ""
88
- );
89
- p2.log.success("Changes to be committed:\n" + staged_files);
90
- if (work_tree.length) {
91
- const unstaged_files = work_tree.reduce(
92
- (acc, curr, i) => color2.red(acc + curr + addNewLine(work_tree, i)),
93
- ""
94
- );
95
- p2.log.error("Changes not staged for commit:\n" + unstaged_files);
96
- const selected_for_staging = await p2.multiselect({
97
- message: `Some files have not been staged, would you like to add them now? ${SPACE_TO_SELECT}`,
98
- options: [
99
- { value: ".", label: "." },
100
- ...work_tree.map((v) => ({ value: v, label: v }))
101
- ],
102
- required: false
103
- });
104
- if (p2.isCancel(selected_for_staging))
105
- process.exit(0);
106
- git_add(selected_for_staging);
107
- }
108
- let updated_status = git_status();
109
- if (!updated_status.index.length) {
110
- p2.log.error(
111
- color2.red(
112
- 'no changes added to commit (use "git add" and/or "git commit -a")'
113
- )
114
- );
115
- process.exit(0);
116
- }
117
- }
118
- if (config.commit_type.enable) {
119
- let message = "Select a commit type";
120
- let initial_value = config.commit_type.initial_value;
121
- if (config.commit_type.infer_type_from_branch) {
122
- const options = config.commit_type.options.map((o) => o.value);
123
- const type_from_branch = infer_type_from_branch(options);
124
- if (type_from_branch) {
125
- message = `Commit type inferred from branch ${color2.dim("(confirm / edit)")}`;
126
- initial_value = type_from_branch;
127
- }
128
- }
129
- const value_to_data = config.commit_type.options.reduce(
130
- (acc, curr) => ({
131
- ...acc,
132
- [curr.value]: {
133
- emoji: curr.emoji ?? "",
134
- trailer: curr.trailer ?? ""
135
- }
136
- }),
137
- {}
138
- );
139
- const commit_type = await p2.select({
140
- message,
141
- initialValue: initial_value,
142
- options: config.commit_type.options
143
- });
144
- if (p2.isCancel(commit_type))
145
- process.exit(0);
146
- commit_state.trailer = value_to_data[commit_type].trailer;
147
- commit_state.type = config.commit_type.append_emoji_to_commit ? `${value_to_data[commit_type].emoji} ${commit_type}`.trim() : commit_type;
148
- }
149
- if (config.commit_scope.enable) {
150
- let commit_scope = await p2.select({
151
- message: "Select a commit scope",
152
- initialValue: config.commit_scope.initial_value,
153
- options: config.commit_scope.options
154
- });
155
- if (p2.isCancel(commit_scope))
156
- process.exit(0);
157
- if (commit_scope === CUSTOM_SCOPE_KEY && config.commit_scope.custom_scope) {
158
- commit_scope = await p2.text({
159
- message: "Write a custom scope",
160
- placeholder: ""
161
- });
162
- if (p2.isCancel(commit_scope))
163
- process.exit(0);
164
- }
165
- commit_state.scope = commit_scope;
166
- }
167
- if (config.check_ticket.infer_ticket) {
168
- try {
169
- const branch = execSync2("git branch --show-current", {
170
- stdio: "pipe"
171
- }).toString();
172
- const found = [
173
- branch.match(REGEX_START_UND),
174
- branch.match(REGEX_SLASH_UND),
175
- branch.match(REGEX_SLASH_TAG),
176
- branch.match(REGEX_SLASH_NUM),
177
- branch.match(REGEX_START_TAG),
178
- branch.match(REGEX_START_NUM)
179
- ].filter((v) => v != null).map((v) => v && v.length >= 2 ? v[1] : "");
180
- if (found.length && found[0]) {
181
- commit_state.ticket = config.check_ticket.append_hashtag || config.check_ticket.prepend_hashtag === "Prompt" ? "#" + found[0] : found[0];
182
- }
183
- } catch (err) {
184
- }
185
- }
186
- if (config.check_ticket.confirm_ticket) {
187
- const user_commit_ticket = await p2.text({
188
- message: commit_state.ticket ? `Ticket / issue inferred from branch ${color2.dim("(confirm / edit)")}` : `Add ticket / issue ${OPTIONAL_PROMPT}`,
189
- placeholder: "",
190
- initialValue: commit_state.ticket
191
- });
192
- if (p2.isCancel(user_commit_ticket))
193
- process.exit(0);
194
- commit_state.ticket = user_commit_ticket ?? "";
195
- }
196
- if (config.check_ticket.prepend_hashtag === "Always" && commit_state.ticket && !commit_state.ticket.startsWith("#")) {
197
- commit_state.ticket = "#" + commit_state.ticket;
198
- }
199
- const commit_title = await p2.text({
200
- message: "Write a brief title describing the commit",
201
- placeholder: "",
202
- validate: (value) => {
203
- if (!value)
204
- return "Please enter a title";
205
- const commit_scope_size = commit_state.scope ? commit_state.scope.length + 2 : 0;
206
- const commit_type_size = commit_state.type.length;
207
- const commit_ticket_size = config.check_ticket.add_to_title ? commit_state.ticket.length : 0;
208
- if (commit_scope_size + commit_type_size + commit_ticket_size + value.length > config.commit_title.max_size)
209
- return `Exceeded max length. Title max [${config.commit_title.max_size}]`;
210
- }
211
- });
212
- if (p2.isCancel(commit_title))
213
- process.exit(0);
214
- commit_state.title = clean_commit_title(commit_title);
215
- if (config.commit_body.enable) {
216
- const commit_body = await p2.text({
217
- message: `Write a detailed description of the changes ${OPTIONAL_PROMPT}`,
218
- placeholder: "",
219
- validate: (val) => {
220
- if (config.commit_body.required && !val)
221
- return "Please enter a description";
222
- }
223
- });
224
- if (p2.isCancel(commit_body))
225
- process.exit(0);
226
- commit_state.body = commit_body ?? "";
227
- }
228
- if (config.commit_footer.enable) {
229
- const commit_footer = await p2.multiselect({
230
- message: `Select optional footers ${SPACE_TO_SELECT}`,
231
- initialValues: config.commit_footer.initial_value,
232
- options: COMMIT_FOOTER_OPTIONS,
233
- required: false
234
- });
235
- if (p2.isCancel(commit_footer))
236
- process.exit(0);
237
- if (commit_footer.includes("breaking-change")) {
238
- const breaking_changes_title = await p2.text({
239
- message: "Breaking changes: Write a short title / summary",
240
- placeholder: "",
241
- validate: (value) => {
242
- if (!value)
243
- return "Please enter a title / summary";
244
- }
245
- });
246
- if (p2.isCancel(breaking_changes_title))
247
- process.exit(0);
248
- const breaking_changes_body = await p2.text({
249
- message: `Breaking Changes: Write a description & migration instructions ${OPTIONAL_PROMPT}`,
250
- placeholder: ""
251
- });
252
- if (p2.isCancel(breaking_changes_body))
253
- process.exit(0);
254
- commit_state.breaking_title = breaking_changes_title;
255
- commit_state.breaking_body = breaking_changes_body;
256
- }
257
- if (commit_footer.includes("deprecated")) {
258
- const deprecated_title = await p2.text({
259
- message: "Deprecated: Write a short title / summary",
260
- placeholder: "",
261
- validate: (value) => {
262
- if (!value)
263
- return "Please enter a title / summary";
264
- }
265
- });
266
- if (p2.isCancel(deprecated_title))
267
- process.exit(0);
268
- const deprecated_body = await p2.text({
269
- message: `Deprecated: Write a description ${OPTIONAL_PROMPT}`,
270
- placeholder: ""
271
- });
272
- if (p2.isCancel(deprecated_body))
273
- process.exit(0);
274
- commit_state.deprecates_body = deprecated_body;
275
- commit_state.deprecates_title = deprecated_title;
276
- }
277
- if (commit_footer.includes("closes")) {
278
- commit_state.closes = "Closes:";
279
- }
280
- if (commit_footer.includes("custom")) {
281
- const custom_footer = await p2.text({
282
- message: "Write a custom footer",
283
- placeholder: ""
284
- });
285
- if (p2.isCancel(custom_footer))
286
- process.exit(0);
287
- commit_state.custom_footer = custom_footer;
288
- }
289
- if (!commit_footer.includes("trailer")) {
290
- commit_state.trailer = "";
291
- }
292
- }
293
- if (config.confirm_with_editor) {
294
- const options = config.overrides.shell ? { shell: config.overrides.shell, stdio: "inherit" } : { stdio: "inherit" };
295
- const trailer = commit_state.trailer ? `--trailer="${commit_state.trailer}"` : "";
296
- execSync2(
297
- `git commit -m "${build_commit_string(commit_state, config, false, true, false)}" ${trailer} --edit`,
298
- options
299
- );
300
- process.exit(0);
301
- }
302
- let continue_commit = true;
303
- p2.note(
304
- build_commit_string(commit_state, config, true, false, true),
305
- "Commit Preview"
306
- );
307
- if (config.confirm_commit) {
308
- continue_commit = await p2.confirm({
309
- message: "Confirm Commit?"
310
- });
311
- if (p2.isCancel(continue_commit))
312
- process.exit(0);
313
- }
314
- if (!continue_commit) {
315
- p2.log.info("Exiting without commit");
316
- process.exit(0);
317
- }
318
- try {
319
- const options = config.overrides.shell ? { shell: config.overrides.shell } : {};
320
- const trailer = commit_state.trailer ? `--trailer="${commit_state.trailer}"` : "";
321
- const output = execSync2(
322
- `git commit -m "${build_commit_string(commit_state, config, false, true, false)}" ${trailer}`,
323
- options
324
- ).toString().trim();
325
- if (config.print_commit_output)
326
- p2.log.info(output);
327
- } catch (err) {
328
- p2.log.error("Something went wrong when committing: " + err);
329
- }
330
- }
331
- function build_commit_string(commit_state, config, colorize = false, escape_quotes = false, include_trailer = false) {
332
- let commit_string = "";
333
- if (commit_state.type) {
334
- commit_string += colorize ? color2.blue(commit_state.type) : commit_state.type;
335
- }
336
- if (commit_state.scope) {
337
- const scope = colorize ? color2.cyan(commit_state.scope) : commit_state.scope;
338
- commit_string += `(${scope})`;
339
- }
340
- let title_ticket = commit_state.ticket;
341
- const surround = config.check_ticket.surround;
342
- if (commit_state.ticket && surround) {
343
- const open_token = surround.charAt(0);
344
- const close_token = surround.charAt(1);
345
- title_ticket = `${open_token}${commit_state.ticket}${close_token}`;
346
- }
347
- const position_beginning = config.check_ticket.title_position === "beginning";
348
- if (title_ticket && config.check_ticket.add_to_title && position_beginning) {
349
- commit_string = `${colorize ? color2.magenta(title_ticket) : title_ticket} ${commit_string}`;
350
- }
351
- const position_before_colon = config.check_ticket.title_position === "before-colon";
352
- if (title_ticket && config.check_ticket.add_to_title && position_before_colon) {
353
- const spacing = commit_state.scope || commit_state.type && !config.check_ticket.surround ? " " : "";
354
- commit_string += colorize ? color2.magenta(spacing + title_ticket) : spacing + title_ticket;
355
- }
356
- if (commit_state.breaking_title && config.breaking_change.add_exclamation_to_title) {
357
- commit_string += colorize ? color2.red("!") : "!";
358
- }
359
- if (commit_state.scope || commit_state.type || title_ticket && position_before_colon) {
360
- commit_string += ": ";
361
- }
362
- const position_start = config.check_ticket.title_position === "start";
363
- const position_end = config.check_ticket.title_position === "end";
364
- if (title_ticket && config.check_ticket.add_to_title && position_start) {
365
- commit_string += colorize ? color2.magenta(title_ticket) + " " : title_ticket + " ";
366
- }
367
- if (commit_state.title) {
368
- commit_string += colorize ? color2.reset(commit_state.title) : commit_state.title;
369
- }
370
- if (title_ticket && config.check_ticket.add_to_title && position_end) {
371
- commit_string += " " + (colorize ? color2.magenta(title_ticket) : title_ticket);
372
- }
373
- if (commit_state.body) {
374
- const temp = commit_state.body.split("\\n");
375
- const res = temp.map((v) => colorize ? color2.reset(v.trim()) : v.trim()).join("\n");
376
- commit_string += colorize ? `
12
+ ${l}`}if(e.breaking_body){let l=o?a.red(e.breaking_body):e.breaking_body;t+=`
377
13
 
378
- ${res}` : `
14
+ ${l}`}if(e.deprecates_title){let l=o?a.yellow(`DEPRECATED: ${e.deprecates_title}`):`DEPRECATED: ${e.deprecates_title}`;t+=`
379
15
 
380
- ${res}`;
381
- }
382
- if (commit_state.breaking_title) {
383
- const title = colorize ? color2.red(`BREAKING CHANGE: ${commit_state.breaking_title}`) : `BREAKING CHANGE: ${commit_state.breaking_title}`;
384
- commit_string += `
16
+ ${l}`}if(e.deprecates_body){let l=o?a.yellow(e.deprecates_body):e.deprecates_body;t+=`
385
17
 
386
- ${title}`;
387
- }
388
- if (commit_state.breaking_body) {
389
- const body = colorize ? color2.red(commit_state.breaking_body) : commit_state.breaking_body;
390
- commit_string += `
18
+ ${l}`}if(e.custom_footer){let u=e.custom_footer.split("\\n").map(h=>o?a.reset(h.trim()):h.trim()).join(`
19
+ `);t+=o?`
391
20
 
392
- ${body}`;
393
- }
394
- if (commit_state.deprecates_title) {
395
- const title = colorize ? color2.yellow(`DEPRECATED: ${commit_state.deprecates_title}`) : `DEPRECATED: ${commit_state.deprecates_title}`;
396
- commit_string += `
21
+ ${u}`:`
397
22
 
398
- ${title}`;
399
- }
400
- if (commit_state.deprecates_body) {
401
- const body = colorize ? color2.yellow(commit_state.deprecates_body) : commit_state.deprecates_body;
402
- commit_string += `
23
+ ${u}`}return e.closes&&e.ticket&&(t+=o?`
403
24
 
404
- ${body}`;
405
- }
406
- if (commit_state.custom_footer) {
407
- const temp = commit_state.custom_footer.split("\\n");
408
- const res = temp.map((v) => colorize ? color2.reset(v.trim()) : v.trim()).join("\n");
409
- commit_string += colorize ? `
25
+ ${a.reset(e.closes)} ${a.magenta(e.ticket)}`:`
410
26
 
411
- ${res}` : `
27
+ ${e.closes} ${e.ticket}`),s&&e.trailer&&(t+=o?`
412
28
 
413
- ${res}`;
414
- }
415
- if (commit_state.closes && commit_state.ticket) {
416
- commit_string += colorize ? `
29
+ ${a.dim(e.trailer)}`:`
417
30
 
418
- ${color2.reset(commit_state.closes)} ${color2.magenta(commit_state.ticket)}` : `
419
-
420
- ${commit_state.closes} ${commit_state.ticket}`;
421
- }
422
- if (include_trailer && commit_state.trailer) {
423
- commit_string += colorize ? `
424
-
425
- ${color2.dim(commit_state.trailer)}` : `
426
-
427
- ${commit_state.trailer}`;
428
- }
429
- if (escape_quotes) {
430
- commit_string = commit_string.replaceAll('"', '\\"');
431
- }
432
- return commit_string;
433
- }
434
- export {
435
- main
436
- };
31
+ ${e.trailer}`),m&&(t=t.replaceAll('"','\\"')),t}export{F as main};