Skip to content

Commit 7b19b65

Browse files
authored
feat(util): #117 cache last value (#118)
Added config "cache_last_value", default . When re-running better-commits, will use values from the previous run. Closes: #117
1 parent 59ad3f3 commit 7b19b65

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
9191
"commit_type": {
9292
"enable": true,
9393
"initial_value": "feat",
94-
"max_items": Infinity,
94+
"max_items": 20,
9595
"infer_type_from_branch": true,
9696
"append_emoji_to_label": false,
9797
"append_emoji_to_commit": false,
@@ -170,7 +170,7 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
170170
"enable": true,
171171
"custom_scope": false,
172172
"initial_value": "app",
173-
"max_items": Infinity
173+
"max_items": 20
174174
"options": [
175175
{
176176
"value": "app",
@@ -219,6 +219,7 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
219219
"add_exclamation_to_title": true
220220
},
221221
"confirm_commit": true,
222+
"cache_last_value": true,
222223
"confirm_with_editor": false,
223224
"print_commit_output": true,
224225
"branch_pre_commands": [],
@@ -316,6 +317,7 @@ Expand to see explanations and possible values
316317
| `breaking_change.add_exclamation_to_title` | If true adds exclamation mark to title for breaking changes |
317318
| `confirm_commit` | If true manually confirm commit at end |
318319
| `confirm_with_editor` | Confirm / Edit commit with $GIT_EDITOR / $EDITOR |
320+
| `cache_last_value` | Reuse last prompt value after cancel |
319321
| `print_commit_output` | If true pretty print commit preview |
320322
| `overrides.shell` | Override default shell, useful for windows users |
321323

src/index.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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";
2528
import { git_add, git_status } from "./git";
2629
import { CUSTOM_SCOPE_KEY, V_FOOTER_OPTIONS } from "./valibot-consts";
2730
import { flags } from "./args";
31+
import Configstore from "configstore";
2832

2933
main(load_setup());
3034

3135
export 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

337364
function build_commit_string(

src/utils.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Config } from "./valibot-state";
88
import { V_BRANCH_ACTIONS } from "./valibot-consts";
99
import { argv } from "process";
1010
import { flags } from "./args";
11+
import Configstore from "configstore";
1112

1213
export const CONFIG_FILE_NAME = ".better-commits.json";
1314
export const SPACE_TO_SELECT = `${color.dim("(<space> to select)")}`;
@@ -56,6 +57,12 @@ export const BRANCH_ACTION_OPTIONS: {
5657
{ value: "worktree", label: "Worktree" },
5758
];
5859

60+
export const NOOP_PROMPT_CACHE = {
61+
get: () => "",
62+
set: (key: string, value: string) => {},
63+
clear: () => {},
64+
} as unknown as Configstore;
65+
5966
/* LOAD */
6067
export function load_setup(
6168
cli_name = " better-commits ",
@@ -84,6 +91,7 @@ export function load_setup(
8491
? global_config.overrides
8592
: repo_config.overrides,
8693
confirm_with_editor: global_config.confirm_with_editor,
94+
cache_last_value: global_config.cache_last_value,
8795
}
8896
: repo_config;
8997
}
@@ -187,3 +195,32 @@ export function clean_commit_title(title: string): string {
187195
function set_non_configuration_arguments() {
188196
flags.git_args = `${argv[2] ?? ""} ${argv[3] ?? ""}`.trim();
189197
}
198+
199+
export function get_value_from_cache(
200+
config_store: Configstore,
201+
key: string,
202+
): string {
203+
try {
204+
return config_store.get(key) ?? "";
205+
} catch (err) {
206+
p.log.warn(
207+
`Could not access ${key} from cache. Check that "~/.config" exists. Set "cache_last_value" to false to disable.`,
208+
);
209+
}
210+
211+
return "";
212+
}
213+
214+
export function set_value_cache(
215+
config_store: Configstore,
216+
key: string,
217+
value: string,
218+
): void {
219+
try {
220+
config_store.set(key, value);
221+
} catch (err) {
222+
p.log.warn(
223+
`Could not access ${key} from cache. Check that "~/.config" exists. Set "cache_last_value" to false to disable.`,
224+
);
225+
}
226+
}

src/valibot-state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export const Config = v.object({
162162
}),
163163
{},
164164
),
165+
cache_last_value: v.optional(v.boolean(), true),
165166
confirm_with_editor: v.optional(v.boolean(), false),
166167
confirm_commit: v.optional(v.boolean(), true),
167168
print_commit_output: v.optional(v.boolean(), true),

0 commit comments

Comments
 (0)