|  | 
| 2 | 2 | 
 | 
| 3 | 3 | import os | 
| 4 | 4 | import shutil | 
|  | 5 | +from pathlib import Path | 
| 5 | 6 | from typing import Any, NamedTuple | 
| 6 | 7 | 
 | 
| 7 | 8 | import questionary | 
| 8 | 9 | import yaml | 
| 9 | 10 | 
 | 
| 10 | 11 | from commitizen import cmd, factory, out | 
| 11 | 12 | from commitizen.__version__ import __version__ | 
| 12 |  | -from commitizen.config import BaseConfig, JsonConfig, TomlConfig, YAMLConfig | 
|  | 13 | +from commitizen.config import ( | 
|  | 14 | +    BaseConfig, | 
|  | 15 | +    create_config, | 
|  | 16 | +) | 
| 13 | 17 | from commitizen.cz import registry | 
| 14 | 18 | from commitizen.defaults import CONFIG_FILES, DEFAULT_SETTINGS | 
| 15 | 19 | from commitizen.exceptions import InitFailedError, NoAnswersError | 
| @@ -187,45 +191,33 @@ def __call__(self) -> None: | 
| 187 | 191 |                 ) | 
| 188 | 192 |             out.write("commitizen pre-commit hook is now installed in your '.git'\n") | 
| 189 | 193 | 
 | 
| 190 |  | -        # Initialize configuration | 
| 191 |  | -        if "toml" in config_path: | 
| 192 |  | -            self.config = TomlConfig(data="", path=config_path) | 
| 193 |  | -        elif "json" in config_path: | 
| 194 |  | -            self.config = JsonConfig(data="{}", path=config_path) | 
| 195 |  | -        elif "yaml" in config_path: | 
| 196 |  | -            self.config = YAMLConfig(data="", path=config_path) | 
| 197 |  | - | 
| 198 |  | -        # Create and initialize config | 
| 199 |  | -        self.config.init_empty_config_content() | 
| 200 |  | - | 
| 201 |  | -        self.config.set_key("name", cz_name) | 
| 202 |  | -        self.config.set_key("tag_format", tag_format) | 
| 203 |  | -        self.config.set_key("version_scheme", version_scheme) | 
| 204 |  | -        if version_provider == "commitizen": | 
| 205 |  | -            self.config.set_key("version", version.public) | 
| 206 |  | -        else: | 
| 207 |  | -            self.config.set_key("version_provider", version_provider) | 
| 208 |  | -        if update_changelog_on_bump: | 
| 209 |  | -            self.config.set_key("update_changelog_on_bump", update_changelog_on_bump) | 
| 210 |  | -        if major_version_zero: | 
| 211 |  | -            self.config.set_key("major_version_zero", major_version_zero) | 
|  | 194 | +        _write_config_to_file( | 
|  | 195 | +            path=config_path, | 
|  | 196 | +            cz_name=cz_name, | 
|  | 197 | +            version_provider=version_provider, | 
|  | 198 | +            version_scheme=version_scheme, | 
|  | 199 | +            version=version, | 
|  | 200 | +            tag_format=tag_format, | 
|  | 201 | +            update_changelog_on_bump=update_changelog_on_bump, | 
|  | 202 | +            major_version_zero=major_version_zero, | 
|  | 203 | +        ) | 
| 212 | 204 | 
 | 
| 213 | 205 |         out.write("\nYou can bump the version running:\n") | 
| 214 | 206 |         out.info("\tcz bump\n") | 
| 215 | 207 |         out.success("Configuration complete 🚀") | 
| 216 | 208 | 
 | 
| 217 |  | -    def _ask_config_path(self) -> str: | 
|  | 209 | +    def _ask_config_path(self) -> Path: | 
| 218 | 210 |         default_path = ( | 
| 219 | 211 |             "pyproject.toml" if self.project_info.has_pyproject else ".cz.toml" | 
| 220 | 212 |         ) | 
| 221 | 213 | 
 | 
| 222 |  | -        name: str = questionary.select( | 
|  | 214 | +        filename: str = questionary.select( | 
| 223 | 215 |             "Please choose a supported config file: ", | 
| 224 | 216 |             choices=CONFIG_FILES, | 
| 225 | 217 |             default=default_path, | 
| 226 | 218 |             style=self.cz.style, | 
| 227 | 219 |         ).unsafe_ask() | 
| 228 |  | -        return name | 
|  | 220 | +        return Path(filename) | 
| 229 | 221 | 
 | 
| 230 | 222 |     def _ask_name(self) -> str: | 
| 231 | 223 |         name: str = questionary.select( | 
| @@ -369,3 +361,30 @@ def _get_config_data(self) -> dict[str, Any]: | 
| 369 | 361 |         else: | 
| 370 | 362 |             repos.append(CZ_HOOK_CONFIG) | 
| 371 | 363 |         return config_data | 
|  | 364 | + | 
|  | 365 | + | 
|  | 366 | +def _write_config_to_file( | 
|  | 367 | +    *, | 
|  | 368 | +    path: Path, | 
|  | 369 | +    cz_name: str, | 
|  | 370 | +    version_provider: str, | 
|  | 371 | +    version_scheme: str, | 
|  | 372 | +    version: Version, | 
|  | 373 | +    tag_format: str, | 
|  | 374 | +    update_changelog_on_bump: bool, | 
|  | 375 | +    major_version_zero: bool, | 
|  | 376 | +) -> None: | 
|  | 377 | +    out_config = create_config(path=path) | 
|  | 378 | +    out_config.init_empty_config_content() | 
|  | 379 | + | 
|  | 380 | +    out_config.set_key("name", cz_name) | 
|  | 381 | +    out_config.set_key("tag_format", tag_format) | 
|  | 382 | +    out_config.set_key("version_scheme", version_scheme) | 
|  | 383 | +    if version_provider == "commitizen": | 
|  | 384 | +        out_config.set_key("version", version.public) | 
|  | 385 | +    else: | 
|  | 386 | +        out_config.set_key("version_provider", version_provider) | 
|  | 387 | +    if update_changelog_on_bump: | 
|  | 388 | +        out_config.set_key("update_changelog_on_bump", update_changelog_on_bump) | 
|  | 389 | +    if major_version_zero: | 
|  | 390 | +        out_config.set_key("major_version_zero", major_version_zero) | 
0 commit comments