Skip to content

Commit 5fb5ce2

Browse files
committed
feat: read config from process.env
1 parent a619cd1 commit 5fb5ce2

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/commands/config.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { COMMANDS } from '../CommandsEnum';
99

1010
export enum CONFIG_KEYS {
1111
OPENAI_API_KEY = 'OPENAI_API_KEY',
12-
description = 'description',
13-
emoji = 'emoji'
12+
OPENCOMMIT_DESCRIPTION = 'OPENCOMMIT_DESCRIPTION',
13+
OPENCOMMIT_EMOJI = 'OPENCOMMIT_EMOJI'
1414
}
1515

1616
export enum CONFIG_MODES {
@@ -47,23 +47,25 @@ export const configValidators = {
4747

4848
return value;
4949
},
50-
[CONFIG_KEYS.description](value: any) {
50+
[CONFIG_KEYS.OPENCOMMIT_DESCRIPTION](value: any) {
51+
const parsedValue = typeof value === 'boolean' || value === 'true' ? value : 'false'
5152
validateConfig(
52-
CONFIG_KEYS.description,
53-
typeof value === 'boolean',
53+
CONFIG_KEYS.OPENCOMMIT_DESCRIPTION,
54+
typeof parsedValue === 'boolean',
5455
'Must be true or false'
5556
);
5657

57-
return value;
58+
return parsedValue;
5859
},
59-
[CONFIG_KEYS.emoji](value: any) {
60+
[CONFIG_KEYS.OPENCOMMIT_EMOJI](value: any) {
61+
const parsedValue = typeof value === 'boolean' || value === 'true' ? value : 'false'
6062
validateConfig(
61-
CONFIG_KEYS.emoji,
62-
typeof value === 'boolean',
63+
CONFIG_KEYS.OPENCOMMIT_EMOJI,
64+
typeof parsedValue === 'boolean',
6365
'Must be true or false'
6466
);
6567

66-
return value;
68+
return parsedValue;
6769
}
6870
};
6971

@@ -75,14 +77,13 @@ const configPath = pathJoin(homedir(), '.opencommit');
7577

7678
export const getConfig = (): ConfigType | null => {
7779
const configExists = existsSync(configPath);
78-
if (!configExists) return null;
7980

80-
const configFile = readFileSync(configPath, 'utf8');
81-
const config = iniParse(configFile);
81+
const configFile = configExists ? readFileSync(configPath, 'utf8') : '';
82+
const config = configFile? iniParse(configFile) : {};
8283

83-
for (const configKey of Object.keys(config)) {
84+
for (const configKey in CONFIG_KEYS) {
8485
const validValue = configValidators[configKey as CONFIG_KEYS](
85-
config[configKey]
86+
config.hasOwnProperty(configKey) ? config[configKey] : process.env[configKey]
8687
);
8788

8889
config[configKey] = validValue;

src/generateCommitMessageFromGitDiff.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
ChatCompletionRequestMessageRoleEnum
44
} from 'openai';
55
import { api } from './api';
6-
import { getConfig } from './commands/config';
6+
import { CONFIG_KEYS, getConfig } from './commands/config';
77
import { mergeStrings } from './utils/mergeStrings';
88

99
const config = getConfig();
@@ -12,11 +12,11 @@ const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
1212
{
1313
role: ChatCompletionRequestMessageRoleEnum.System,
1414
content: `You are to act as the author of a commit message in git. Your mission is to create clean and comprehensive commit messages in the conventional commit convention. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message. ${
15-
config?.emoji
15+
config?.[CONFIG_KEYS.OPENCOMMIT_EMOJI]
1616
? 'Use Gitmoji convention to preface the commit'
1717
: 'Do not preface the commit with anything'
1818
}, use the present tense. ${
19-
config?.description
19+
config?.[CONFIG_KEYS.OPENCOMMIT_DESCRIPTION]
2020
? 'Add a short description of what commit is about after the commit message. Don\'t start it with "This commit", just describe the changes.'
2121
: "Don't add any descriptions to the commit, only commit message."
2222
}`
@@ -49,9 +49,9 @@ const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
4949
{
5050
role: ChatCompletionRequestMessageRoleEnum.Assistant,
5151
// prettier-ignore
52-
content: `${config?.emoji ? '🐛 ' : ''}fix(server.ts): change port variable case from lowercase port to uppercase PORT
53-
${config?.emoji ? '✨ ' : ''}feat(server.ts): add support for process.env.PORT environment variable
54-
${config?.description ? 'The port variable is now named PORT, which improves consistency with the naming conventions as PORT is a constant. Support for an environment variable allows the application to be more flexible as it can now run on any available port specified via the process.env.PORT environment variable.' : ''}`
52+
content: `${config?.[CONFIG_KEYS.OPENCOMMIT_EMOJI] ? '🐛 ' : ''}fix(server.ts): change port variable case from lowercase port to uppercase PORT
53+
${config?.[CONFIG_KEYS.OPENCOMMIT_EMOJI] ? '✨ ' : ''}feat(server.ts): add support for process.env.PORT environment variable
54+
${config?.[CONFIG_KEYS.OPENCOMMIT_DESCRIPTION] ? 'The port variable is now named PORT, which improves consistency with the naming conventions as PORT is a constant. Support for an environment variable allows the application to be more flexible as it can now run on any available port specified via the process.env.PORT environment variable.' : ''}`
5555
}
5656
];
5757

0 commit comments

Comments
 (0)