Skip to content

Commit

Permalink
refactor: get rid of Exitable, use throw instead
Browse files Browse the repository at this point in the history
Signed-off-by: Kipras Melnikovas <[email protected]>
  • Loading branch information
kiprasmel committed Apr 9, 2022
1 parent e48d363 commit f06be4c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 204 deletions.
15 changes: 9 additions & 6 deletions branchSequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Git from "nodegit";
import { filenames } from "./filenames";

import { createExecSyncInRepo } from "./util/execSyncInRepo";
import { EitherExitFinal, fail } from "./util/Exitable";
import { Termination } from "./util/error";

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

export type BranchSequencerBase = (args: BranchSequencerArgsBase) => Promise<EitherExitFinal>;
export type BranchSequencer = (args: BranchSequencerArgs) => Promise<EitherExitFinal>;
export type BranchSequencerBase = (args: BranchSequencerArgsBase) => Promise<void>;
export type BranchSequencer = (args: BranchSequencerArgs) => Promise<void>;

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

const [exit, stackedRebaseCommandsNew] = parseNewGoodCommands(repo, pathToStackedRebaseTodoFile, rewrittenListFile);
if (!stackedRebaseCommandsNew) return fail(exit);
const stackedRebaseCommandsNew: GoodCommand[] = parseNewGoodCommands(
repo,
pathToStackedRebaseTodoFile,
rewrittenListFile
);

// const remotes: Git.Remote[] = await repo.getRemotes();
// const remote: Git.Remote | undefined = remotes.find((r) =>
Expand Down
44 changes: 28 additions & 16 deletions git-stacked-rebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { createExecSyncInRepo } from "./util/execSyncInRepo";
import { noop } from "./util/noop";
import { uniq } from "./util/uniq";
import { parseTodoOfStackedRebase } from "./parse-todo-of-stacked-rebase/parseTodoOfStackedRebase";
import { processWriteAndOrExit, fail, EitherExitFinal } from "./util/Exitable";
import { namesOfRebaseCommandsThatMakeRebaseExitToPause } from "./parse-todo-of-stacked-rebase/validator";
import { Termination } from "./util/error";
import { GoodCommand, namesOfRebaseCommandsThatMakeRebaseExitToPause } from "./parse-todo-of-stacked-rebase/validator";

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

Expand Down Expand Up @@ -84,7 +84,7 @@ function areOptionsIncompetible(
export const gitStackedRebase = async (
nameOfInitialBranch: string,
specifiedOptions: SomeOptionsForGitStackedRebase = {}
): Promise<EitherExitFinal> => {
): Promise<void> => {
try {
const options: OptionsForGitStackedRebase = {
...getDefaultOptions(), //
Expand All @@ -95,7 +95,7 @@ export const gitStackedRebase = async (
const reasonsWhatWhyIncompatible: string[] = [];

if (areOptionsIncompetible(options, reasonsWhatWhyIncompatible)) {
return fail(
throw new Termination(
"\n" +
bullets(
"error - incompatible options:", //
Expand Down Expand Up @@ -195,7 +195,7 @@ export const gitStackedRebase = async (

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

return await forcePush({
Expand Down Expand Up @@ -225,7 +225,7 @@ export const gitStackedRebase = async (
* to branchSequencer later.
*/

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

Expand Down Expand Up @@ -273,8 +273,7 @@ export const gitStackedRebase = async (

const regularRebaseTodoLines: string[] = [];

const [exit, goodCommands] = parseTodoOfStackedRebase(pathToStackedRebaseTodoFile);
if (!goodCommands) return fail(exit);
const goodCommands: GoodCommand[] = parseTodoOfStackedRebase(pathToStackedRebaseTodoFile);

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

return;
} catch (e) {
console.error(e);
return fail(e);
throw e; // TODO FIXME - no try/catch at all?
}
};

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

const nameOfInitialBranch: string | undefined = eatNextArg();
if (!nameOfInitialBranch) return fail(helpMsg);
if (!nameOfInitialBranch) {
throw new Termination(helpMsg);
}

/**
* TODO: improve arg parsing, lmao
Expand Down Expand Up @@ -1112,17 +1112,19 @@ git-stacked-rebase ${gitStackedRebaseVersionStr}
const fourth = eatNextArg();
branchSequencerExec = fourth ? fourth : false;
} else {
return fail(`\n--branch-sequencer can only (for now) be followed by ${execNames.join("|")}\n\n`);
throw new Termination(
`\n--branch-sequencer can only (for now) be followed by ${execNames.join("|")}\n\n`
);
}
}

if (!isForcePush && !branchSequencerExec) {
return fail(`\nunrecognized 3th option (got "${third}")\n\n`);
throw new Termination(`\nunrecognized 3th option (got "${third}")\n\n`);
}
}

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

if (!module.parent) {
git_stacked_rebase() //
.then(processWriteAndOrExit);
.then(() => process.exit(0))
.catch((e) => {
if (e instanceof Termination) {
process.stderr.write(e.message);
process.exit(1);
} else {
console.error(e);
process.exit(1);
// throw e;
}
});
}
8 changes: 3 additions & 5 deletions parse-todo-of-stacked-rebase/parseNewGoodCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Git from "nodegit";
import { array } from "nice-comment";

import { filenames } from "../filenames";
import { EitherExit, fail, succ } from "../util/Exitable";

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

logGoodCmds(goodCommands);

Expand Down Expand Up @@ -164,7 +162,7 @@ export function parseNewGoodCommands(

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

return succ(stackedRebaseCommandsNew);
return stackedRebaseCommandsNew;
}

const logGoodCmds = (goodCommands: GoodCommand[]): void => {
Expand Down
3 changes: 1 addition & 2 deletions parse-todo-of-stacked-rebase/parseTodoOfStackedRebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import fs from "fs";

import { EitherExit } from "../util/Exitable";
// import path from "path";

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

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

Expand Down
8 changes: 4 additions & 4 deletions parse-todo-of-stacked-rebase/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

/**
* if invalid, should fill the array with reasons why not valid.
Expand Down Expand Up @@ -292,7 +292,7 @@ export type GoodCommand = {
}
);

export function validate(linesOfEditedRebaseTodo: string[]): EitherExit<GoodCommand[]> {
export function validate(linesOfEditedRebaseTodo: string[]): GoodCommand[] {
const badCommands: BadCommand[] = [];
const goodCommands: GoodCommand[] = [];

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

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

return succ(goodCommands);
return goodCommands;
}
Loading

0 comments on commit f06be4c

Please sign in to comment.