10
10
import sys
11
11
from pathlib import Path
12
12
13
- from tqdm import tqdm
14
-
15
13
from pipeline .commands .build import build_command
16
14
from pipeline .commands .dev import dev_command
17
15
from pipeline .tools .docusaurus_parser import convert_docusaurus_to_mintlify
18
16
from pipeline .tools .links import drop_suffix_from_links , move_file_with_link_updates
19
17
from pipeline .tools .notebook .convert import convert_notebook
20
- from pipeline .tools .parser import to_mint
18
+ from pipeline .tools .parser import ParseError , to_mint
21
19
22
20
23
21
def setup_logging () -> None :
24
22
"""Configure logging for the CLI application."""
25
23
logging .basicConfig (
26
24
level = logging .INFO ,
27
- format = "%(asctime)s - %(name)s - %( levelname)s - %(message)s" ,
25
+ format = "%(levelname)s - %(message)s" ,
28
26
handlers = [logging .StreamHandler (sys .stdout )],
29
27
)
30
28
@@ -68,51 +66,65 @@ def _find_files_to_migrate(
68
66
69
67
def _process_single_file (
70
68
file_path : Path , output_path : Path , * , dry_run : bool , migration_type : str = "mkdocs"
71
- ) -> None :
69
+ ) -> bool :
72
70
"""Process a single file for migration.
73
71
74
72
Args:
75
73
file_path: Input file path
76
74
output_path: Output file path
77
75
dry_run: Whether to print to stdout instead of writing
78
76
migration_type: Type of migration ("mkdocs" or "docusaurus")
79
- """
80
- extension = file_path .suffix .lower ()
81
- content = file_path .read_text ()
82
77
83
- if extension in {".md" , ".markdown" , ".mdx" }:
84
- if migration_type == "docusaurus" :
85
- mint_markdown = convert_docusaurus_to_mintlify (content , file_path )
86
- else :
87
- mint_markdown = to_mint (content )
88
- elif extension == ".ipynb" :
89
- markdown = convert_notebook (file_path )
90
- if migration_type == "docusaurus" :
91
- mint_markdown = convert_docusaurus_to_mintlify (markdown , file_path )
78
+ Returns:
79
+ True if processing was successful, False if there was an error
80
+ """
81
+ try :
82
+ extension = file_path .suffix .lower ()
83
+ content = file_path .read_text ()
84
+
85
+ if extension in {".md" , ".markdown" , ".mdx" }:
86
+ if migration_type == "docusaurus" :
87
+ mint_markdown = convert_docusaurus_to_mintlify (content , file_path )
88
+ else :
89
+ mint_markdown = to_mint (content , str (file_path ))
90
+ elif extension == ".ipynb" :
91
+ markdown = convert_notebook (file_path )
92
+ if migration_type == "docusaurus" :
93
+ mint_markdown = convert_docusaurus_to_mintlify (markdown , file_path )
94
+ else :
95
+ mint_markdown = to_mint (markdown , str (file_path ))
92
96
else :
93
- mint_markdown = to_mint (markdown )
94
- else :
95
- logger .warning (
96
- "Skipping unsupported file extension %s: %s" , extension , file_path
97
- )
98
- return
97
+ logger .warning (
98
+ "Skipping unsupported file extension %s: %s" , extension , file_path
99
+ )
100
+ return True # Not an error, just unsupported
99
101
100
- _ , mint_markdown = drop_suffix_from_links (mint_markdown )
102
+ _ , mint_markdown = drop_suffix_from_links (mint_markdown )
101
103
102
- if dry_run :
103
- # Print the converted markdown to stdout
104
- print (f"=== { file_path } ===" ) # noqa: T201 (OK to use print)
105
- print (mint_markdown ) # noqa: T201 (OK to use print)
106
- print () # noqa: T201 (OK to use print)
107
- else :
108
- # Ensure output directory exists
109
- output_path .parent .mkdir (parents = True , exist_ok = True )
104
+ if dry_run :
105
+ # Print the converted markdown to stdout
106
+ print (f"=== { file_path } ===" ) # noqa: T201 (OK to use print)
107
+ print (mint_markdown ) # noqa: T201 (OK to use print)
108
+ print () # noqa: T201 (OK to use print)
109
+ else :
110
+ # Ensure output directory exists
111
+ output_path .parent .mkdir (parents = True , exist_ok = True )
110
112
111
- # Write the converted content
112
- with output_path .open ("w" , encoding = "utf-8" ) as file :
113
- file .write (mint_markdown )
113
+ # Write the converted content
114
+ with output_path .open ("w" , encoding = "utf-8" ) as file :
115
+ file .write (mint_markdown )
114
116
115
- logger .info ("Converted %s -> %s" , file_path , output_path )
117
+ logger .info ("Converted %s -> %s" , file_path , output_path )
118
+ except ParseError as e :
119
+ # We want to use logger.error rather than exception here. We do not need the
120
+ # full stack trace! ParseError should have a nice message.
121
+ logger .error ("Parse error while processing file: %s" , str (e )) # noqa: TRY400
122
+ return False
123
+ except Exception :
124
+ logger .exception ("Unexpected error while processing file %s" , file_path )
125
+ return False
126
+
127
+ return True
116
128
117
129
118
130
def _determine_output_path (
@@ -167,6 +179,8 @@ def migrate_command(args) -> None: # noqa: ANN001
167
179
logger .info ("No %s files found in %s" , file_types , input_path )
168
180
return
169
181
182
+ logger .info ("Found %d files to migrate" , len (files_to_migrate ))
183
+
170
184
if input_path .is_dir () and args .output and not args .output .exists ():
171
185
# Create output directory if it doesn't exist
172
186
args .output .mkdir (parents = True , exist_ok = True )
@@ -175,24 +189,43 @@ def migrate_command(args) -> None: # noqa: ANN001
175
189
if len (files_to_migrate ) > 1 :
176
190
logger .info ("Processing %d files..." , len (files_to_migrate ))
177
191
178
- with tqdm (
179
- files_to_migrate , desc = "Migrating files" , disable = len (files_to_migrate ) == 1
180
- ) as pbar :
181
- for file_path in pbar :
182
- pbar .set_description (f"Processing { file_path .name } " )
192
+ successful_files = 0
193
+ failed_files = 0
183
194
184
- output_path = _determine_output_path (
185
- input_path , file_path , args , migration_type
186
- )
195
+ for file_path in files_to_migrate :
196
+ logger .info ("Processing %s" , file_path .name )
187
197
188
- _process_single_file (
189
- file_path ,
190
- output_path ,
191
- dry_run = args .dry_run ,
192
- migration_type = migration_type ,
193
- )
198
+ output_path = _determine_output_path (
199
+ input_path , file_path , args , migration_type
200
+ )
201
+
202
+ success = _process_single_file (
203
+ file_path ,
204
+ output_path ,
205
+ dry_run = args .dry_run ,
206
+ migration_type = migration_type ,
207
+ )
194
208
209
+ if success :
210
+ successful_files += 1
195
211
_cleanup_original_file (file_path , args , dry_run = args .dry_run )
212
+ else :
213
+ failed_files += 1
214
+
215
+ # Report final results
216
+ if len (files_to_migrate ) > 1 :
217
+ logger .info (
218
+ "Migration completed: %d successful, %d failed out of %d total files" ,
219
+ successful_files ,
220
+ failed_files ,
221
+ len (files_to_migrate ),
222
+ )
223
+ if failed_files > 0 :
224
+ logger .warning (
225
+ "%d files failed to migrate. Check the error messages above for "
226
+ "details." ,
227
+ failed_files ,
228
+ )
196
229
197
230
198
231
def main () -> None :
0 commit comments