22
33import os
44import shutil
5+ from pathlib import Path
56from typing import Any , NamedTuple
67
78import questionary
89import yaml
910
1011from commitizen import cmd , factory , out
1112from commitizen .__version__ import __version__
12- from commitizen .config import BaseConfig , JsonConfig , TomlConfig , YAMLConfig
13+ from commitizen .config import (
14+ BaseConfig ,
15+ create_config ,
16+ )
1317from commitizen .cz import registry
1418from commitizen .defaults import CONFIG_FILES , DEFAULT_SETTINGS
1519from commitizen .exceptions import InitFailedError , NoAnswersError
@@ -153,14 +157,6 @@ def __call__(self) -> None:
153157 except KeyboardInterrupt :
154158 raise InitFailedError ("Stopped by user" )
155159
156- # Initialize configuration
157- if "toml" in config_path :
158- self .config = TomlConfig (data = "" , path = config_path )
159- elif "json" in config_path :
160- self .config = JsonConfig (data = "{}" , path = config_path )
161- elif "yaml" in config_path :
162- self .config = YAMLConfig (data = "" , path = config_path )
163-
164160 # Collect hook data
165161 hook_types = questionary .checkbox (
166162 "What types of pre-commit hook you want to install? (Leave blank if you don't want to install)" ,
@@ -175,37 +171,33 @@ def __call__(self) -> None:
175171 except InitFailedError as e :
176172 raise InitFailedError (f"Failed to install pre-commit hook.\n { e } " )
177173
178- # Create and initialize config
179- self .config .init_empty_config_content ()
180-
181- self .config .set_key ("name" , cz_name )
182- self .config .set_key ("tag_format" , tag_format )
183- self .config .set_key ("version_scheme" , version_scheme )
184- if version_provider == "commitizen" :
185- self .config .set_key ("version" , version .public )
186- else :
187- self .config .set_key ("version_provider" , version_provider )
188- if update_changelog_on_bump :
189- self .config .set_key ("update_changelog_on_bump" , update_changelog_on_bump )
190- if major_version_zero :
191- self .config .set_key ("major_version_zero" , major_version_zero )
174+ _write_config_to_file (
175+ path = config_path ,
176+ cz_name = cz_name ,
177+ version_provider = version_provider ,
178+ version_scheme = version_scheme ,
179+ version = version ,
180+ tag_format = tag_format ,
181+ update_changelog_on_bump = update_changelog_on_bump ,
182+ major_version_zero = major_version_zero ,
183+ )
192184
193185 out .write ("\n You can bump the version running:\n " )
194186 out .info ("\t cz bump\n " )
195187 out .success ("Configuration complete 🚀" )
196188
197- def _ask_config_path (self ) -> str :
189+ def _ask_config_path (self ) -> Path :
198190 default_path = (
199191 "pyproject.toml" if self .project_info .has_pyproject else ".cz.toml"
200192 )
201193
202- name : str = questionary .select (
194+ filename : str = questionary .select (
203195 "Please choose a supported config file: " ,
204196 choices = CONFIG_FILES ,
205197 default = default_path ,
206198 style = self .cz .style ,
207199 ).unsafe_ask ()
208- return name
200+ return Path ( filename )
209201
210202 def _ask_name (self ) -> str :
211203 name : str = questionary .select (
@@ -383,3 +375,30 @@ def _install_pre_commit_hook(self, hook_types: list[str] | None = None) -> None:
383375 hook_types = ["commit-msg" , "pre-push" ]
384376 self ._exec_install_pre_commit_hook (hook_types )
385377 out .write ("commitizen pre-commit hook is now installed in your '.git'\n " )
378+
379+
380+ def _write_config_to_file (
381+ * ,
382+ path : Path ,
383+ cz_name : str ,
384+ version_provider : str ,
385+ version_scheme : str ,
386+ version : Version ,
387+ tag_format : str ,
388+ update_changelog_on_bump : bool ,
389+ major_version_zero : bool ,
390+ ) -> None :
391+ out_config = create_config (path = path )
392+ out_config .init_empty_config_content ()
393+
394+ out_config .set_key ("name" , cz_name )
395+ out_config .set_key ("tag_format" , tag_format )
396+ out_config .set_key ("version_scheme" , version_scheme )
397+ if version_provider == "commitizen" :
398+ out_config .set_key ("version" , version .public )
399+ else :
400+ out_config .set_key ("version_provider" , version_provider )
401+ if update_changelog_on_bump :
402+ out_config .set_key ("update_changelog_on_bump" , update_changelog_on_bump )
403+ if major_version_zero :
404+ out_config .set_key ("major_version_zero" , major_version_zero )
0 commit comments