Skip to content

Commit 4b4b58f

Browse files
author
gidoichi
committed
open editor after the hook on error
1 parent ebbaff0 commit 4b4b58f

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

out/cli.cjs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67923,9 +67923,15 @@ var prepareCommitMessageHook = async (isStageAllFlag = false) => {
6792367923
}
6792467924
const spin = le();
6792567925
spin.start("Generating commit message");
67926-
const commitMessage = await generateCommitMessageByDiff(
67927-
await getDiff({ files: staged })
67928-
);
67926+
let commitMessage;
67927+
try {
67928+
commitMessage = await generateCommitMessageByDiff(
67929+
await getDiff({ files: staged })
67930+
);
67931+
} catch (error) {
67932+
spin.stop("Done");
67933+
throw error;
67934+
}
6792967935
spin.stop("Done");
6793067936
const fileContent = await import_promises4.default.readFile(messageFilePath);
6793167937
const messageWithComment = `# ${commitMessage}
@@ -67941,8 +67947,24 @@ ${fileContent.toString()}`;
6794167947
const message = config7.OCO_HOOK_AUTO_UNCOMMENT ? messageWithoutComment : messageWithComment;
6794267948
await import_promises4.default.writeFile(messageFilePath, message);
6794367949
} catch (error) {
67944-
ce(`${source_default.red("\u2716")} ${error}`);
67945-
process.exit(1);
67950+
try {
67951+
ce(`${source_default.red("\u2716")} ${error}`);
67952+
const fileContent = await import_promises4.default.readFile(messageFilePath);
67953+
const commentedError = String(error).replace(new RegExp("^", "gm"), "# ");
67954+
const message = `
67955+
67956+
# ---------- [OpenCommit] ---------- #
67957+
# Failed to generate the commit message.
67958+
# To cancel the commit, just close this window without making any changes.
67959+
67960+
${commentedError}
67961+
67962+
${fileContent.toString()}`;
67963+
await import_promises4.default.writeFile(messageFilePath, message);
67964+
} catch (error2) {
67965+
ce(`${source_default.red("\u2716")} ${error2}`);
67966+
process.exit(1);
67967+
}
6794667968
}
6794767969
};
6794867970

src/commands/prepare-commit-msg-hook.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@ export const prepareCommitMessageHook = async (
4949
const spin = spinner();
5050
spin.start('Generating commit message');
5151

52-
const commitMessage = await generateCommitMessageByDiff(
53-
await getDiff({ files: staged })
54-
);
52+
let commitMessage: string;
53+
try {
54+
commitMessage = await generateCommitMessageByDiff(
55+
await getDiff({ files: staged })
56+
);
57+
} catch (error) {
58+
spin.stop('Done');
59+
throw error;
60+
}
61+
5562
spin.stop('Done');
5663

5764
const fileContent = await fs.readFile(messageFilePath);
@@ -65,7 +72,17 @@ export const prepareCommitMessageHook = async (
6572

6673
await fs.writeFile(messageFilePath, message);
6774
} catch (error) {
68-
outro(`${chalk.red('✖')} ${error}`);
69-
process.exit(1);
75+
try {
76+
outro(`${chalk.red('✖')} ${error}`);
77+
const fileContent = await fs.readFile(messageFilePath);
78+
79+
const commentedError = String(error).replace(new RegExp('^', 'gm'), '# ');
80+
const message = `\n\n# ---------- [OpenCommit] ---------- #\n# Failed to generate the commit message.\n# To cancel the commit, just close this window without making any changes.\n\n${commentedError}\n\n${fileContent.toString()}`
81+
82+
await fs.writeFile(messageFilePath, message);
83+
} catch (error) {
84+
outro(`${chalk.red('✖')} ${error}`);
85+
process.exit(1);
86+
}
7087
}
7188
};

0 commit comments

Comments
 (0)