|
3 | 3 |
|
4 | 4 | from . import data, language, log, settings |
5 | 5 |
|
6 | | -def cli(caller_file): |
7 | | - cli = data.sns.from_dict(data.json.read(Path(__file__).parent.parent / 'assets/data/package_data.json')) |
| 6 | +data_path = Path(__file__).parent.parent / 'assets/data' |
| 7 | + |
| 8 | +def cli(): |
| 9 | + cli = data.sns.from_dict(data.json.read(data_path / 'package_data.json')) |
8 | 10 | cli.msgs = language.get_msgs() |
9 | | - settings.load(cli, caller_file) |
| 11 | + settings.load(cli) |
10 | 12 | return cli |
11 | 13 |
|
12 | 14 | def config_file(cli): |
13 | | - config_path = Path(cli.config_filepath) |
14 | | - if config_path.exists(): |
| 15 | + target_path = Path.cwd() / f'.{cli.short_name}.config.json5' |
| 16 | + project_markers = data.json.read(data_path / 'project_markers.json') |
| 17 | + if not any((Path.cwd() / marker).exists() for marker in project_markers): |
| 18 | + log.warn(f'{cli.msgs.warn_NO_PROJECT_ROOT_FOUND_IN} {Path.cwd()}') |
| 19 | + user_resp = input(f'{cli.msgs.prompt_INIT_CONFIG_HERE_ANYWAY}? (y/N): ').strip().lower() |
| 20 | + if not user_resp.startswith('y') : return |
| 21 | + else : not_in_root = True # for move tip |
| 22 | + |
| 23 | + # Handle existing file |
| 24 | + if target_path.exists(): |
15 | 25 | if cli.config.force: |
16 | | - log.info(f'{cli.msgs.log_OVERWRITING_CONFIG_AT} {config_path}...') |
| 26 | + log.info(f'{cli.msgs.log_OVERWRITING_CONFIG_AT} {target_path}...') |
17 | 27 | else: |
18 | | - log.warn(f'{cli.msgs.warn_CONFIG_EXISTS_AT} {config_path}. {cli.msgs.log_SKIPPING} init.') |
| 28 | + log.warn(f'{cli.msgs.warn_CONFIG_EXISTS_AT} {target_path}. {cli.msgs.log_SKIPPING} init...') |
19 | 29 | log.tip(f'{cli.msgs.tip_PASS_FORCE_TO_OVERWRITE}.') |
20 | 30 | return |
21 | | - cli.config_filename = f'.{cli.short_name}.config.json5' |
22 | | - cli.config_filepath = str(Path(cli.project_root) / cli.config_filename) |
23 | | - if not getattr(cli, 'default_file_config', None): |
24 | | - cli.default_file_config = data.url.get(f'{cli.urls.jsdelivr}/{cli.name}/{cli.config_filename}') |
25 | | - data.file.write(cli.config_filepath, cli.default_file_config) |
26 | | - log.success(f'{cli.msgs.log_DEFAULT_CONFIG_CREATED_AT} {cli.config_filepath}') |
| 31 | + |
| 32 | + # Fetch/write from jsDelivr |
| 33 | + if not getattr(cli, 'default_file_config', ''): |
| 34 | + cli.default_file_config = data.url.get(f'{cli.urls.jsdelivr}/{cli.name}/{target_path.name}') |
| 35 | + data.file.write(str(target_path), cli.default_file_config) |
| 36 | + log.success(f'{cli.msgs.log_DEFAULT_CONFIG_CREATED_AT} {target_path}') |
| 37 | + if not_in_root : log.tip(f'{cli.msgs.tip_MOVE_CONFIG_TO_ROOT}.') |
| 38 | + |
| 39 | +def config_filepath(cli): # for settings.load() |
| 40 | + |
| 41 | + # Check --config <path> |
| 42 | + for idx, arg in enumerate(sys.argv): |
| 43 | + if arg == '--config' and idx +1 < len(sys.argv): |
| 44 | + cli.config_filepath = Path(sys.argv[idx + 1]).resolve() |
| 45 | + if cli.config_filepath.exists(): return |
| 46 | + else : log.warn(f'{cli.msgs.warn_SPECIFIED_CONFIG} {cli.config_filepath} {cli.msgs.warn_NOT_FOUND.lower()}') |
| 47 | + |
| 48 | + # Search upwards |
| 49 | + possible_config_filenames = [ |
| 50 | + f'{prefix}{name}.config.json{suffix}' |
| 51 | + for prefix in ['.', ''] for name in [cli.short_name, cli.name] for suffix in ['5', '', 'c'] |
| 52 | + ] |
| 53 | + current_dir = Path.cwd().resolve() |
| 54 | + for parent in [current_dir, *current_dir.parents]: |
| 55 | + for filename in possible_config_filenames: |
| 56 | + possible_config_file = parent / filename |
| 57 | + if possible_config_file.exists(): |
| 58 | + cli.config_filepath = possible_config_file |
| 59 | + return |
27 | 60 |
|
28 | 61 | def locales_dir(cli): |
29 | 62 | for path in Path.cwd().rglob(cli.config.locales_dir): |
30 | 63 | if path.is_dir(): |
31 | 64 | cli.config.locales_dir = str(path) |
32 | 65 | return |
33 | | - cli.config.locales_dir = None |
| 66 | + cli.config.locales_dir = '' |
34 | 67 |
|
35 | 68 | def src_msgs(cli): |
36 | 69 | cli.msgs_filename = 'messages.json' |
|
0 commit comments