Skip to content

Commit

Permalink
allow providing custom behavior when asking questions from user
Browse files Browse the repository at this point in the history
Signed-off-by: Kipras Melnikovas <[email protected]>
  • Loading branch information
kiprasmel committed Apr 3, 2023
1 parent 72f4f72 commit 4c981e4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
12 changes: 7 additions & 5 deletions apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "path";
import Git from "nodegit";
import { combineRewrittenLists } from "./git-reconcile-rewritten-list/combineRewrittenLists";

import { question } from "./util/createQuestion";
import { AskQuestion, question, Questions } from "./util/createQuestion";
import { isDirEmptySync } from "./util/fs";

import { filenames } from "./filenames";
Expand Down Expand Up @@ -82,10 +82,12 @@ export async function applyIfNeedsToApply({
pathToStackedRebaseDirInsideDotGit, //
autoApplyIfNeeded,
config,
askQuestion = question,
...rest
}: BranchSequencerArgsBase & {
autoApplyIfNeeded: boolean; //
config: Git.Config;
askQuestion: AskQuestion;
}): Promise<ReturnOfApplyIfNeedsToApply> {
const needsToApply: boolean = doesNeedToApply(pathToStackedRebaseDirInsideDotGit);

Expand All @@ -95,7 +97,7 @@ export async function applyIfNeedsToApply({
};
}

const allowedToApply = autoApplyIfNeeded || (await askIfCanApply(config));
const allowedToApply = autoApplyIfNeeded || (await askIfCanApply(config, askQuestion));
if (!allowedToApply) {
return {
neededToApply: true,
Expand All @@ -116,9 +118,9 @@ export async function applyIfNeedsToApply({
};
}

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

Expand Down
8 changes: 7 additions & 1 deletion git-stacked-rebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { apply, applyIfNeedsToApply, markThatNeedsToApply } from "./apply";
import { forcePush } from "./forcePush";
import { BehaviorOfGetBranchBoundaries, branchSequencer } from "./branchSequencer";
import { autosquash } from "./autosquash";
import { editor__internal, EitherEditor } from "./internal";
import { askQuestion__internal, editor__internal, EitherEditor } from "./internal";

import { createExecSyncInRepo } from "./util/execSyncInRepo";
import { noop } from "./util/noop";
Expand All @@ -37,6 +37,7 @@ import { Termination } from "./util/error";
import { assertNever } from "./util/assertNever";
import { Single, Tuple } from "./util/tuple";
import { isDirEmptySync } from "./util/fs";
import { AskQuestion, question } from "./util/createQuestion";
import {
getParseTargetsCtxFromLine,
GoodCommand,
Expand Down Expand Up @@ -86,6 +87,8 @@ export async function gitStackedRebase(

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

const askQuestion: AskQuestion = askQuestion__internal in options ? options[askQuestion__internal]! : question;

if (fs.existsSync(path.join(pathToStackedRebaseDirInsideDotGit, filenames.willNeedToApply))) {
markThatNeedsToApply(pathToStackedRebaseDirInsideDotGit);
}
Expand Down Expand Up @@ -128,6 +131,7 @@ export async function gitStackedRebase(
config,
initialBranch,
currentBranch,
askQuestion,
});

return;
Expand All @@ -143,6 +147,7 @@ export async function gitStackedRebase(
config,
initialBranch,
currentBranch,
askQuestion,
});

if (neededToApply && !userAllowedToApplyAndWeApplied) {
Expand Down Expand Up @@ -702,6 +707,7 @@ mv -f "${preparedRegularRebaseTodoFile}" "${pathToRegularRebaseTodoFile}"
config,
initialBranch,
currentBranch,
askQuestion,
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import Git from "nodegit";

import { AskQuestion } from "./util/createQuestion";

export const editor__internal = Symbol("editor__internal");
export const getGitConfig__internal = Symbol("getGitConfig__internal");

export const askQuestion__internal = Symbol("askQuestion__internal");

/**
* meant to NOT be exported to the end user of the library
*/
export type InternalOnlyOptions = {
[editor__internal]?: EitherEditor;
[getGitConfig__internal]?: GetGitConfig;
[askQuestion__internal]?: AskQuestion;
};

export type EitherEditor = string | ((ctx: { filePath: string }) => void | Promise<void>);
Expand Down
13 changes: 11 additions & 2 deletions util/createQuestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,14 @@ export const createQuestion =
})
);

export const question = (q: string, cb: (ans: string) => string, { prefix = "\n" } = {}): Promise<string> =>
createQuestion()(prefix + q).then(cb);
export type AskQuestion = typeof question;

export const question = (
q: string, //
cb: (ans: string) => string = (ans) => ans,
{ prefix = "\n" } = {}
): string | Promise<string> => createQuestion()(prefix + q).then(cb);

export const Questions = {
need_to_apply_before_continuing: "need to --apply before continuing. proceed? [Y/n/(a)lways] ", //
} as const;

0 comments on commit 4c981e4

Please sign in to comment.