Skip to content

Commit 8702c17

Browse files
di-sukharevmatscube
andauthoredSep 1, 2024
390 add config set tests (#399)
* 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 <matscube@gmail.com>
1 parent 60597d2 commit 8702c17

File tree

7 files changed

+458
-301
lines changed

7 files changed

+458
-301
lines changed
 

‎out/cli.cjs

+84-86
Original file line numberDiff line numberDiff line change
@@ -25193,7 +25193,7 @@ function G3(t2, e3) {
2519325193
// package.json
2519425194
var package_default = {
2519525195
name: "opencommit",
25196-
version: "3.0.20",
25196+
version: "3.1.0",
2519725197
description: "Auto-generate impressive commits in 1 second. Killing lame commits with AI \u{1F92F}\u{1F52B}",
2519825198
keywords: [
2519925199
"git",
@@ -28029,44 +28029,24 @@ var configValidators = {
2802928029
};
2803028030
var defaultConfigPath = (0, import_path.join)((0, import_os.homedir)(), ".opencommit");
2803128031
var defaultEnvPath = (0, import_path.resolve)(process.cwd(), ".env");
28032-
var assertConfigsAreValid = (config7) => {
28033-
for (const [key, value] of Object.entries(config7)) {
28034-
if (!value)
28035-
continue;
28036-
if (typeof value === "string" && ["null", "undefined"].includes(value)) {
28037-
config7[key] = void 0;
28038-
continue;
28039-
}
28040-
try {
28041-
const validate = configValidators[key];
28042-
validate(value, config7);
28043-
} catch (error) {
28044-
ce(`Unknown '${key}' config option or missing validator.`);
28045-
ce(
28046-
`Manually fix the '.env' file or global '~/.opencommit' config file.`
28047-
);
28048-
process.exit(1);
28049-
}
28050-
}
28051-
};
28052-
var initGlobalConfig = () => {
28053-
const defaultConfig = {
28054-
OCO_TOKENS_MAX_INPUT: 40960 /* DEFAULT_MAX_TOKENS_INPUT */,
28055-
OCO_TOKENS_MAX_OUTPUT: 4096 /* DEFAULT_MAX_TOKENS_OUTPUT */,
28056-
OCO_DESCRIPTION: false,
28057-
OCO_EMOJI: false,
28058-
OCO_MODEL: getDefaultModel("openai"),
28059-
OCO_LANGUAGE: "en",
28060-
OCO_MESSAGE_TEMPLATE_PLACEHOLDER: "$msg",
28061-
OCO_PROMPT_MODULE: "conventional-commit" /* CONVENTIONAL_COMMIT */,
28062-
OCO_AI_PROVIDER: "openai" /* OPENAI */,
28063-
OCO_ONE_LINE_COMMIT: false,
28064-
OCO_TEST_MOCK_TYPE: "commit-message",
28065-
OCO_FLOWISE_ENDPOINT: ":",
28066-
OCO_GITPUSH: true
28067-
};
28068-
(0, import_fs.writeFileSync)(defaultConfigPath, (0, import_ini.stringify)(defaultConfig), "utf8");
28069-
return defaultConfig;
28032+
var DEFAULT_CONFIG = {
28033+
OCO_TOKENS_MAX_INPUT: 40960 /* DEFAULT_MAX_TOKENS_INPUT */,
28034+
OCO_TOKENS_MAX_OUTPUT: 4096 /* DEFAULT_MAX_TOKENS_OUTPUT */,
28035+
OCO_DESCRIPTION: false,
28036+
OCO_EMOJI: false,
28037+
OCO_MODEL: getDefaultModel("openai"),
28038+
OCO_LANGUAGE: "en",
28039+
OCO_MESSAGE_TEMPLATE_PLACEHOLDER: "$msg",
28040+
OCO_PROMPT_MODULE: "conventional-commit" /* CONVENTIONAL_COMMIT */,
28041+
OCO_AI_PROVIDER: "openai" /* OPENAI */,
28042+
OCO_ONE_LINE_COMMIT: false,
28043+
OCO_TEST_MOCK_TYPE: "commit-message",
28044+
OCO_FLOWISE_ENDPOINT: ":",
28045+
OCO_GITPUSH: true
28046+
};
28047+
var initGlobalConfig = (configPath = defaultConfigPath) => {
28048+
(0, import_fs.writeFileSync)(configPath, (0, import_ini.stringify)(DEFAULT_CONFIG), "utf8");
28049+
return DEFAULT_CONFIG;
2807028050
};
2807128051
var parseEnvVarValue = (value) => {
2807228052
try {
@@ -28075,12 +28055,9 @@ var parseEnvVarValue = (value) => {
2807528055
return value;
2807628056
}
2807728057
};
28078-
var getConfig = ({
28079-
configPath = defaultConfigPath,
28080-
envPath = defaultEnvPath
28081-
} = {}) => {
28058+
var getEnvConfig = (envPath) => {
2808228059
dotenv.config({ path: envPath });
28083-
const envConfig = {
28060+
return {
2808428061
OCO_MODEL: process.env.OCO_MODEL,
2808528062
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
2808628063
OCO_ANTHROPIC_API_KEY: process.env.OCO_ANTHROPIC_API_KEY,
@@ -28104,23 +28081,35 @@ var getConfig = ({
2810428081
OCO_TEST_MOCK_TYPE: process.env.OCO_TEST_MOCK_TYPE,
2810528082
OCO_GITPUSH: parseEnvVarValue(process.env.OCO_GITPUSH)
2810628083
};
28084+
};
28085+
var getGlobalConfig = (configPath) => {
2810728086
let globalConfig;
2810828087
const isGlobalConfigFileExist = (0, import_fs.existsSync)(configPath);
2810928088
if (!isGlobalConfigFileExist)
28110-
globalConfig = initGlobalConfig();
28089+
globalConfig = initGlobalConfig(configPath);
2811128090
else {
2811228091
const configFile = (0, import_fs.readFileSync)(configPath, "utf8");
2811328092
globalConfig = (0, import_ini.parse)(configFile);
2811428093
}
28115-
const mergeObjects = (main, fallback) => Object.keys(CONFIG_KEYS).reduce((acc, key) => {
28116-
acc[key] = parseEnvVarValue(main[key] ?? fallback[key]);
28117-
return acc;
28118-
}, {});
28119-
const config7 = mergeObjects(envConfig, globalConfig);
28094+
return globalConfig;
28095+
};
28096+
var mergeConfigs = (main, fallback) => Object.keys(CONFIG_KEYS).reduce((acc, key) => {
28097+
acc[key] = parseEnvVarValue(main[key] ?? fallback[key]);
28098+
return acc;
28099+
}, {});
28100+
var getConfig = ({
28101+
envPath = defaultEnvPath,
28102+
globalPath = defaultConfigPath
28103+
} = {}) => {
28104+
const envConfig = getEnvConfig(envPath);
28105+
const globalConfig = getGlobalConfig(globalPath);
28106+
const config7 = mergeConfigs(envConfig, globalConfig);
2812028107
return config7;
2812128108
};
28122-
var setConfig = (keyValues, configPath = defaultConfigPath) => {
28123-
const config7 = getConfig();
28109+
var setConfig = (keyValues, globalConfigPath = defaultConfigPath) => {
28110+
const config7 = getConfig({
28111+
globalPath: globalConfigPath
28112+
});
2812428113
for (let [key, value] of keyValues) {
2812528114
if (!configValidators.hasOwnProperty(key)) {
2812628115
const supportedKeys = Object.keys(configValidators).join("\n");
@@ -28144,8 +28133,7 @@ For more help refer to our docs: https://github.com/di-sukharev/opencommit`
2814428133
);
2814528134
config7[key] = validValue;
2814628135
}
28147-
(0, import_fs.writeFileSync)(configPath, (0, import_ini.stringify)(config7), "utf8");
28148-
assertConfigsAreValid(config7);
28136+
(0, import_fs.writeFileSync)(globalConfigPath, (0, import_ini.stringify)(config7), "utf8");
2814928137
ce(`${source_default.green("\u2714")} config successfully set`);
2815028138
};
2815128139
var configCommand = G3(
@@ -42400,7 +42388,7 @@ var OpenAiEngine = class {
4240042388
function getEngine() {
4240142389
const config7 = getConfig();
4240242390
const provider = config7.OCO_AI_PROVIDER;
42403-
const DEFAULT_CONFIG = {
42391+
const DEFAULT_CONFIG2 = {
4240442392
model: config7.OCO_MODEL,
4240542393
maxTokensOutput: config7.OCO_TOKENS_MAX_OUTPUT,
4240642394
maxTokensInput: config7.OCO_TOKENS_MAX_INPUT,
@@ -42409,37 +42397,37 @@ function getEngine() {
4240942397
switch (provider) {
4241042398
case "ollama" /* OLLAMA */:
4241142399
return new OllamaAi({
42412-
...DEFAULT_CONFIG,
42400+
...DEFAULT_CONFIG2,
4241342401
apiKey: "",
4241442402
baseURL: config7.OCO_OLLAMA_API_URL
4241542403
});
4241642404
case "anthropic" /* ANTHROPIC */:
4241742405
return new AnthropicEngine({
42418-
...DEFAULT_CONFIG,
42406+
...DEFAULT_CONFIG2,
4241942407
apiKey: config7.OCO_ANTHROPIC_API_KEY
4242042408
});
4242142409
case "test" /* TEST */:
4242242410
return new TestAi(config7.OCO_TEST_MOCK_TYPE);
4242342411
case "gemini" /* GEMINI */:
4242442412
return new Gemini({
42425-
...DEFAULT_CONFIG,
42413+
...DEFAULT_CONFIG2,
4242642414
apiKey: config7.OCO_GEMINI_API_KEY,
4242742415
baseURL: config7.OCO_GEMINI_BASE_PATH
4242842416
});
4242942417
case "azure" /* AZURE */:
4243042418
return new AzureEngine({
42431-
...DEFAULT_CONFIG,
42419+
...DEFAULT_CONFIG2,
4243242420
apiKey: config7.OCO_AZURE_API_KEY
4243342421
});
4243442422
case "flowise" /* FLOWISE */:
4243542423
return new FlowiseAi({
42436-
...DEFAULT_CONFIG,
42437-
baseURL: config7.OCO_FLOWISE_ENDPOINT || DEFAULT_CONFIG.baseURL,
42424+
...DEFAULT_CONFIG2,
42425+
baseURL: config7.OCO_FLOWISE_ENDPOINT || DEFAULT_CONFIG2.baseURL,
4243842426
apiKey: config7.OCO_FLOWISE_API_KEY
4243942427
});
4244042428
default:
4244142429
return new OpenAiEngine({
42442-
...DEFAULT_CONFIG,
42430+
...DEFAULT_CONFIG2,
4244342431
apiKey: config7.OCO_OPENAI_API_KEY
4244442432
});
4244542433
}
@@ -43172,8 +43160,8 @@ var generateCommitMessageFromGitDiff = async ({
4317243160
skipCommitConfirmation = false
4317343161
}) => {
4317443162
await assertGitRepo();
43175-
const commitSpinner = le();
43176-
commitSpinner.start("Generating the commit message");
43163+
const commitGenerationSpinner = le();
43164+
commitGenerationSpinner.start("Generating the commit message");
4317743165
try {
4317843166
let commitMessage = await generateCommitMessageByDiff(
4317943167
diff,
@@ -43188,7 +43176,7 @@ var generateCommitMessageFromGitDiff = async ({
4318843176
commitMessage
4318943177
);
4319043178
}
43191-
commitSpinner.stop("\u{1F4DD} Commit message generated");
43179+
commitGenerationSpinner.stop("\u{1F4DD} Commit message generated");
4319243180
ce(
4319343181
`Generated commit message:
4319443182
${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014")}
@@ -43198,14 +43186,20 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
4319843186
const isCommitConfirmedByUser = skipCommitConfirmation || await Q3({
4319943187
message: "Confirm the commit message?"
4320043188
});
43201-
if (isCommitConfirmedByUser && !hD2(isCommitConfirmedByUser)) {
43189+
if (hD2(isCommitConfirmedByUser))
43190+
process.exit(1);
43191+
if (isCommitConfirmedByUser) {
43192+
const committingChangesSpinner = le();
43193+
committingChangesSpinner.start("Committing the changes");
4320243194
const { stdout } = await execa("git", [
4320343195
"commit",
4320443196
"-m",
4320543197
commitMessage,
4320643198
...extraArgs2
4320743199
]);
43208-
ce(`${source_default.green("\u2714")} Successfully committed`);
43200+
committingChangesSpinner.stop(
43201+
`${source_default.green("\u2714")} Successfully committed`
43202+
);
4320943203
ce(stdout);
4321043204
const remotes = await getGitRemotes();
4321143205
if (!remotes.length) {
@@ -43218,7 +43212,9 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
4321843212
const isPushConfirmedByUser = await Q3({
4321943213
message: "Do you want to run `git push`?"
4322043214
});
43221-
if (isPushConfirmedByUser && !hD2(isPushConfirmedByUser)) {
43215+
if (hD2(isPushConfirmedByUser))
43216+
process.exit(1);
43217+
if (isPushConfirmedByUser) {
4322243218
const pushSpinner = le();
4322343219
pushSpinner.start(`Running 'git push ${remotes[0]}'`);
4322443220
const { stdout: stdout2 } = await execa("git", [
@@ -43240,26 +43236,26 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
4324043236
message: "Choose a remote to push to",
4324143237
options: remotes.map((remote) => ({ value: remote, label: remote }))
4324243238
});
43243-
if (!hD2(selectedRemote)) {
43244-
const pushSpinner = le();
43245-
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
43246-
const { stdout: stdout2 } = await execa("git", ["push", selectedRemote]);
43247-
pushSpinner.stop(
43248-
`${source_default.green(
43249-
"\u2714"
43250-
)} Successfully pushed all commits to ${selectedRemote}`
43251-
);
43252-
if (stdout2)
43253-
ce(stdout2);
43254-
} else
43255-
ce(`${source_default.gray("\u2716")} process cancelled`);
43239+
if (hD2(selectedRemote))
43240+
process.exit(1);
43241+
const pushSpinner = le();
43242+
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
43243+
const { stdout: stdout2 } = await execa("git", ["push", selectedRemote]);
43244+
pushSpinner.stop(
43245+
`${source_default.green(
43246+
"\u2714"
43247+
)} Successfully pushed all commits to ${selectedRemote}`
43248+
);
43249+
if (stdout2)
43250+
ce(stdout2);
4325643251
}
43257-
}
43258-
if (!isCommitConfirmedByUser && !hD2(isCommitConfirmedByUser)) {
43252+
} else {
4325943253
const regenerateMessage = await Q3({
4326043254
message: "Do you want to regenerate the message?"
4326143255
});
43262-
if (regenerateMessage && !hD2(isCommitConfirmedByUser)) {
43256+
if (hD2(regenerateMessage))
43257+
process.exit(1);
43258+
if (regenerateMessage) {
4326343259
await generateCommitMessageFromGitDiff({
4326443260
diff,
4326543261
extraArgs: extraArgs2,
@@ -43268,7 +43264,7 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
4326843264
}
4326943265
}
4327043266
} catch (error) {
43271-
commitSpinner.stop("\u{1F4DD} Commit message generated");
43267+
commitGenerationSpinner.stop("\u{1F4DD} Commit message generated");
4327243268
const err = error;
4327343269
ce(`${source_default.red("\u2716")} ${err?.message || err}`);
4327443270
process.exit(1);
@@ -43302,7 +43298,9 @@ async function commit(extraArgs2 = [], isStageAllFlag = false, fullGitMojiSpec =
4330243298
const isStageAllAndCommitConfirmedByUser = await Q3({
4330343299
message: "Do you want to stage all files and generate commit message?"
4330443300
});
43305-
if (isStageAllAndCommitConfirmedByUser && !hD2(isStageAllAndCommitConfirmedByUser)) {
43301+
if (hD2(isStageAllAndCommitConfirmedByUser))
43302+
process.exit(1);
43303+
if (isStageAllAndCommitConfirmedByUser) {
4330643304
await commit(extraArgs2, true, fullGitMojiSpec);
4330743305
process.exit(1);
4330843306
}

0 commit comments

Comments
 (0)
Failed to load comments.