Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for commit prefix #160

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow For Prefix: fix(config.ts): fix typo in OCO_PREFIX environment …
…variable name

feat(config.ts): add validation for OCO_PREFIX environment variable to ensure it is a valid string or regex pattern
  • Loading branch information
amitayk committed May 27, 2023
commit dae0408bc40267196633ce1b0fd47b74259e38b2
42 changes: 38 additions & 4 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -128,11 +128,11 @@ export const configValidators = {
[CONFIG_KEYS.OCO_PREFIX](value: any) {
validateConfig(
CONFIG_KEYS.OCO_PREFIX,
true,
'Cannot be empty'
validatePrefix(value),
'Invalid Prefix'
);
return value;
}
}

};

@@ -150,7 +150,8 @@ export const getConfig = (): ConfigType | null => {
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === 'true' ? true : false,
OCO_EMOJI: process.env.OCO_EMOJI === 'true' ? true : false,
OCO_MODEL: process.env.OCO_MODEL || 'gpt-3.5-turbo',
OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en'
OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en',
OCO_PREFIX: process.env.OCO_PREFEIX || ''
};

const configExists = existsSync(configPath);
@@ -241,3 +242,36 @@ export const configCommand = command(
}
}
);


function validatePrefix(prefix: any): boolean {

if (typeof prefix !== 'string') {
outro('Prefix is not a string.');
return false;
}

const isTryingToBeRegex = prefix.match(/^\/.*\/.*|^\^(\(.*\))$/)

if (!isTryingToBeRegex) {
return true
}

if (!isRegex(prefix)) {
outro('This Regex format is not supported, please use the classic /*/ foramt')
return false;
}

return true;
}

function isRegex(str: string): boolean {
try {
new RegExp(str);
} catch(e) {
if (e instanceof SyntaxError) {
return false;
}
}
return true;
}