( isStageAllFlag: Boolean = false )
| 10 | const [messageFilePath, commitSource] = process.argv.slice(2); |
| 11 | |
| 12 | export const prepareCommitMessageHook = async ( |
| 13 | isStageAllFlag: Boolean = false |
| 14 | ) => { |
| 15 | try { |
| 16 | if (!messageFilePath) { |
| 17 | throw new Error( |
| 18 | 'Commit message file path is missing. This file should be called from the "prepare-commit-msg" git hook' |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | if (commitSource) return; |
| 23 | |
| 24 | if (isStageAllFlag) { |
| 25 | const changedFiles = await getChangedFiles(); |
| 26 | |
| 27 | if (changedFiles) await gitAdd({ files: changedFiles }); |
| 28 | else { |
| 29 | outro('No changes detected, write some code and run `oco` again'); |
| 30 | process.exit(1); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | const staged = await getStagedFiles(); |
| 35 | |
| 36 | if (!staged) return; |
| 37 | |
| 38 | intro('opencommit'); |
| 39 | |
| 40 | const config = getConfig(); |
| 41 | |
| 42 | if (!config.OCO_API_KEY) { |
| 43 | outro( |
| 44 | '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' |
| 45 | ); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | const spin = spinner(); |
| 50 | spin.start('Generating commit message'); |
| 51 | |
| 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 | |
| 62 | spin.stop('Done'); |
| 63 | |
| 64 | const fileContent = await fs.readFile(messageFilePath); |
| 65 | |
| 66 | 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()}`; |
| 67 | const messageWithoutComment = `${commitMessage}\n\n${fileContent.toString()}`; |
| 68 | |
| 69 | const message = config.OCO_HOOK_AUTO_UNCOMMENT |
no test coverage detected
searching dependent graphs…