From c34f56b4a60ec6862919a94d4b7c51c4725321d6 Mon Sep 17 00:00:00 2001 From: Kipras Melnikovas Date: Mon, 3 Apr 2023 12:17:50 +0300 Subject: [PATCH] move! 42946d1 allow providing custom behavior when asking `question`s from user Signed-off-by: Kipras Melnikovas --- git-stacked-rebase.ts | 8 +++++++- internal.ts | 5 +++++ util/createQuestion.ts | 13 +++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/git-stacked-rebase.ts b/git-stacked-rebase.ts index 8a4a15c7..efe2d3ef 100755 --- a/git-stacked-rebase.ts +++ b/git-stacked-rebase.ts @@ -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"; @@ -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, @@ -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); } @@ -129,6 +132,7 @@ export async function gitStackedRebase( config, initialBranch, currentBranch, + askQuestion, }); return; @@ -154,6 +158,7 @@ export async function gitStackedRebase( config, initialBranch, currentBranch, + askQuestion, }); if (options.push) { @@ -703,6 +708,7 @@ mv -f "${preparedRegularRebaseTodoFile}" "${pathToRegularRebaseTodoFile}" config, initialBranch, currentBranch, + askQuestion, }); } diff --git a/internal.ts b/internal.ts index f361f2be..b9e439cd 100644 --- a/internal.ts +++ b/internal.ts @@ -2,6 +2,8 @@ import Git from "nodegit"; +import { AskQuestion } from "./util/createQuestion"; + export const editor__internal = Symbol("editor__internal"); export const getGitConfig__internal = Symbol("getGitConfig__internal"); @@ -9,12 +11,15 @@ export const noEditor = { [editor__internal]: () => void 0, }; +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); diff --git a/util/createQuestion.ts b/util/createQuestion.ts index b265dc1a..6d9ef3ee 100644 --- a/util/createQuestion.ts +++ b/util/createQuestion.ts @@ -13,5 +13,14 @@ export const createQuestion = }) ); -export const question = (q: string, cb: (ans: string) => string, { prefix = "\n" } = {}): Promise => - 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 => 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;