Skip to content

Commit

Permalink
refactor applyIfNeedsToApply logic
Browse files Browse the repository at this point in the history
Signed-off-by: Kipras Melnikovas <[email protected]>
  • Loading branch information
kiprasmel committed Apr 2, 2023
1 parent dce7a22 commit 1acd5aa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
65 changes: 33 additions & 32 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 { createQuestion } from "./util/createQuestion";
import { question } from "./util/createQuestion";
import { isDirEmptySync } from "./util/fs";

import { filenames } from "./filenames";
Expand Down Expand Up @@ -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<boolean> => {
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 //
) =>
Expand Down
25 changes: 15 additions & 10 deletions util/createQuestion.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import readline from "readline";

export const createQuestion = (
rl = readline.createInterface(process.stdin, process.stdout) //
) => (
q: string //
): Promise<string> =>
new Promise<string>((r) =>
rl.question(q, (ans) => {
r(ans);
})
);
export const createQuestion =
(
rl = readline.createInterface(process.stdin, process.stdout) //
) =>
(
q: string //
): Promise<string> =>
new Promise<string>((r) =>
rl.question(q, (ans) => {
r(ans);
})
);

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

0 comments on commit 1acd5aa

Please sign in to comment.