forked from di-sukharev/opencommit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-commit-msg-hook.ts
More file actions
71 lines (53 loc) · 2.12 KB
/
prepare-commit-msg-hook.ts
File metadata and controls
71 lines (53 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import chalk from 'chalk';
import fs from 'fs/promises';
import { intro, outro, spinner } from '@clack/prompts';
import { generateCommitMessageByDiff } from '../generateCommitMessageFromGitDiff';
import { getChangedFiles, getDiff, getStagedFiles, gitAdd } from '../utils/git';
import { getConfig } from './config';
const [messageFilePath, commitSource] = process.argv.slice(2);
export const prepareCommitMessageHook = async (
isStageAllFlag: Boolean = false
) => {
try {
if (!messageFilePath) {
throw new Error(
'Commit message file path is missing. This file should be called from the "prepare-commit-msg" git hook'
);
}
if (commitSource) return;
if (isStageAllFlag) {
const changedFiles = await getChangedFiles();
if (changedFiles) await gitAdd({ files: changedFiles });
else {
outro('No changes detected, write some code and run `oco` again');
process.exit(1);
}
}
const staged = await getStagedFiles();
if (!staged) return;
intro('opencommit');
const config = getConfig();
if (!config.OCO_API_KEY) {
outro(
'No OCO_API_KEY is set. Set your key via `oco config set OCO_API_KEY=<value>. For more info see https://github.com/di-sukharev/opencommit'
);
return;
}
const spin = spinner();
spin.start('Generating commit message');
const commitMessage = await generateCommitMessageByDiff(
await getDiff({ files: staged })
);
spin.stop('Done');
const fileContent = await fs.readFile(messageFilePath);
const messageWithComment = `# ${commitMessage}\n\n# ---------- [OpenCommit] ---------- #\n# Remove the # above to use this generated commit message.\n# To cancel the commit, just close this window without making any changes.\n\n${fileContent.toString()}`;
const messageWithoutComment = `${commitMessage}\n\n${fileContent.toString()}`;
const message = config.OCO_HOOK_AUTO_UNCOMMENT
? messageWithoutComment
: messageWithComment;
await fs.writeFile(messageFilePath, message);
} catch (error) {
outro(`${chalk.red('✖')} ${error}`);
process.exit(1);
}
};