Skip to content

Commit 8ed6846

Browse files
author
gidoichi
committed
open editor after the hook on error
1 parent 58b9d84 commit 8ed6846

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

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)