"""Commands are used to harness the power of nbdev."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_commands.ipynb.

# %% auto 0
__all__ = ['console', 'error_console', 'nbdev_new', 'check', 'new']

# %% ../nbs/01_commands.ipynb 2
import types, pathlib, os
from functools import wraps
import typer
from typing_extensions import Annotated
from fastcore.docments import *
from fastcore.meta import delegates
from fastcore.script import call_parse
from fastcore.utils import *
from rich import print
from rich.console import Console
from shutil import which

# %% ../nbs/01_commands.ipynb 3
from nbdev import cli, release, config, quarto, doclinks, merge, migrate, sync
from nbdev import clean as nbclean
from nbdev import test as nbtest

# %% ../nbs/01_commands.ipynb 4
console = Console()
error_console = Console(stderr=True, style="bold red")

# %% ../nbs/01_commands.ipynb 5
def check():
    """
    Check that all the components are configured.
    
    Usage:    
    
    * `nbz check`
    
    ---
    
    Learn more [nbz.answer.ai/commands#check](https://nbz.answer.ai/commands#check)
    """
    errors=[]
    if which('quarto') is None:
        errors.append('[red]Quarto not yet installed.[/red] Fix: [b]nbz install-quarto[/b]')
    if not os.getenv('GITHUB_TOKEN') and not os.getenv('GITHUB_JWT_TOKEN'): 
        errors.append('[red]No github token.[/red] Fix: Set environment variable for `GITHUB_TOKEN` or `GITHUB_JWT_TOKEN`')
    
    if errors:
        error_console.print('ERROR: nbdev not fully configured yet.')
        for i, error in enumerate(errors): console.print(f'{i+1}. {error}')
        raise typer.Exit(code=1)
check.rich_help_panel = 'Getting started'
check.no_args_is_help=False

# %% ../nbs/01_commands.ipynb 6
nbdev_new = cli.nbdev_new.__wrapped__ # remove callparse

@delegates(nbdev_new)
def new(
    target: Annotated[pathlib.Path, typer.Argument(help="Path to create project")],
    **kwargs):
    """
    Create an nbdev project.
    
    Usage:
    
    * In your current directory: `nbz new .`
    
    * In a different directory: `nbz new my-project`
    
    ---
    
    Learn more [nbz.answer.ai/commands#new](https://nbz.answer.ai/commands#new)
    """
    # kwargs['path'] = str(path)
    print(f'Changing directory to {target}')
    olddir = pathlib.Path('.')
    os.chdir(target)
    resp=nbdev_new(**kwargs)
    os.chdir(olddir)
    print(f'Changing directory back')
    return resp
new.rich_help_panel = 'Getting started'
new.no_args_is_help=False

