@@ -21,16 +21,23 @@ import {
2121 get_git_root ,
2222 REGEX_SLASH_UND ,
2323 REGEX_START_UND ,
24+ get_value_from_cache ,
25+ set_value_cache ,
26+ NOOP_PROMPT_CACHE ,
2427} from "./utils" ;
2528import { git_add , git_status } from "./git" ;
2629import { CUSTOM_SCOPE_KEY , V_FOOTER_OPTIONS } from "./valibot-consts" ;
2730import { flags } from "./args" ;
31+ import Configstore from "configstore" ;
2832
2933main ( load_setup ( ) ) ;
3034
3135export async function main ( config : Output < typeof Config > ) {
3236 let commit_state = parse ( CommitState , { } ) ;
3337 chdir ( get_git_root ( ) ) ;
38+ const prompt_cache = config . cache_last_value
39+ ? new Configstore ( "better-commits" )
40+ : NOOP_PROMPT_CACHE ;
3441
3542 if ( config . check_status ) {
3643 let { index, work_tree } = git_status ( ) ;
@@ -94,11 +101,13 @@ export async function main(config: Output<typeof Config>) {
94101 }
95102 const commit_type = await p . select ( {
96103 message,
97- initialValue : initial_value ,
104+ initialValue :
105+ get_value_from_cache ( prompt_cache , "commit_type" ) || initial_value ,
98106 maxItems : config . commit_type . max_items ,
99107 options : config . commit_type . options ,
100108 } ) ;
101109 if ( p . isCancel ( commit_type ) ) process . exit ( 0 ) ;
110+ set_value_cache ( prompt_cache , "commit_type" , commit_type ) ;
102111 commit_state . trailer = value_to_data [ commit_type ] . trailer ;
103112 commit_state . type =
104113 config . commit_type . append_emoji_to_commit &&
@@ -110,11 +119,15 @@ export async function main(config: Output<typeof Config>) {
110119 if ( config . commit_scope . enable ) {
111120 let commit_scope = await p . select ( {
112121 message : "Select a commit scope" ,
113- initialValue : config . commit_scope . initial_value ,
122+ initialValue :
123+ get_value_from_cache ( prompt_cache , "commit_scope" ) ||
124+ config . commit_scope . initial_value ,
114125 maxItems : config . commit_scope . max_items ,
115126 options : config . commit_scope . options ,
116127 } ) ;
117128 if ( p . isCancel ( commit_scope ) ) process . exit ( 0 ) ;
129+ set_value_cache ( prompt_cache , "commit_scope" , commit_scope ) ;
130+
118131 if ( commit_scope === CUSTOM_SCOPE_KEY && config . commit_scope . custom_scope ) {
119132 commit_scope = await p . text ( {
120133 message : "Write a custom scope" ,
@@ -158,9 +171,12 @@ export async function main(config: Output<typeof Config>) {
158171 ? `Ticket / issue inferred from branch ${ color . dim ( "(confirm / edit)" ) } `
159172 : `Add ticket / issue ${ OPTIONAL_PROMPT } ` ,
160173 placeholder : "" ,
161- initialValue : commit_state . ticket ,
174+ initialValue :
175+ get_value_from_cache ( prompt_cache , "commit_ticket" ) ||
176+ commit_state . ticket ,
162177 } ) ;
163178 if ( p . isCancel ( user_commit_ticket ) ) process . exit ( 0 ) ;
179+ set_value_cache ( prompt_cache , "commit_ticket" , user_commit_ticket ) ;
164180 commit_state . ticket = user_commit_ticket ?? "" ;
165181 }
166182
@@ -174,6 +190,7 @@ export async function main(config: Output<typeof Config>) {
174190
175191 const commit_title = await p . text ( {
176192 message : "Write a brief title describing the commit" ,
193+ initialValue : get_value_from_cache ( prompt_cache , "commit_title" ) || "" ,
177194 placeholder : "" ,
178195 validate : ( value ) => {
179196 if ( ! value ) return "Please enter a title" ;
@@ -195,6 +212,8 @@ export async function main(config: Output<typeof Config>) {
195212 } ,
196213 } ) ;
197214 if ( p . isCancel ( commit_title ) ) process . exit ( 0 ) ;
215+ set_value_cache ( prompt_cache , "commit_title" , commit_title ) ;
216+
198217 let commit_title_with_emoji = commit_title ;
199218 if (
200219 config . commit_type . append_emoji_to_commit &&
@@ -206,20 +225,26 @@ export async function main(config: Output<typeof Config>) {
206225 if ( config . commit_body . enable ) {
207226 const commit_body = await p . text ( {
208227 message : `Write a detailed description of the changes ${ OPTIONAL_PROMPT } ` ,
228+ initialValue : get_value_from_cache ( prompt_cache , "commit_body" ) || "" ,
209229 placeholder : "" ,
210230 validate : ( val ) => {
211231 if ( config . commit_body . required && ! val )
212232 return "Please enter a description" ;
213233 } ,
214234 } ) ;
215235 if ( p . isCancel ( commit_body ) ) process . exit ( 0 ) ;
236+ set_value_cache ( prompt_cache , "commit_body" , commit_body ) ;
216237 commit_state . body = commit_body ?? "" ;
217238 }
218239
219240 if ( config . commit_footer . enable ) {
241+ const cache_footer = get_value_from_cache (
242+ prompt_cache ,
243+ "commit_footer" ,
244+ ) . split ( "," ) ;
220245 const commit_footer = await p . multiselect ( {
221246 message : `Select optional footers ${ SPACE_TO_SELECT } ` ,
222- initialValues : config . commit_footer . initial_value ,
247+ initialValues : cache_footer || config . commit_footer . initial_value ,
223248 options : COMMIT_FOOTER_OPTIONS as {
224249 value : Output < typeof V_FOOTER_OPTIONS > ;
225250 label : string ;
@@ -228,6 +253,7 @@ export async function main(config: Output<typeof Config>) {
228253 required : false ,
229254 } ) ;
230255 if ( p . isCancel ( commit_footer ) ) process . exit ( 0 ) ;
256+ set_value_cache ( prompt_cache , "commit_footer" , commit_footer . join ( "," ) ) ;
231257
232258 if ( commit_footer . includes ( "breaking-change" ) ) {
233259 const breaking_changes_title = await p . text ( {
@@ -332,6 +358,7 @@ export async function main(config: Output<typeof Config>) {
332358 p . log . error ( "Something went wrong when committing: " + err ) ;
333359 }
334360 p . log . success ( "Commit Complete" ) ;
361+ prompt_cache . clear ( ) ;
335362}
336363
337364function build_commit_string (
0 commit comments