Skip to content

Commit f06be4c

Browse files
committed
refactor: get rid of Exitable, use throw instead
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent e48d363 commit f06be4c

File tree

7 files changed

+46
-204
lines changed

7 files changed

+46
-204
lines changed

branchSequencer.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Git from "nodegit";
66
import { filenames } from "./filenames";
77

88
import { createExecSyncInRepo } from "./util/execSyncInRepo";
9-
import { EitherExitFinal, fail } from "./util/Exitable";
9+
import { Termination } from "./util/error";
1010

1111
import { parseNewGoodCommands } from "./parse-todo-of-stacked-rebase/parseNewGoodCommands";
1212
import { GoodCommand } from "./parse-todo-of-stacked-rebase/validator";
@@ -57,8 +57,8 @@ export type BranchSequencerArgs = BranchSequencerArgsBase & {
5757
rewrittenListFile?: typeof filenames.rewrittenList | typeof filenames.rewrittenListApplied;
5858
};
5959

60-
export type BranchSequencerBase = (args: BranchSequencerArgsBase) => Promise<EitherExitFinal>;
61-
export type BranchSequencer = (args: BranchSequencerArgs) => Promise<EitherExitFinal>;
60+
export type BranchSequencerBase = (args: BranchSequencerArgsBase) => Promise<void>;
61+
export type BranchSequencer = (args: BranchSequencerArgs) => Promise<void>;
6262

