Skip to content

Commit a4447a5

Browse files
committed
allow providing custom behavior when asking questions from user
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent c89c7da commit a4447a5

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

apply.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from "path";
44
import Git from "nodegit";
55
import { combineRewrittenLists } from "./git-reconcile-rewritten-list/combineRewrittenLists";
66

7-
import { question } from "./util/createQuestion";
7+
import { AskQuestion, question, Questions } from "./util/createQuestion";
88
import { isDirEmptySync } from "./util/fs";
99

1010
import { filenames } from "./filenames";
@@ -82,10 +82,12 @@ export async function applyIfNeedsToApply({
8282
pathToStackedRebaseDirInsideDotGit, //
8383
autoApplyIfNeeded,
8484
config,
85+
askQuestion = question,
8586
...rest
8687
}: BranchSequencerArgsBase & {
8788
autoApplyIfNeeded: boolean; //
8889
config: Git.Config;
90+
askQuestion: AskQuestion;
8991
}): Promise<ReturnOfApplyIfNeedsToApply> {
9092
const needsToApply: boolean = doesNeedToApply(pathToStackedRebaseDirInsideDotGit);
9193

@@ -95,7 +97,7 @@ export async function applyIfNeedsToApply({
9597
};
9698
}
9799

98-
const allowedToApply = autoApplyIfNeeded || (await askIfCanApply(config));
100+
const allowedToApply = autoApplyIfNeeded || (await askIfCanApply(config, askQuestion));
99101
if (!allowedToApply) {
100102
return {
101103
neededToApply: true,
@@ -116,9 +118,9 @@ export async function applyIfNeedsToApply({
116118
};
117119
}
118120

119-
const askIfCanApply = async (config: Git.Config): Promise<boolean> => {
120-
const answer = await question(
121-
"need to --apply before continuing. proceed? [Y/n/(a)lways] ", //
121+
const askIfCanApply = async (config: Git.Config, askQuestion: AskQuestion = question): Promise<boolean> => {
122+
const answer = await askQuestion(
123+
Questions.need_to_apply_before_continuing, //
122124
(ans) => ans.trim().toLowerCase()
123125
);
124126

git-stacked-rebase.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { apply, applyIfNeedsToApply, markThatNeedsToApply } from "./apply";
2727
import { forcePush } from "./forcePush";
2828
import { BehaviorOfGetBranchBoundaries, branchSequencer } from "./branchSequencer";
2929
import { autosquash } from "./autosquash";
30-
import { editor__internal, EitherEditor } from "./internal";
30+
import { askQuestion__internal, editor__internal, EitherEditor } from "./internal";
3131

3232
import { createExecSyncInRepo } from "./util/execSyncInRepo";
3333
import { noop } from "./util/noop";
@@ -37,6 +37,7 @@ import { Termination } from "./util/error";
3737
import { assertNever } from "./util/assertNever";
3838
import { Single, Tuple } from "./util/tuple";
3939
import { isDirEmptySync } from "./util/fs";
40+
import { AskQuestion, question } from "./util/createQuestion";
4041
import {
4142
getParseTargetsCtxFromLine,
4243
GoodCommand,
@@ -86,6 +87,8 @@ export async function gitStackedRebase(
8687

8788
const currentBranch: Git.Reference = await repo.getCurrentBranch();
8889

90+
const askQuestion: AskQuestion = askQuestion__internal in options ? options[askQuestion__internal]! : question;
91+
8992
if (fs.existsSync(path.join(pathToStackedRebaseDirInsideDotGit, filenames.willNeedToApply))) {
9093
markThatNeedsToApply(pathToStackedRebaseDirInsideDotGit);
9194
}
@@ -128,6 +131,7 @@ export async function gitStackedRebase(
128131
config,
129132
initialBranch,
130133
currentBranch,
134+
askQuestion,
131135
});
132136

133137
return;
@@ -143,6 +147,7 @@ export async function gitStackedRebase(
143147
config,
144148
initialBranch,
145149
currentBranch,
150+
askQuestion,
146151
});
147152

148153
if (neededToApply && !userAllowedToApplyAndWeApplied) {
@@ -702,6 +707,7 @@ mv -f "${preparedRegularRebaseTodoFile}" "${pathToRegularRebaseTodoFile}"
702707
config,
703708
initialBranch,
704709
currentBranch,
710+
askQuestion,
705711
});
706712
}
707713
}

internal.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
import Git from "nodegit";
44

5+
import { AskQuestion } from "./util/createQuestion";
6+
57
export const editor__internal = Symbol("editor__internal");
68
export const getGitConfig__internal = Symbol("getGitConfig__internal");
79

10+
export const askQuestion__internal = Symbol("askQuestion__internal");
11+
812
/**
913
* meant to NOT be exported to the end user of the library
1014
*/
1115
export type InternalOnlyOptions = {
1216
[editor__internal]?: EitherEditor;
1317
[getGitConfig__internal]?: GetGitConfig;
18+
[askQuestion__internal]?: AskQuestion;
1419
};
1520

1621
export type EitherEditor = string | ((ctx: { filePath: string }) => void | Promise<void>);

util/createQuestion.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,14 @@ export const createQuestion =
1313
})
1414
);
1515

16-
export const question = (q: string, cb: (ans: string) => string, { prefix = "\n" } = {}): Promise<string> =>
17-
createQuestion()(prefix + q).then(cb);
16+
export type AskQuestion = typeof question;
17+
18+
export const question = (
19+
q: typeof Questions[keyof typeof Questions] | (string & {}), //
20+
cb: (ans: string) => string = (ans) => ans,
21+
{ prefix = "\n" } = {}
22+
): string | Promise<string> => createQuestion()(prefix + q).then(cb);
23+
24+
export const Questions = {
25+
need_to_apply_before_continuing: "need to --apply before continuing. proceed? [Y/n/(a)lways] ", //
26+
} as const;

0 commit comments

Comments
 (0)