Skip to content

Commit

Permalink
390 add config set tests (#399)
Browse files Browse the repository at this point in the history
* fix(commit.ts): improve user confirmation handling by exiting on cancel actions to prevent unintended behavior
refactor(commit.ts): streamline conditional checks for user confirmations to enhance code readability and maintainability

* refactor(commit.ts): rename spinner variables for clarity and consistency in commit message generation process
fix(commit.ts): ensure proper stopping of spinners in case of errors during commit message generation and committing process

* refactor(config.ts): extract default configuration to a constant for better maintainability and readability
refactor(config.ts): improve initGlobalConfig function to accept a configPath parameter for flexibility
feat(config.ts): enhance getConfig function to support separate paths for global and environment configurations
test(config.test.ts): update tests to reflect changes in config handling and ensure proper functionality
style(utils.ts): clean up code formatting for consistency and readability
style(tsconfig.json): adjust formatting in tsconfig.json for better clarity and maintainability

* fix(utils.ts): add existsSync check before removing temp directory to prevent errors if directory does not exist (#401)

---------

Co-authored-by: Takanori Matsumoto <[email protected]>
  • Loading branch information
di-sukharev and matscube authored Sep 1, 2024
1 parent 60597d2 commit 8702c17
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 301 deletions.
170 changes: 84 additions & 86 deletions out/cli.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -25193,7 +25193,7 @@ function G3(t2, e3) {
// package.json
var package_default = {
name: "opencommit",
version: "3.0.20",
version: "3.1.0",
description: "Auto-generate impressive commits in 1 second. Killing lame commits with AI \u{1F92F}\u{1F52B}",
keywords: [
"git",
Expand Down Expand Up @@ -28029,44 +28029,24 @@ var configValidators = {
};
var defaultConfigPath = (0, import_path.join)((0, import_os.homedir)(), ".opencommit");
var defaultEnvPath = (0, import_path.resolve)(process.cwd(), ".env");
var assertConfigsAreValid = (config7) => {
for (const [key, value] of Object.entries(config7)) {
if (!value)
continue;
if (typeof value === "string" && ["null", "undefined"].includes(value)) {
config7[key] = void 0;
continue;
}
try {
const validate = configValidators[key];
validate(value, config7);
} catch (error) {
ce(`Unknown '${key}' config option or missing validator.`);
ce(
`Manually fix the '.env' file or global '~/.opencommit' config file.`
);
process.exit(1);
}
}
};
var initGlobalConfig = () => {
const defaultConfig = {
OCO_TOKENS_MAX_INPUT: 40960 /* DEFAULT_MAX_TOKENS_INPUT */,
OCO_TOKENS_MAX_OUTPUT: 4096 /* 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: "conventional-commit" /* CONVENTIONAL_COMMIT */,
OCO_AI_PROVIDER: "openai" /* OPENAI */,
OCO_ONE_LINE_COMMIT: false,
OCO_TEST_MOCK_TYPE: "commit-message",
OCO_FLOWISE_ENDPOINT: ":",
OCO_GITPUSH: true
};
(0, import_fs.writeFileSync)(defaultConfigPath, (0, import_ini.stringify)(defaultConfig), "utf8");
return defaultConfig;
var DEFAULT_CONFIG = {
OCO_TOKENS_MAX_INPUT: 40960 /* DEFAULT_MAX_TOKENS_INPUT */,
OCO_TOKENS_MAX_OUTPUT: 4096 /* 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: "conventional-commit" /* CONVENTIONAL_COMMIT */,
OCO_AI_PROVIDER: "openai" /* OPENAI */,
OCO_ONE_LINE_COMMIT: false,
OCO_TEST_MOCK_TYPE: "commit-message",
OCO_FLOWISE_ENDPOINT: ":",
OCO_GITPUSH: true
};
var initGlobalConfig = (configPath = defaultConfigPath) => {
(0, import_fs.writeFileSync)(configPath, (0, import_ini.stringify)(DEFAULT_CONFIG), "utf8");
return DEFAULT_CONFIG;
};
var parseEnvVarValue = (value) => {
try {
Expand All @@ -28075,12 +28055,9 @@ var parseEnvVarValue = (value) => {
return value;
}
};
var getConfig = ({
configPath = defaultConfigPath,
envPath = defaultEnvPath
} = {}) => {
var getEnvConfig = (envPath) => {
dotenv.config({ path: envPath });
const envConfig = {
return {
OCO_MODEL: process.env.OCO_MODEL,
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
OCO_ANTHROPIC_API_KEY: process.env.OCO_ANTHROPIC_API_KEY,
Expand All @@ -28104,23 +28081,35 @@ var getConfig = ({
OCO_TEST_MOCK_TYPE: process.env.OCO_TEST_MOCK_TYPE,
OCO_GITPUSH: parseEnvVarValue(process.env.OCO_GITPUSH)
};
};
var getGlobalConfig = (configPath) => {
let globalConfig;
const isGlobalConfigFileExist = (0, import_fs.existsSync)(configPath);
if (!isGlobalConfigFileExist)
globalConfig = initGlobalConfig();
globalConfig = initGlobalConfig(configPath);
else {
const configFile = (0, import_fs.readFileSync)(configPath, "utf8");
globalConfig = (0, import_ini.parse)(configFile);
}
const mergeObjects = (main, fallback) => Object.keys(CONFIG_KEYS).reduce((acc, key) => {
acc[key] = parseEnvVarValue(main[key] ?? fallback[key]);
return acc;
}, {});
const config7 = mergeObjects(envConfig, globalConfig);
return globalConfig;
};
var mergeConfigs = (main, fallback) => Object.keys(CONFIG_KEYS).reduce((acc, key) => {
acc[key] = parseEnvVarValue(main[key] ?? fallback[key]);
return acc;
}, {});
var getConfig = ({
envPath = defaultEnvPath,
globalPath = defaultConfigPath
} = {}) => {
const envConfig = getEnvConfig(envPath);
const globalConfig = getGlobalConfig(globalPath);
const config7 = mergeConfigs(envConfig, globalConfig);
return config7;
};
var setConfig = (keyValues, configPath = defaultConfigPath) => {
const config7 = getConfig();
var setConfig = (keyValues, globalConfigPath = defaultConfigPath) => {
const config7 = getConfig({
globalPath: globalConfigPath
});
for (let [key, value] of keyValues) {
if (!configValidators.hasOwnProperty(key)) {
const supportedKeys = Object.keys(configValidators).join("\n");
Expand All @@ -28144,8 +28133,7 @@ For more help refer to our docs: https://github.com/di-sukharev/opencommit`
);
config7[key] = validValue;
}
(0, import_fs.writeFileSync)(configPath, (0, import_ini.stringify)(config7), "utf8");
assertConfigsAreValid(config7);
(0, import_fs.writeFileSync)(globalConfigPath, (0, import_ini.stringify)(config7), "utf8");
ce(`${source_default.green("\u2714")} config successfully set`);
};
var configCommand = G3(
Expand Down Expand Up @@ -42400,7 +42388,7 @@ var OpenAiEngine = class {
function getEngine() {
const config7 = getConfig();
const provider = config7.OCO_AI_PROVIDER;
const DEFAULT_CONFIG = {
const DEFAULT_CONFIG2 = {
model: config7.OCO_MODEL,
maxTokensOutput: config7.OCO_TOKENS_MAX_OUTPUT,
maxTokensInput: config7.OCO_TOKENS_MAX_INPUT,
Expand All @@ -42409,37 +42397,37 @@ function getEngine() {
switch (provider) {
case "ollama" /* OLLAMA */:
return new OllamaAi({
...DEFAULT_CONFIG,
...DEFAULT_CONFIG2,
apiKey: "",
baseURL: config7.OCO_OLLAMA_API_URL
});
case "anthropic" /* ANTHROPIC */:
return new AnthropicEngine({
...DEFAULT_CONFIG,
...DEFAULT_CONFIG2,
apiKey: config7.OCO_ANTHROPIC_API_KEY
});
case "test" /* TEST */:
return new TestAi(config7.OCO_TEST_MOCK_TYPE);
case "gemini" /* GEMINI */:
return new Gemini({
...DEFAULT_CONFIG,
...DEFAULT_CONFIG2,
apiKey: config7.OCO_GEMINI_API_KEY,
baseURL: config7.OCO_GEMINI_BASE_PATH
});
case "azure" /* AZURE */:
return new AzureEngine({
...DEFAULT_CONFIG,
...DEFAULT_CONFIG2,
apiKey: config7.OCO_AZURE_API_KEY
});
case "flowise" /* FLOWISE */:
return new FlowiseAi({
...DEFAULT_CONFIG,
baseURL: config7.OCO_FLOWISE_ENDPOINT || DEFAULT_CONFIG.baseURL,
...DEFAULT_CONFIG2,
baseURL: config7.OCO_FLOWISE_ENDPOINT || DEFAULT_CONFIG2.baseURL,
apiKey: config7.OCO_FLOWISE_API_KEY
});
default:
return new OpenAiEngine({
...DEFAULT_CONFIG,
...DEFAULT_CONFIG2,
apiKey: config7.OCO_OPENAI_API_KEY
});
}
Expand Down Expand Up @@ -43172,8 +43160,8 @@ var generateCommitMessageFromGitDiff = async ({
skipCommitConfirmation = false
}) => {
await assertGitRepo();
const commitSpinner = le();
commitSpinner.start("Generating the commit message");
const commitGenerationSpinner = le();
commitGenerationSpinner.start("Generating the commit message");
try {
let commitMessage = await generateCommitMessageByDiff(
diff,
Expand All @@ -43188,7 +43176,7 @@ var generateCommitMessageFromGitDiff = async ({
commitMessage
);
}
commitSpinner.stop("\u{1F4DD} Commit message generated");
commitGenerationSpinner.stop("\u{1F4DD} Commit message generated");
ce(
`Generated commit message:
${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014")}
Expand All @@ -43198,14 +43186,20 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
const isCommitConfirmedByUser = skipCommitConfirmation || await Q3({
message: "Confirm the commit message?"
});
if (isCommitConfirmedByUser && !hD2(isCommitConfirmedByUser)) {
if (hD2(isCommitConfirmedByUser))
process.exit(1);
if (isCommitConfirmedByUser) {
const committingChangesSpinner = le();
committingChangesSpinner.start("Committing the changes");
const { stdout } = await execa("git", [
"commit",
"-m",
commitMessage,
...extraArgs2
]);
ce(`${source_default.green("\u2714")} Successfully committed`);
committingChangesSpinner.stop(
`${source_default.green("\u2714")} Successfully committed`
);
ce(stdout);
const remotes = await getGitRemotes();
if (!remotes.length) {
Expand All @@ -43218,7 +43212,9 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
const isPushConfirmedByUser = await Q3({
message: "Do you want to run `git push`?"
});
if (isPushConfirmedByUser && !hD2(isPushConfirmedByUser)) {
if (hD2(isPushConfirmedByUser))
process.exit(1);
if (isPushConfirmedByUser) {
const pushSpinner = le();
pushSpinner.start(`Running 'git push ${remotes[0]}'`);
const { stdout: stdout2 } = await execa("git", [
Expand All @@ -43240,26 +43236,26 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
message: "Choose a remote to push to",
options: remotes.map((remote) => ({ value: remote, label: remote }))
});
if (!hD2(selectedRemote)) {
const pushSpinner = le();
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
const { stdout: stdout2 } = await execa("git", ["push", selectedRemote]);
pushSpinner.stop(
`${source_default.green(
"\u2714"
)} Successfully pushed all commits to ${selectedRemote}`
);
if (stdout2)
ce(stdout2);
} else
ce(`${source_default.gray("\u2716")} process cancelled`);
if (hD2(selectedRemote))
process.exit(1);
const pushSpinner = le();
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
const { stdout: stdout2 } = await execa("git", ["push", selectedRemote]);
pushSpinner.stop(
`${source_default.green(
"\u2714"
)} Successfully pushed all commits to ${selectedRemote}`
);
if (stdout2)
ce(stdout2);
}
}
if (!isCommitConfirmedByUser && !hD2(isCommitConfirmedByUser)) {
} else {
const regenerateMessage = await Q3({
message: "Do you want to regenerate the message?"
});
if (regenerateMessage && !hD2(isCommitConfirmedByUser)) {
if (hD2(regenerateMessage))
process.exit(1);
if (regenerateMessage) {
await generateCommitMessageFromGitDiff({
diff,
extraArgs: extraArgs2,
Expand All @@ -43268,7 +43264,7 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
}
}
} catch (error) {
commitSpinner.stop("\u{1F4DD} Commit message generated");
commitGenerationSpinner.stop("\u{1F4DD} Commit message generated");
const err = error;
ce(`${source_default.red("\u2716")} ${err?.message || err}`);
process.exit(1);
Expand Down Expand Up @@ -43302,7 +43298,9 @@ async function commit(extraArgs2 = [], isStageAllFlag = false, fullGitMojiSpec =
const isStageAllAndCommitConfirmedByUser = await Q3({
message: "Do you want to stage all files and generate commit message?"
});
if (isStageAllAndCommitConfirmedByUser && !hD2(isStageAllAndCommitConfirmedByUser)) {
if (hD2(isStageAllAndCommitConfirmedByUser))
process.exit(1);
if (isStageAllAndCommitConfirmedByUser) {
await commit(extraArgs2, true, fullGitMojiSpec);
process.exit(1);
}
Expand Down
Loading

0 comments on commit 8702c17

Please sign in to comment.