Skip to content

Commit a32f15c

Browse files
committed
refactor applyIfNeedsToApply logic
Signed-off-by: Kipras Melnikovas <[email protected]>
1 parent 094cddc commit a32f15c

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

apply.ts

Lines changed: 33 additions & 32 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 { createQuestion } from "./util/createQuestion";
7+
import { question } from "./util/createQuestion";
88
import { isDirEmptySync } from "./util/fs";
99

1010
import { filenames } from "./filenames";
@@ -93,46 +93,47 @@ export async function applyIfNeedsToApply({
9393
return {
9494
neededToApply: false,
9595
};
96-
} else {
97-
if (!autoApplyIfNeeded) {
98-
const question = createQuestion();
99-
100-
const answerRaw: string = await question("\nneed to --apply before continuing. proceed? [Y/n/(a)lways] ");
101-
console.log({ answerRaw });
102-
103-
const answer: string = answerRaw.trim().toLowerCase();
104-
105-
const userAllowedToApply: boolean = ["y", "yes", ""].includes(answer);
106-
console.log({ userAllowedToApply });
107-
108-
const userAllowedToApplyAlways: boolean = ["a", "always"].includes(answer);
109-
110-
if (!userAllowedToApply && !userAllowedToApplyAlways) {
111-
return {
112-
neededToApply: true,
113-
userAllowedToApplyAndWeApplied: false,
114-
};
115-
}
116-
117-
if (userAllowedToApplyAlways) {
118-
await config.setBool(configKeys.autoApplyIfNeeded, 1);
119-
}
120-
}
96+
}
12197

122-
await apply({
123-
repo,
124-
pathToStackedRebaseTodoFile,
125-
pathToStackedRebaseDirInsideDotGit, //
126-
...rest,
127-
});
98+
const allowedToApply = autoApplyIfNeeded || (await askIfCanApply(config));
99+
if (!allowedToApply) {
100+
return {
101+
neededToApply: true,
102+
userAllowedToApplyAndWeApplied: false,
103+
};
128104
}
129105

106+
await apply({
107+
repo,
108+
pathToStackedRebaseTodoFile,
109+
pathToStackedRebaseDirInsideDotGit, //
110+
...rest,
111+
});
112+
130113
return {
131114
neededToApply: true,
132115
userAllowedToApplyAndWeApplied: true, //
133116
};
134117
}
135118

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] ", //
122+
(ans) => ans.trim().toLowerCase()
123+
);
124+
125+
const userAllowedToApply: boolean = ["y", "yes", ""].includes(answer);
126+
const userAllowedToApplyAlways: boolean = ["a", "always"].includes(answer);
127+
128+
if (userAllowedToApplyAlways) {
129+
await config.setBool(configKeys.autoApplyIfNeeded, 1);
130+
}
131+
132+
const canApply = userAllowedToApply || userAllowedToApplyAlways;
133+
134+
return canApply;
135+
};
136+
136137
const getPaths = (
137138
pathToStackedRebaseDirInsideDotGit: string //
138139
) =>

util/createQuestion.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import readline from "readline";
22

3-
export const createQuestion = (
4-
rl = readline.createInterface(process.stdin, process.stdout) //
5-
) => (
6-
q: string //
7-
): Promise<string> =>
8-
new Promise<string>((r) =>
9-
rl.question(q, (ans) => {
10-
r(ans);
11-
})
12-
);
3+
export const createQuestion =
4+
(
5+
rl = readline.createInterface(process.stdin, process.stdout) //
6+
) =>
7+
(
8+
q: string //
9+
): Promise<string> =>
10+
new Promise<string>((r) =>
11+
rl.question(q, (ans) => {
12+
r(ans);
13+
})
14+
);
15+
16+
export const question = (q: string, cb: (ans: string) => string, { prefix = "\n" } = {}): Promise<string> =>
17+
createQuestion()(prefix + q).then(cb);

0 commit comments

Comments
 (0)