mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-01-13 07:38:01 -05:00
Compare commits
24 Commits
378_fix_ho
...
refactorin
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
676c2b20c1 | ||
|
|
21da102f6d | ||
|
|
6ae7ce2720 | ||
|
|
e44610fea3 | ||
|
|
9dcb264420 | ||
|
|
dd7fdba94e | ||
|
|
5fa12e2d4a | ||
|
|
42a36492ad | ||
|
|
443d27fc8d | ||
|
|
04991dd00f | ||
|
|
3ded6062c1 | ||
|
|
f8584e7b78 | ||
|
|
94faceefd3 | ||
|
|
720cd6f9c1 | ||
|
|
b6a92d557f | ||
|
|
71354e4687 | ||
|
|
8f85ee8f8e | ||
|
|
f9103a3c6a | ||
|
|
4afd7de7a8 | ||
|
|
5cfa3cded2 | ||
|
|
bb0b0e804e | ||
|
|
5d87cc514b | ||
|
|
6f4e8fde93 | ||
|
|
745bb5218f |
17
README.md
17
README.md
@@ -76,8 +76,7 @@ oco config set OCO_AI_PROVIDER='ollama'
|
||||
If you want to use a model other than mistral (default), you can do so by setting the `OCO_AI_PROVIDER` environment variable as follows:
|
||||
|
||||
```sh
|
||||
oco config set OCO_AI_PROVIDER='ollama'
|
||||
oco config set OCO_MODEL='llama3:8b'
|
||||
oco config set OCO_AI_PROVIDER='ollama/llama3:8b'
|
||||
```
|
||||
|
||||
If you have ollama that is set up in docker/ on another machine with GPUs (not locally), you can change the default endpoint url.
|
||||
@@ -128,12 +127,12 @@ OCO_TOKENS_MAX_OUTPUT=<max response tokens (default: 500)>
|
||||
OCO_OPENAI_BASE_PATH=<may be used to set proxy path to OpenAI api>
|
||||
OCO_DESCRIPTION=<postface a message with ~3 sentences description of the changes>
|
||||
OCO_EMOJI=<boolean, add GitMoji>
|
||||
OCO_MODEL=<either 'gpt-4o', 'gpt-4', 'gpt-4-turbo', 'gpt-3.5-turbo' (default), 'gpt-3.5-turbo-0125', 'gpt-4-1106-preview', 'gpt-4-turbo-preview' or 'gpt-4-0125-preview' or any Anthropic or Ollama model or any string basically, but it should be a valid model name>
|
||||
OCO_MODEL=<either 'gpt-4o', 'gpt-4', 'gpt-4-turbo', 'gpt-3.5-turbo' (default), 'gpt-3.5-turbo-0125', 'gpt-4-1106-preview', 'gpt-4-turbo-preview' or 'gpt-4-0125-preview' or any string basically, but it should be a valid model name>
|
||||
OCO_LANGUAGE=<locale, scroll to the bottom to see options>
|
||||
OCO_MESSAGE_TEMPLATE_PLACEHOLDER=<message template placeholder, default: '$msg'>
|
||||
OCO_PROMPT_MODULE=<either conventional-commit or @commitlint, default: conventional-commit>
|
||||
OCO_ONE_LINE_COMMIT=<one line commit message, default: false>
|
||||
OCO_AI_PROVIDER=<openai (default), anthropic, azure, ollama>
|
||||
OCO_AI_PROVIDER=<openai (default), anthropic, azure, ollama or ollama/model>
|
||||
...
|
||||
```
|
||||
|
||||
@@ -163,16 +162,6 @@ oco config set OCO_EMOJI=false
|
||||
|
||||
Other config options are behaving the same.
|
||||
|
||||
### Output WHY the changes were done (WIP)
|
||||
|
||||
You can set the `OCO_WHY` config to `true` to have OpenCommit output a short description of WHY the changes were done after the commit message. Default is `false`.
|
||||
|
||||
To make this perform accurate we must store 'what files do' in some kind of an index or embedding and perform a lookup (kinda RAG) for the accurate git commit message. If you feel like building this comment on this ticket https://github.com/di-sukharev/opencommit/issues/398 and let's go from there together.
|
||||
|
||||
```sh
|
||||
oco config set OCO_WHY=true
|
||||
```
|
||||
|
||||
### Switch to GPT-4 or other models
|
||||
|
||||
By default, OpenCommit uses `gpt-4o-mini` model.
|
||||
|
||||
2393
out/cli.cjs
2393
out/cli.cjs
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "opencommit",
|
||||
"version": "3.1.2",
|
||||
"version": "3.0.20",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "opencommit",
|
||||
"version": "3.1.2",
|
||||
"version": "3.0.20",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.10.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "opencommit",
|
||||
"version": "3.1.2",
|
||||
"version": "3.0.20",
|
||||
"description": "Auto-generate impressive commits in 1 second. Killing lame commits with AI 🤯🔫",
|
||||
"keywords": [
|
||||
"git",
|
||||
|
||||
@@ -50,8 +50,8 @@ const generateCommitMessageFromGitDiff = async ({
|
||||
skipCommitConfirmation = false
|
||||
}: GenerateCommitMessageFromGitDiffParams): Promise<void> => {
|
||||
await assertGitRepo();
|
||||
const commitGenerationSpinner = spinner();
|
||||
commitGenerationSpinner.start('Generating the commit message');
|
||||
const commitSpinner = spinner();
|
||||
commitSpinner.start('Generating the commit message');
|
||||
|
||||
try {
|
||||
let commitMessage = await generateCommitMessageByDiff(
|
||||
@@ -73,7 +73,7 @@ const generateCommitMessageFromGitDiff = async ({
|
||||
);
|
||||
}
|
||||
|
||||
commitGenerationSpinner.stop('📝 Commit message generated');
|
||||
commitSpinner.stop('📝 Commit message generated');
|
||||
|
||||
outro(
|
||||
`Generated commit message:
|
||||
@@ -88,20 +88,15 @@ ${chalk.grey('——————————————————')}`
|
||||
message: 'Confirm the commit message?'
|
||||
}));
|
||||
|
||||
if (isCancel(isCommitConfirmedByUser)) process.exit(1);
|
||||
|
||||
if (isCommitConfirmedByUser) {
|
||||
const committingChangesSpinner = spinner();
|
||||
committingChangesSpinner.start('Committing the changes');
|
||||
if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) {
|
||||
const { stdout } = await execa('git', [
|
||||
'commit',
|
||||
'-m',
|
||||
commitMessage,
|
||||
...extraArgs
|
||||
]);
|
||||
committingChangesSpinner.stop(
|
||||
`${chalk.green('✔')} Successfully committed`
|
||||
);
|
||||
|
||||
outro(`${chalk.green('✔')} Successfully committed`);
|
||||
|
||||
outro(stdout);
|
||||
|
||||
@@ -118,9 +113,7 @@ ${chalk.grey('——————————————————')}`
|
||||
message: 'Do you want to run `git push`?'
|
||||
});
|
||||
|
||||
if (isCancel(isPushConfirmedByUser)) process.exit(1);
|
||||
|
||||
if (isPushConfirmedByUser) {
|
||||
if (isPushConfirmedByUser && !isCancel(isPushConfirmedByUser)) {
|
||||
const pushSpinner = spinner();
|
||||
|
||||
pushSpinner.start(`Running 'git push ${remotes[0]}'`);
|
||||
@@ -148,30 +141,28 @@ ${chalk.grey('——————————————————')}`
|
||||
options: remotes.map((remote) => ({ value: remote, label: remote }))
|
||||
})) as string;
|
||||
|
||||
if (isCancel(selectedRemote)) process.exit(1);
|
||||
if (!isCancel(selectedRemote)) {
|
||||
const pushSpinner = spinner();
|
||||
|
||||
const pushSpinner = spinner();
|
||||
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
|
||||
|
||||
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
|
||||
const { stdout } = await execa('git', ['push', selectedRemote]);
|
||||
|
||||
const { stdout } = await execa('git', ['push', selectedRemote]);
|
||||
pushSpinner.stop(
|
||||
`${chalk.green(
|
||||
'✔'
|
||||
)} Successfully pushed all commits to ${selectedRemote}`
|
||||
);
|
||||
|
||||
pushSpinner.stop(
|
||||
`${chalk.green(
|
||||
'✔'
|
||||
)} Successfully pushed all commits to ${selectedRemote}`
|
||||
);
|
||||
|
||||
if (stdout) outro(stdout);
|
||||
if (stdout) outro(stdout);
|
||||
} else outro(`${chalk.gray('✖')} process cancelled`);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
if (!isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) {
|
||||
const regenerateMessage = await confirm({
|
||||
message: 'Do you want to regenerate the message?'
|
||||
});
|
||||
|
||||
if (isCancel(regenerateMessage)) process.exit(1);
|
||||
|
||||
if (regenerateMessage) {
|
||||
if (regenerateMessage && !isCancel(isCommitConfirmedByUser)) {
|
||||
await generateCommitMessageFromGitDiff({
|
||||
diff,
|
||||
extraArgs,
|
||||
@@ -180,7 +171,7 @@ ${chalk.grey('——————————————————')}`
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
commitGenerationSpinner.stop('📝 Commit message generated');
|
||||
commitSpinner.stop('📝 Commit message generated');
|
||||
|
||||
const err = error as Error;
|
||||
outro(`${chalk.red('✖')} ${err?.message || err}`);
|
||||
@@ -228,9 +219,10 @@ export async function commit(
|
||||
message: 'Do you want to stage all files and generate commit message?'
|
||||
});
|
||||
|
||||
if (isCancel(isStageAllAndCommitConfirmedByUser)) process.exit(1);
|
||||
|
||||
if (isStageAllAndCommitConfirmedByUser) {
|
||||
if (
|
||||
isStageAllAndCommitConfirmedByUser &&
|
||||
!isCancel(isStageAllAndCommitConfirmedByUser)
|
||||
) {
|
||||
await commit(extraArgs, true, fullGitMojiSpec);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ export const commitlintConfigCommand = command(
|
||||
if (mode === CONFIG_MODES.get) {
|
||||
const commitLintConfig = await getCommitlintLLMConfig();
|
||||
|
||||
outro(JSON.stringify(commitLintConfig, null, 2));
|
||||
outro(commitLintConfig.toString());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ export enum CONFIG_KEYS {
|
||||
OCO_EMOJI = 'OCO_EMOJI',
|
||||
OCO_MODEL = 'OCO_MODEL',
|
||||
OCO_LANGUAGE = 'OCO_LANGUAGE',
|
||||
OCO_WHY = 'OCO_WHY',
|
||||
OCO_MESSAGE_TEMPLATE_PLACEHOLDER = 'OCO_MESSAGE_TEMPLATE_PLACEHOLDER',
|
||||
OCO_PROMPT_MODULE = 'OCO_PROMPT_MODULE',
|
||||
OCO_AI_PROVIDER = 'OCO_AI_PROVIDER',
|
||||
@@ -377,7 +376,6 @@ export type ConfigType = {
|
||||
[CONFIG_KEYS.OCO_OPENAI_BASE_PATH]?: string;
|
||||
[CONFIG_KEYS.OCO_DESCRIPTION]: boolean;
|
||||
[CONFIG_KEYS.OCO_EMOJI]: boolean;
|
||||
[CONFIG_KEYS.OCO_WHY]: boolean;
|
||||
[CONFIG_KEYS.OCO_MODEL]: string;
|
||||
[CONFIG_KEYS.OCO_LANGUAGE]: string;
|
||||
[CONFIG_KEYS.OCO_MESSAGE_TEMPLATE_PLACEHOLDER]: string;
|
||||
@@ -424,26 +422,25 @@ enum OCO_PROMPT_MODULE_ENUM {
|
||||
COMMITLINT = '@commitlint'
|
||||
}
|
||||
|
||||
export const DEFAULT_CONFIG = {
|
||||
OCO_TOKENS_MAX_INPUT: DEFAULT_TOKEN_LIMITS.DEFAULT_MAX_TOKENS_INPUT,
|
||||
OCO_TOKENS_MAX_OUTPUT: DEFAULT_TOKEN_LIMITS.DEFAULT_MAX_TOKENS_OUTPUT,
|
||||
OCO_DESCRIPTION: false,
|
||||
OCO_EMOJI: false,
|
||||
OCO_MODEL: getDefaultModel('openai'),
|
||||
OCO_LANGUAGE: 'en',
|
||||
OCO_MESSAGE_TEMPLATE_PLACEHOLDER: '$msg',
|
||||
OCO_PROMPT_MODULE: OCO_PROMPT_MODULE_ENUM.CONVENTIONAL_COMMIT,
|
||||
OCO_AI_PROVIDER: OCO_AI_PROVIDER_ENUM.OPENAI,
|
||||
OCO_ONE_LINE_COMMIT: false,
|
||||
OCO_TEST_MOCK_TYPE: 'commit-message',
|
||||
OCO_FLOWISE_ENDPOINT: ':',
|
||||
OCO_WHY: false,
|
||||
OCO_GITPUSH: true // todo: deprecate
|
||||
};
|
||||
const initGlobalConfig = () => {
|
||||
const defaultConfig = {
|
||||
OCO_TOKENS_MAX_INPUT: DEFAULT_TOKEN_LIMITS.DEFAULT_MAX_TOKENS_INPUT,
|
||||
OCO_TOKENS_MAX_OUTPUT: DEFAULT_TOKEN_LIMITS.DEFAULT_MAX_TOKENS_OUTPUT,
|
||||
OCO_DESCRIPTION: false,
|
||||
OCO_EMOJI: false,
|
||||
OCO_MODEL: getDefaultModel('openai'),
|
||||
OCO_LANGUAGE: 'en',
|
||||
OCO_MESSAGE_TEMPLATE_PLACEHOLDER: '$msg',
|
||||
OCO_PROMPT_MODULE: OCO_PROMPT_MODULE_ENUM.CONVENTIONAL_COMMIT,
|
||||
OCO_AI_PROVIDER: OCO_AI_PROVIDER_ENUM.OPENAI,
|
||||
OCO_ONE_LINE_COMMIT: false,
|
||||
OCO_TEST_MOCK_TYPE: 'commit-message',
|
||||
OCO_FLOWISE_ENDPOINT: ':',
|
||||
OCO_GITPUSH: true // todo: deprecate
|
||||
};
|
||||
|
||||
const initGlobalConfig = (configPath: string = defaultConfigPath) => {
|
||||
writeFileSync(configPath, iniStringify(DEFAULT_CONFIG), 'utf8');
|
||||
return DEFAULT_CONFIG;
|
||||
writeFileSync(defaultConfigPath, iniStringify(defaultConfig), 'utf8');
|
||||
return defaultConfig;
|
||||
};
|
||||
|
||||
const parseEnvVarValue = (value?: any) => {
|
||||
@@ -454,10 +451,16 @@ const parseEnvVarValue = (value?: any) => {
|
||||
}
|
||||
};
|
||||
|
||||
const getEnvConfig = (envPath: string) => {
|
||||
export const getConfig = ({
|
||||
configPath = defaultConfigPath,
|
||||
envPath = defaultEnvPath
|
||||
}: {
|
||||
configPath?: string;
|
||||
envPath?: string;
|
||||
} = {}): ConfigType => {
|
||||
dotenv.config({ path: envPath });
|
||||
|
||||
return {
|
||||
const envConfig = {
|
||||
OCO_MODEL: process.env.OCO_MODEL,
|
||||
|
||||
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
|
||||
@@ -488,59 +491,33 @@ const getEnvConfig = (envPath: string) => {
|
||||
|
||||
OCO_GITPUSH: parseEnvVarValue(process.env.OCO_GITPUSH) // todo: deprecate
|
||||
};
|
||||
};
|
||||
|
||||
const getGlobalConfig = (configPath: string) => {
|
||||
let globalConfig: ConfigType;
|
||||
|
||||
const isGlobalConfigFileExist = existsSync(configPath);
|
||||
if (!isGlobalConfigFileExist) globalConfig = initGlobalConfig(configPath);
|
||||
if (!isGlobalConfigFileExist) globalConfig = initGlobalConfig();
|
||||
else {
|
||||
const configFile = readFileSync(configPath, 'utf8');
|
||||
globalConfig = iniParse(configFile) as ConfigType;
|
||||
}
|
||||
|
||||
return globalConfig;
|
||||
};
|
||||
const mergeObjects = (main: Partial<ConfigType>, fallback: ConfigType) =>
|
||||
Object.keys(CONFIG_KEYS).reduce((acc, key) => {
|
||||
acc[key] = parseEnvVarValue(main[key] ?? fallback[key]);
|
||||
|
||||
/**
|
||||
* Merges two configs.
|
||||
* Env config takes precedence over global ~/.opencommit config file
|
||||
* @param main - env config
|
||||
* @param fallback - global ~/.opencommit config file
|
||||
* @returns merged config
|
||||
*/
|
||||
const mergeConfigs = (main: Partial<ConfigType>, fallback: ConfigType) =>
|
||||
Object.keys(CONFIG_KEYS).reduce((acc, key) => {
|
||||
acc[key] = parseEnvVarValue(main[key] ?? fallback[key]);
|
||||
return acc;
|
||||
}, {} as ConfigType);
|
||||
|
||||
return acc;
|
||||
}, {} as ConfigType);
|
||||
|
||||
interface GetConfigOptions {
|
||||
globalPath?: string;
|
||||
envPath?: string;
|
||||
}
|
||||
|
||||
export const getConfig = ({
|
||||
envPath = defaultEnvPath,
|
||||
globalPath = defaultConfigPath
|
||||
}: GetConfigOptions = {}): ConfigType => {
|
||||
const envConfig = getEnvConfig(envPath);
|
||||
const globalConfig = getGlobalConfig(globalPath);
|
||||
|
||||
const config = mergeConfigs(envConfig, globalConfig);
|
||||
// env config takes precedence over global ~/.opencommit config file
|
||||
const config = mergeObjects(envConfig, globalConfig);
|
||||
|
||||
return config;
|
||||
};
|
||||
|
||||
export const setConfig = (
|
||||
keyValues: [key: string, value: string][],
|
||||
globalConfigPath: string = defaultConfigPath
|
||||
configPath: string = defaultConfigPath
|
||||
) => {
|
||||
const config = getConfig({
|
||||
globalPath: globalConfigPath
|
||||
});
|
||||
const config = getConfig();
|
||||
|
||||
for (let [key, value] of keyValues) {
|
||||
if (!configValidators.hasOwnProperty(key)) {
|
||||
@@ -566,7 +543,9 @@ export const setConfig = (
|
||||
config[key] = validValue;
|
||||
}
|
||||
|
||||
writeFileSync(globalConfigPath, iniStringify(config), 'utf8');
|
||||
writeFileSync(configPath, iniStringify(config), 'utf8');
|
||||
|
||||
assertConfigsAreValid(config);
|
||||
|
||||
outro(`${chalk.green('✔')} config successfully set`);
|
||||
};
|
||||
|
||||
@@ -44,10 +44,9 @@ export const prepareCommitMessageHook = async (
|
||||
!config.OCO_ANTHROPIC_API_KEY &&
|
||||
!config.OCO_AZURE_API_KEY
|
||||
) {
|
||||
outro(
|
||||
'No OCO_OPENAI_API_KEY or OCO_ANTHROPIC_API_KEY or OCO_AZURE_API_KEY exists. Set your key via `oco config set <key>=<value>, e.g. `oco config set OCO_OPENAI_API_KEY=<value>`. For more info see https://github.com/di-sukharev/opencommit'
|
||||
throw new Error(
|
||||
'No OPEN_AI_API or OCO_ANTHROPIC_API_KEY or OCO_AZURE_API_KEY exists. Set your key in ~/.opencommit'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const spin = spinner();
|
||||
|
||||
@@ -28,10 +28,7 @@ export class OllamaAi implements AiEngine {
|
||||
stream: false
|
||||
};
|
||||
try {
|
||||
const response = await this.client.post(
|
||||
this.client.getUri(this.config),
|
||||
params
|
||||
);
|
||||
const response = await this.client.post('', params);
|
||||
|
||||
const message = response.data.message;
|
||||
|
||||
|
||||
@@ -258,9 +258,7 @@ const INIT_MAIN_PROMPT = (
|
||||
prompts: string[]
|
||||
): OpenAI.Chat.Completions.ChatCompletionMessageParam => ({
|
||||
role: 'system',
|
||||
content: `${IDENTITY} Your mission is to create clean and comprehensive commit messages in the given @commitlint convention and explain WHAT were the changes ${
|
||||
config.OCO_WHY ? 'and WHY the changes were done' : ''
|
||||
}. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message.
|
||||
content: `${IDENTITY} Your mission is to create clean and comprehensive commit messages in the given @commitlint convention and explain WHAT were the changes and WHY the changes were done. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message.
|
||||
${
|
||||
config.OCO_EMOJI
|
||||
? 'Use GitMoji convention to preface the commit.'
|
||||
|
||||
@@ -1,29 +1,13 @@
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
const findModulePath = (moduleName: string) => {
|
||||
const searchPaths = [
|
||||
path.join('node_modules', moduleName),
|
||||
path.join('node_modules', '.pnpm')
|
||||
];
|
||||
|
||||
for (const basePath of searchPaths) {
|
||||
try {
|
||||
const resolvedPath = require.resolve(moduleName, { paths: [basePath] });
|
||||
return resolvedPath;
|
||||
} catch {
|
||||
// Continue to the next search path if the module is not found
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`Cannot find module ${moduleName}`);
|
||||
};
|
||||
|
||||
const getCommitLintModuleType = async (): Promise<'cjs' | 'esm'> => {
|
||||
const packageFile = '@commitlint/load/package.json';
|
||||
const packageJsonPath = findModulePath(packageFile);
|
||||
const packageFile = 'node_modules/@commitlint/load/package.json';
|
||||
const packageJsonPath = path.join(
|
||||
process.env.PWD || process.cwd(),
|
||||
packageFile,
|
||||
);
|
||||
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
|
||||
|
||||
if (!packageJson) {
|
||||
throw new Error(`Failed to parse ${packageFile}`);
|
||||
}
|
||||
@@ -35,7 +19,7 @@ const getCommitLintModuleType = async (): Promise<'cjs' | 'esm'> => {
|
||||
* QualifiedConfig from any version of @commitlint/types
|
||||
* @see https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/types/src/load.ts
|
||||
*/
|
||||
type QualifiedConfigOnAnyVersion = { [key: string]: unknown };
|
||||
type QualifiedConfigOnAnyVersion = { [key:string]: unknown };
|
||||
|
||||
/**
|
||||
* This code is loading the configuration for the `@commitlint` package from the current working
|
||||
@@ -43,31 +27,36 @@ type QualifiedConfigOnAnyVersion = { [key: string]: unknown };
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
export const getCommitLintPWDConfig =
|
||||
async (): Promise<QualifiedConfigOnAnyVersion | null> => {
|
||||
let load: Function, modulePath: string;
|
||||
switch (await getCommitLintModuleType()) {
|
||||
case 'cjs':
|
||||
/**
|
||||
* CommonJS (<= commitlint@v18.x.x.)
|
||||
*/
|
||||
modulePath = findModulePath('@commitlint/load');
|
||||
load = require(modulePath).default;
|
||||
break;
|
||||
case 'esm':
|
||||
/**
|
||||
* ES Module (commitlint@v19.x.x. <= )
|
||||
* Directory import is not supported in ES Module resolution, so import the file directly
|
||||
*/
|
||||
modulePath = await findModulePath('@commitlint/load/lib/load.js');
|
||||
load = (await import(modulePath)).default;
|
||||
break;
|
||||
}
|
||||
export const getCommitLintPWDConfig = async (): Promise<QualifiedConfigOnAnyVersion | null> => {
|
||||
let load, nodeModulesPath;
|
||||
switch (await getCommitLintModuleType()) {
|
||||
case 'cjs':
|
||||
/**
|
||||
* CommonJS (<= commitlint@v18.x.x.)
|
||||
*/
|
||||
nodeModulesPath = path.join(
|
||||
process.env.PWD || process.cwd(),
|
||||
'node_modules/@commitlint/load',
|
||||
);
|
||||
load = require(nodeModulesPath).default;
|
||||
break;
|
||||
case 'esm':
|
||||
/**
|
||||
* ES Module (commitlint@v19.x.x. <= )
|
||||
* Directory import is not supported in ES Module resolution, so import the file directly
|
||||
*/
|
||||
nodeModulesPath = path.join(
|
||||
process.env.PWD || process.cwd(),
|
||||
'node_modules/@commitlint/load/lib/load.js',
|
||||
);
|
||||
load = (await import(nodeModulesPath)).default;
|
||||
break;
|
||||
}
|
||||
|
||||
if (load && typeof load === 'function') {
|
||||
return await load();
|
||||
}
|
||||
if (load && typeof load === 'function') {
|
||||
return await load();
|
||||
}
|
||||
|
||||
// @commitlint/load is not a function
|
||||
return null;
|
||||
};
|
||||
// @commitlint/load is not a function
|
||||
return null;
|
||||
};
|
||||
|
||||
@@ -181,7 +181,9 @@ describe('cli flow to generate commit message using @commitlint prompt-module',
|
||||
[],
|
||||
{ cwd: gitDir }
|
||||
);
|
||||
expect(await commitlintGet.findByText('consistency')).toBeInTheConsole();
|
||||
expect(
|
||||
await commitlintGet.findByText('[object Object]')
|
||||
).toBeInTheConsole();
|
||||
|
||||
// Run 'oco' using .opencommit-commitlint
|
||||
await render('echo', [`'console.log("Hello World");' > index.ts`], {
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
import { existsSync, readFileSync, rmSync } from 'fs';
|
||||
import {
|
||||
DEFAULT_CONFIG,
|
||||
getConfig,
|
||||
setConfig
|
||||
} from '../../src/commands/config';
|
||||
import { getConfig } from '../../src/commands/config';
|
||||
import { prepareFile } from './utils';
|
||||
import { dirname } from 'path';
|
||||
|
||||
describe('config', () => {
|
||||
describe('getConfig', () => {
|
||||
const originalEnv = { ...process.env };
|
||||
let globalConfigFile: { filePath: string; cleanup: () => Promise<void> };
|
||||
let envConfigFile: { filePath: string; cleanup: () => Promise<void> };
|
||||
let localEnvFile: { filePath: string; cleanup: () => Promise<void> };
|
||||
|
||||
function resetEnv(env: NodeJS.ProcessEnv) {
|
||||
Object.keys(process.env).forEach((key) => {
|
||||
@@ -25,12 +19,7 @@ describe('config', () => {
|
||||
beforeEach(async () => {
|
||||
resetEnv(originalEnv);
|
||||
if (globalConfigFile) await globalConfigFile.cleanup();
|
||||
if (envConfigFile) await envConfigFile.cleanup();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
if (globalConfigFile) await globalConfigFile.cleanup();
|
||||
if (envConfigFile) await envConfigFile.cleanup();
|
||||
if (localEnvFile) await localEnvFile.cleanup();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
@@ -47,249 +36,115 @@ describe('config', () => {
|
||||
return await prepareFile(fileName, fileContent);
|
||||
};
|
||||
|
||||
describe('getConfig', () => {
|
||||
it('should prioritize local .env over global .opencommit config', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_OPENAI_API_KEY: 'global-key',
|
||||
OCO_MODEL: 'gpt-3.5-turbo',
|
||||
OCO_LANGUAGE: 'en'
|
||||
});
|
||||
|
||||
envConfigFile = await generateConfig('.env', {
|
||||
OCO_OPENAI_API_KEY: 'local-key',
|
||||
OCO_ANTHROPIC_API_KEY: 'local-anthropic-key',
|
||||
OCO_LANGUAGE: 'fr'
|
||||
});
|
||||
|
||||
const config = getConfig({
|
||||
globalPath: globalConfigFile.filePath,
|
||||
envPath: envConfigFile.filePath
|
||||
});
|
||||
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual('local-key');
|
||||
expect(config.OCO_MODEL).toEqual('gpt-3.5-turbo');
|
||||
expect(config.OCO_LANGUAGE).toEqual('fr');
|
||||
expect(config.OCO_ANTHROPIC_API_KEY).toEqual('local-anthropic-key');
|
||||
it('should prioritize local .env over global .opencommit config', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_OPENAI_API_KEY: 'global-key',
|
||||
OCO_MODEL: 'gpt-3.5-turbo',
|
||||
OCO_LANGUAGE: 'en'
|
||||
});
|
||||
|
||||
it('should fallback to global config when local config is not set', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_OPENAI_API_KEY: 'global-key',
|
||||
OCO_MODEL: 'gpt-4',
|
||||
OCO_LANGUAGE: 'de',
|
||||
OCO_DESCRIPTION: 'true'
|
||||
});
|
||||
|
||||
envConfigFile = await generateConfig('.env', {
|
||||
OCO_ANTHROPIC_API_KEY: 'local-anthropic-key'
|
||||
});
|
||||
|
||||
const config = getConfig({
|
||||
globalPath: globalConfigFile.filePath,
|
||||
envPath: envConfigFile.filePath
|
||||
});
|
||||
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual('global-key');
|
||||
expect(config.OCO_ANTHROPIC_API_KEY).toEqual('local-anthropic-key');
|
||||
expect(config.OCO_MODEL).toEqual('gpt-4');
|
||||
expect(config.OCO_LANGUAGE).toEqual('de');
|
||||
expect(config.OCO_DESCRIPTION).toEqual(true);
|
||||
localEnvFile = await generateConfig('.env', {
|
||||
OCO_OPENAI_API_KEY: 'local-key',
|
||||
OCO_ANTHROPIC_API_KEY: 'local-anthropic-key',
|
||||
OCO_LANGUAGE: 'fr'
|
||||
});
|
||||
|
||||
it('should handle boolean and numeric values correctly', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_TOKENS_MAX_INPUT: '4096',
|
||||
OCO_TOKENS_MAX_OUTPUT: '500',
|
||||
OCO_GITPUSH: 'true'
|
||||
});
|
||||
|
||||
envConfigFile = await generateConfig('.env', {
|
||||
OCO_TOKENS_MAX_INPUT: '8192',
|
||||
OCO_ONE_LINE_COMMIT: 'false'
|
||||
});
|
||||
|
||||
const config = getConfig({
|
||||
globalPath: globalConfigFile.filePath,
|
||||
envPath: envConfigFile.filePath
|
||||
});
|
||||
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_TOKENS_MAX_INPUT).toEqual(8192);
|
||||
expect(config.OCO_TOKENS_MAX_OUTPUT).toEqual(500);
|
||||
expect(config.OCO_GITPUSH).toEqual(true);
|
||||
expect(config.OCO_ONE_LINE_COMMIT).toEqual(false);
|
||||
const config = getConfig({
|
||||
configPath: globalConfigFile.filePath,
|
||||
envPath: localEnvFile.filePath
|
||||
});
|
||||
|
||||
it('should handle empty local config correctly', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_OPENAI_API_KEY: 'global-key',
|
||||
OCO_MODEL: 'gpt-4',
|
||||
OCO_LANGUAGE: 'es'
|
||||
});
|
||||
|
||||
envConfigFile = await generateConfig('.env', {});
|
||||
|
||||
const config = getConfig({
|
||||
globalPath: globalConfigFile.filePath,
|
||||
envPath: envConfigFile.filePath
|
||||
});
|
||||
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual('global-key');
|
||||
expect(config.OCO_MODEL).toEqual('gpt-4');
|
||||
expect(config.OCO_LANGUAGE).toEqual('es');
|
||||
});
|
||||
|
||||
it('should override global config with null values in local .env', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_OPENAI_API_KEY: 'global-key',
|
||||
OCO_MODEL: 'gpt-4',
|
||||
OCO_LANGUAGE: 'es'
|
||||
});
|
||||
|
||||
envConfigFile = await generateConfig('.env', {
|
||||
OCO_OPENAI_API_KEY: 'null'
|
||||
});
|
||||
|
||||
const config = getConfig({
|
||||
globalPath: globalConfigFile.filePath,
|
||||
envPath: envConfigFile.filePath
|
||||
});
|
||||
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual(null);
|
||||
});
|
||||
|
||||
it('should handle empty global config', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {});
|
||||
envConfigFile = await generateConfig('.env', {});
|
||||
|
||||
const config = getConfig({
|
||||
globalPath: globalConfigFile.filePath,
|
||||
envPath: envConfigFile.filePath
|
||||
});
|
||||
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual(undefined);
|
||||
});
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual('local-key');
|
||||
expect(config.OCO_MODEL).toEqual('gpt-3.5-turbo');
|
||||
expect(config.OCO_LANGUAGE).toEqual('fr');
|
||||
expect(config.OCO_ANTHROPIC_API_KEY).toEqual('local-anthropic-key');
|
||||
});
|
||||
|
||||
describe('setConfig', () => {
|
||||
beforeEach(async () => {
|
||||
// we create and delete the file to have the parent directory, but not the file, to test the creation of the file
|
||||
globalConfigFile = await generateConfig('.opencommit', {});
|
||||
rmSync(globalConfigFile.filePath);
|
||||
it('should fallback to global config when local config is not set', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_OPENAI_API_KEY: 'global-key',
|
||||
OCO_MODEL: 'gpt-4',
|
||||
OCO_LANGUAGE: 'de',
|
||||
OCO_DESCRIPTION: 'true'
|
||||
});
|
||||
|
||||
it('should create .opencommit file with DEFAULT CONFIG if it does not exist on first setConfig run', async () => {
|
||||
const isGlobalConfigFileExist = existsSync(globalConfigFile.filePath);
|
||||
expect(isGlobalConfigFileExist).toBe(false);
|
||||
|
||||
await setConfig(
|
||||
[['OCO_OPENAI_API_KEY', 'persisted-key_1']],
|
||||
globalConfigFile.filePath
|
||||
);
|
||||
|
||||
const fileContent = readFileSync(globalConfigFile.filePath, 'utf8');
|
||||
expect(fileContent).toContain('OCO_OPENAI_API_KEY=persisted-key_1');
|
||||
Object.entries(DEFAULT_CONFIG).forEach(([key, value]) => {
|
||||
expect(fileContent).toContain(`${key}=${value}`);
|
||||
});
|
||||
localEnvFile = await generateConfig('.env', {
|
||||
OCO_ANTHROPIC_API_KEY: 'local-anthropic-key'
|
||||
});
|
||||
|
||||
it('should set new config values', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {});
|
||||
await setConfig(
|
||||
[
|
||||
['OCO_OPENAI_API_KEY', 'new-key'],
|
||||
['OCO_MODEL', 'gpt-4']
|
||||
],
|
||||
globalConfigFile.filePath
|
||||
);
|
||||
|
||||
const config = getConfig({ globalPath: globalConfigFile.filePath });
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual('new-key');
|
||||
expect(config.OCO_MODEL).toEqual('gpt-4');
|
||||
const config = getConfig({
|
||||
configPath: globalConfigFile.filePath,
|
||||
envPath: localEnvFile.filePath
|
||||
});
|
||||
|
||||
it('should update existing config values', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_OPENAI_API_KEY: 'initial-key'
|
||||
});
|
||||
await setConfig(
|
||||
[['OCO_OPENAI_API_KEY', 'updated-key']],
|
||||
globalConfigFile.filePath
|
||||
);
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual('global-key');
|
||||
expect(config.OCO_ANTHROPIC_API_KEY).toEqual('local-anthropic-key');
|
||||
expect(config.OCO_MODEL).toEqual('gpt-4');
|
||||
expect(config.OCO_LANGUAGE).toEqual('de');
|
||||
expect(config.OCO_DESCRIPTION).toEqual(true);
|
||||
});
|
||||
|
||||
const config = getConfig({ globalPath: globalConfigFile.filePath });
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual('updated-key');
|
||||
it('should handle boolean and numeric values correctly', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_TOKENS_MAX_INPUT: '4096',
|
||||
OCO_TOKENS_MAX_OUTPUT: '500',
|
||||
OCO_GITPUSH: 'true'
|
||||
});
|
||||
|
||||
it('should handle boolean and numeric values correctly', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {});
|
||||
await setConfig(
|
||||
[
|
||||
['OCO_TOKENS_MAX_INPUT', '8192'],
|
||||
['OCO_DESCRIPTION', 'true'],
|
||||
['OCO_ONE_LINE_COMMIT', 'false']
|
||||
],
|
||||
globalConfigFile.filePath
|
||||
);
|
||||
|
||||
const config = getConfig({ globalPath: globalConfigFile.filePath });
|
||||
expect(config.OCO_TOKENS_MAX_INPUT).toEqual(8192);
|
||||
expect(config.OCO_DESCRIPTION).toEqual(true);
|
||||
expect(config.OCO_ONE_LINE_COMMIT).toEqual(false);
|
||||
localEnvFile = await generateConfig('.env', {
|
||||
OCO_TOKENS_MAX_INPUT: '8192',
|
||||
OCO_ONE_LINE_COMMIT: 'false'
|
||||
});
|
||||
|
||||
it('should throw an error for unsupported config keys', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {});
|
||||
|
||||
try {
|
||||
await setConfig(
|
||||
[['UNSUPPORTED_KEY', 'value']],
|
||||
globalConfigFile.filePath
|
||||
);
|
||||
throw new Error('NEVER_REACHED');
|
||||
} catch (error) {
|
||||
expect(error.message).toContain(
|
||||
'Unsupported config key: UNSUPPORTED_KEY'
|
||||
);
|
||||
expect(error.message).not.toContain('NEVER_REACHED');
|
||||
}
|
||||
const config = getConfig({
|
||||
configPath: globalConfigFile.filePath,
|
||||
envPath: localEnvFile.filePath
|
||||
});
|
||||
|
||||
it('should persist changes to the config file', async () => {
|
||||
const isGlobalConfigFileExist = existsSync(globalConfigFile.filePath);
|
||||
expect(isGlobalConfigFileExist).toBe(false);
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_TOKENS_MAX_INPUT).toEqual(8192);
|
||||
expect(config.OCO_TOKENS_MAX_OUTPUT).toEqual(500);
|
||||
expect(config.OCO_GITPUSH).toEqual(true);
|
||||
expect(config.OCO_ONE_LINE_COMMIT).toEqual(false);
|
||||
});
|
||||
|
||||
await setConfig(
|
||||
[['OCO_OPENAI_API_KEY', 'persisted-key']],
|
||||
globalConfigFile.filePath
|
||||
);
|
||||
|
||||
const fileContent = readFileSync(globalConfigFile.filePath, 'utf8');
|
||||
expect(fileContent).toContain('OCO_OPENAI_API_KEY=persisted-key');
|
||||
it('should handle empty local config correctly', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_OPENAI_API_KEY: 'global-key',
|
||||
OCO_MODEL: 'gpt-4',
|
||||
OCO_LANGUAGE: 'es'
|
||||
});
|
||||
|
||||
it('should set multiple configs in a row and keep the changes', async () => {
|
||||
const isGlobalConfigFileExist = existsSync(globalConfigFile.filePath);
|
||||
expect(isGlobalConfigFileExist).toBe(false);
|
||||
localEnvFile = await generateConfig('.env', {});
|
||||
|
||||
await setConfig(
|
||||
[['OCO_OPENAI_API_KEY', 'persisted-key']],
|
||||
globalConfigFile.filePath
|
||||
);
|
||||
|
||||
const fileContent1 = readFileSync(globalConfigFile.filePath, 'utf8');
|
||||
expect(fileContent1).toContain('OCO_OPENAI_API_KEY=persisted-key');
|
||||
|
||||
await setConfig([['OCO_MODEL', 'gpt-4']], globalConfigFile.filePath);
|
||||
|
||||
const fileContent2 = readFileSync(globalConfigFile.filePath, 'utf8');
|
||||
expect(fileContent2).toContain('OCO_MODEL=gpt-4');
|
||||
const config = getConfig({
|
||||
configPath: globalConfigFile.filePath,
|
||||
envPath: localEnvFile.filePath
|
||||
});
|
||||
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual('global-key');
|
||||
expect(config.OCO_MODEL).toEqual('gpt-4');
|
||||
expect(config.OCO_LANGUAGE).toEqual('es');
|
||||
});
|
||||
|
||||
it('should override global config with null values in local .env', async () => {
|
||||
globalConfigFile = await generateConfig('.opencommit', {
|
||||
OCO_OPENAI_API_KEY: 'global-key',
|
||||
OCO_MODEL: 'gpt-4',
|
||||
OCO_LANGUAGE: 'es'
|
||||
});
|
||||
|
||||
localEnvFile = await generateConfig('.env', { OCO_OPENAI_API_KEY: 'null' });
|
||||
|
||||
const config = getConfig({
|
||||
configPath: globalConfigFile.filePath,
|
||||
envPath: localEnvFile.filePath
|
||||
});
|
||||
|
||||
expect(config).not.toEqual(null);
|
||||
expect(config.OCO_OPENAI_API_KEY).toEqual(null);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { existsSync, mkdtemp, rm, writeFile } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import path from 'path';
|
||||
import { mkdtemp, rm, writeFile } from 'fs';
|
||||
import { promisify } from 'util';
|
||||
import { tmpdir } from 'os';
|
||||
const fsMakeTempDir = promisify(mkdtemp);
|
||||
const fsRemove = promisify(rm);
|
||||
const fsWriteFile = promisify(writeFile);
|
||||
@@ -20,9 +20,7 @@ export async function prepareFile(
|
||||
const filePath = path.resolve(tempDir, fileName);
|
||||
await fsWriteFile(filePath, content);
|
||||
const cleanup = async () => {
|
||||
if (existsSync(tempDir)) {
|
||||
await fsRemove(tempDir, { recursive: true });
|
||||
}
|
||||
return fsRemove(tempDir, { recursive: true });
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"lib": ["ES6", "ES2020"],
|
||||
|
||||
"module": "CommonJS",
|
||||
|
||||
|
||||
"resolveJsonModule": true,
|
||||
"moduleResolution": "Node",
|
||||
|
||||
@@ -21,7 +21,9 @@
|
||||
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["test/jest-setup.ts"],
|
||||
"include": [
|
||||
"test/jest-setup.ts"
|
||||
],
|
||||
"exclude": ["node_modules"],
|
||||
"ts-node": {
|
||||
"esm": true,
|
||||
|
||||
Reference in New Issue
Block a user