6363
export const branchSequencer: BranchSequencer = async ({
6464
pathToStackedRebaseDirInsideDotGit, //
@@ -73,11 +73,14 @@ export const branchSequencer: BranchSequencer = async ({
7373
rewrittenListFile = filenames.rewrittenList,
7474
}) => {
7575
if (!fs.existsSync(pathToStackedRebaseDirInsideDotGit)) {
76-
return fail(`\n\nno stacked-rebase in progress? (nothing to ${rootLevelCommandName})\n\n`);
76+
throw new Termination(`\n\nno stacked-rebase in progress? (nothing to ${rootLevelCommandName})\n\n`);
7777
}
7878

79-
const [exit, stackedRebaseCommandsNew] = parseNewGoodCommands(repo, pathToStackedRebaseTodoFile, rewrittenListFile);
80-
if (!stackedRebaseCommandsNew) return fail(exit);
79+
const stackedRebaseCommandsNew: GoodCommand[] = parseNewGoodCommands(
80+
repo,
81+
pathToStackedRebaseTodoFile,
82+
rewrittenListFile
83+
);
8184

8285
// const remotes: Git.Remote[] = await repo.getRemotes();
8386
// const remote: Git.Remote | undefined = remotes.find((r) =>

git-stacked-rebase.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import { createExecSyncInRepo } from "./util/execSyncInRepo";
1919
import { noop } from "./util/noop";
2020
import { uniq } from "./util/uniq";
2121
import { parseTodoOfStackedRebase } from "./parse-todo-of-stacked-rebase/parseTodoOfStackedRebase";
22-
import { processWriteAndOrExit, fail, EitherExitFinal } from "./util/Exitable";
23-
import { namesOfRebaseCommandsThatMakeRebaseExitToPause } from "./parse-todo-of-stacked-rebase/validator";
22+
import { Termination } from "./util/error";
23+
import { GoodCommand, namesOfRebaseCommandsThatMakeRebaseExitToPause } from "./parse-todo-of-stacked-rebase/validator";
2424

2525
// console.log = () => {};
2626

@@ -84,7 +84,7 @@ function areOptionsIncompetible(
8484
export const gitStackedRebase = async (
8585
nameOfInitialBranch: string,
8686
specifiedOptions: SomeOptionsForGitStackedRebase = {}
87-
): Promise<EitherExitFinal> => {
87+
): Promise<void> => {
8888
try {
8989
const options: OptionsForGitStackedRebase = {
9090
...getDefaultOptions(), //
@@ -95,7 +95,7 @@ export const gitStackedRebase = async (
9595
const reasonsWhatWhyIncompatible: string[] = [];
9696

9797
if (areOptionsIncompetible(options, reasonsWhatWhyIncompatible)) {
98-
return fail(
98+
throw new Termination(
9999
"\n" +
100100
bullets(
101101
"error - incompatible options:", //
@@ -195,7 +195,7 @@ export const gitStackedRebase = async (
195195

196196
if (options.push) {
197197
if (!options.forcePush) {
198-
return fail("\npush without --force will fail (since git rebase overrides history).\n\n");
198+
throw new Termination("\npush without --force will fail (since git rebase overrides history).\n\n");
199199
}
200200

201201
return await forcePush({
@@ -225,7 +225,7 @@ export const gitStackedRebase = async (
225225
* to branchSequencer later.
226226
*/
227227

228-
return fail("\n--branch-sequencer (without --exec) - nothing to do?\n\n");
228+
throw new Termination("\n--branch-sequencer (without --exec) - nothing to do?\n\n");
229229
}
230230
}
231231

@@ -273,8 +273,7 @@ export const gitStackedRebase = async (
273273

274274
const regularRebaseTodoLines: string[] = [];
275275

276-
const [exit, goodCommands] = parseTodoOfStackedRebase(pathToStackedRebaseTodoFile);
277-
if (!goodCommands) return fail(exit);
276+
const goodCommands: GoodCommand[] = parseTodoOfStackedRebase(pathToStackedRebaseTodoFile);
278277

279278
const proms: Promise<void>[] = goodCommands.map(async (cmd) => {
280279
if (cmd.rebaseKind === "regular") {
@@ -646,8 +645,7 @@ cat "$REWRITTEN_LIST_FILE_PATH" > "$REWRITTEN_LIST_BACKUP_FILE_PATH"
646645

647646
return;
648647
} catch (e) {
649-
console.error(e);
650-
return fail(e);
648+
throw e; // TODO FIXME - no try/catch at all?
651649
}
652650
};
653651

@@ -949,7 +947,7 @@ async function getCommitOfBranch(repo: Git.Repository, branchReference: Git.Refe
949947
/**
950948
* the CLI
951949
*/
952-
export async function git_stacked_rebase(): Promise<EitherExitFinal> {
950+
export async function git_stacked_rebase(): Promise<void> {
953951
const pkgFromSrc = path.join(__dirname, "package.json");
954952
const pkgFromDist = path.join(__dirname, "../", "package.json");
955953
let pkg;
@@ -1058,7 +1056,9 @@ git-stacked-rebase ${gitStackedRebaseVersionStr}
10581056
console.log({ "process.argv after non-positional": process.argv });
10591057

10601058
const nameOfInitialBranch: string | undefined = eatNextArg();
1061-
if (!nameOfInitialBranch) return fail(helpMsg);
1059+
if (!nameOfInitialBranch) {
1060+
throw new Termination(helpMsg);
1061+
}
10621062

10631063
/**
10641064
* TODO: improve arg parsing, lmao
@@ -1112,17 +1112,19 @@ git-stacked-rebase ${gitStackedRebaseVersionStr}
11121112
const fourth = eatNextArg();
11131113
branchSequencerExec = fourth ? fourth : false;
11141114
} else {
1115-
return fail(`\n--branch-sequencer can only (for now) be followed by ${execNames.join("|")}\n\n`);
1115+
throw new Termination(
1116+
`\n--branch-sequencer can only (for now) be followed by ${execNames.join("|")}\n\n`
1117+
);
11161118
}
11171119
}
11181120

11191121
if (!isForcePush && !branchSequencerExec) {
1120-
return fail(`\nunrecognized 3th option (got "${third}")\n\n`);
1122+
throw new Termination(`\nunrecognized 3th option (got "${third}")\n\n`);
11211123
}
11221124
}
11231125

11241126
if (process.argv.length) {
1125-
return fail(
1127+
throw new Termination(
11261128
"" + //
11271129
"\n" +
11281130
bullets("\nerror - leftover arguments: ", process.argv, " ") +
@@ -1146,5 +1148,15 @@ git-stacked-rebase ${gitStackedRebaseVersionStr}
11461148

11471149
if (!module.parent) {
11481150
git_stacked_rebase() //
1149-
.then(processWriteAndOrExit);
1151+
.then(() => process.exit(0))
1152+
.catch((e) => {
1153+
if (e instanceof Termination) {
1154+
process.stderr.write(e.message);
1155+
process.exit(1);
1156+
} else {
1157+
console.error(e);
1158+
process.exit(1);
1159+
// throw e;
1160+
}
1161+
});
11501162
}

parse-todo-of-stacked-rebase/parseNewGoodCommands.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Git from "nodegit";
88
import { array } from "nice-comment";
99

1010
import { filenames } from "../filenames";
11-
import { EitherExit, fail, succ } from "../util/Exitable";
1211

1312
import { parseTodoOfStackedRebase } from "./parseTodoOfStackedRebase";
1413
import { GoodCommand, stackedRebaseCommands } from "./validator";
@@ -17,9 +16,8 @@ export function parseNewGoodCommands(
1716
repo: Git.Repository,
1817
pathToStackedRebaseTodoFile: string, //
1918
rewrittenListFile: typeof filenames.rewrittenList | typeof filenames.rewrittenListApplied
20-
): EitherExit<GoodCommand[]> {
21-
const [exit, goodCommands] = parseTodoOfStackedRebase(pathToStackedRebaseTodoFile);
22-
if (!goodCommands) return fail(exit);
19+
): GoodCommand[] {
20+
const goodCommands: GoodCommand[] = parseTodoOfStackedRebase(pathToStackedRebaseTodoFile);
2321

2422
logGoodCmds(goodCommands);
2523

@@ -164,7 +162,7 @@ export function parseNewGoodCommands(
164162

165163
assert(stackedRebaseCommandsOld.length === stackedRebaseCommandsNew.length);
166164

167-
return succ(stackedRebaseCommandsNew);
165+
return stackedRebaseCommandsNew;
168166
}
169167

170168
const logGoodCmds = (goodCommands: GoodCommand[]): void => {

parse-todo-of-stacked-rebase/parseTodoOfStackedRebase.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import fs from "fs";
44

5-
import { EitherExit } from "../util/Exitable";
65
// import path from "path";
76

87
import { GoodCommand, validate } from "./validator";
98

109
export function parseTodoOfStackedRebase(
1110
pathToStackedRebaseTodoFile: string //
1211
// goodCommands: GoodCommand[]
13-
): EitherExit<GoodCommand[]> {
12+
): GoodCommand[] {
1413
const editedRebaseTodo: string = fs.readFileSync(pathToStackedRebaseTodoFile, { encoding: "utf-8" });
1514
const linesOfEditedRebaseTodo: string[] = editedRebaseTodo.split("\n").filter((line) => !!line);
1615

parse-todo-of-stacked-rebase/validator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { assert } from "console";
55
import { bullets, joinWith, tick } from "nice-comment";
6-
import { EitherExit, fail, succ } from "../util/Exitable";
6+
import { Termination } from "../util/error";
77

88
/**
99
* if invalid, should fill the array with reasons why not valid.
@@ -292,7 +292,7 @@ export type GoodCommand = {
292292
}
293293
);
294294

295-
export function validate(linesOfEditedRebaseTodo: string[]): EitherExit<GoodCommand[]> {
295+
export function validate(linesOfEditedRebaseTodo: string[]): GoodCommand[] {
296296
const badCommands: BadCommand[] = [];
297297
const goodCommands: GoodCommand[] = [];
298298

@@ -418,7 +418,7 @@ export function validate(linesOfEditedRebaseTodo: string[]): EitherExit<GoodComm
418418
});
419419

420420
if (badCommands.length) {
421-
return fail(
421+
throw new Termination(
422422
"\n" +
423423
joinWith("\n\n")([
424424
"found errors in rebase commands:",
@@ -432,5 +432,5 @@ export function validate(linesOfEditedRebaseTodo: string[]): EitherExit<GoodComm
432432
);
433433
}
434434

435-
return succ(goodCommands);
435+
return goodCommands;
436436
}

0 commit comments

Comments
 (0)