diff --git a/apply.ts b/apply.ts index 721892fd..2d48f5fe 100644 --- a/apply.ts +++ b/apply.ts @@ -4,7 +4,7 @@ import path from "path"; import Git from "nodegit"; import { combineRewrittenLists } from "./git-reconcile-rewritten-list/combineRewrittenLists"; -import { createQuestion } from "./util/createQuestion"; +import { question } from "./util/createQuestion"; import { isDirEmptySync } from "./util/fs"; import { filenames } from "./filenames"; @@ -93,46 +93,47 @@ export async function applyIfNeedsToApply({ return { neededToApply: false, }; - } else { - if (!autoApplyIfNeeded) { - const question = createQuestion(); - - const answerRaw: string = await question("\nneed to --apply before continuing. proceed? [Y/n/(a)lways] "); - console.log({ answerRaw }); - - const answer: string = answerRaw.trim().toLowerCase(); - - const userAllowedToApply: boolean = ["y", "yes", ""].includes(answer); - console.log({ userAllowedToApply }); - - const userAllowedToApplyAlways: boolean = ["a", "always"].includes(answer); - - if (!userAllowedToApply && !userAllowedToApplyAlways) { - return { - neededToApply: true, - userAllowedToApplyAndWeApplied: false, - }; - } - - if (userAllowedToApplyAlways) { - await config.setBool(configKeys.autoApplyIfNeeded, 1); - } - } + } - await apply({ - repo, - pathToStackedRebaseTodoFile, - pathToStackedRebaseDirInsideDotGit, // - ...rest, - }); + const allowedToApply = autoApplyIfNeeded || (await askIfCanApply(config)); + if (!allowedToApply) { + return { + neededToApply: true, + userAllowedToApplyAndWeApplied: false, + }; } + await apply({ + repo, + pathToStackedRebaseTodoFile, + pathToStackedRebaseDirInsideDotGit, // + ...rest, + }); + return { neededToApply: true, userAllowedToApplyAndWeApplied: true, // }; } +const askIfCanApply = async (config: Git.Config): Promise => { + const answer = await question( + "need to --apply before continuing. proceed? [Y/n/(a)lways] ", // + (ans) => ans.trim().toLowerCase() + ); + + const userAllowedToApply: boolean = ["y", "yes", ""].includes(answer); + const userAllowedToApplyAlways: boolean = ["a", "always"].includes(answer); + + if (userAllowedToApplyAlways) { + await config.setBool(configKeys.autoApplyIfNeeded, 1); + } + + const canApply = userAllowedToApply || userAllowedToApplyAlways; + + return canApply; +}; + const getPaths = ( pathToStackedRebaseDirInsideDotGit: string // ) => diff --git a/util/createQuestion.ts b/util/createQuestion.ts index 050925df..b265dc1a 100644 --- a/util/createQuestion.ts +++ b/util/createQuestion.ts @@ -1,12 +1,17 @@ import readline from "readline"; -export const createQuestion = ( - rl = readline.createInterface(process.stdin, process.stdout) // -) => ( - q: string // -): Promise => - new Promise((r) => - rl.question(q, (ans) => { - r(ans); - }) - ); +export const createQuestion = + ( + rl = readline.createInterface(process.stdin, process.stdout) // + ) => + ( + q: string // + ): Promise => + new Promise((r) => + rl.question(q, (ans) => { + r(ans); + }) + ); + +export const question = (q: string, cb: (ans: string) => string, { prefix = "\n" } = {}): Promise => + createQuestion()(prefix + q).then(cb);