Skip to content

Commit 503fcd6

Browse files
committed
More robust new command
1 parent da1635e commit 503fcd6

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

nbs/01_commands.ipynb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"outputs": [],
6363
"source": [
6464
"#| export\n",
65-
"console = Console()\n",
65+
"console = Console(style='bold')\n",
6666
"error_console = Console(stderr=True, style=\"bold red\")"
6767
]
6868
},
@@ -226,7 +226,7 @@
226226
" target: Annotated[pathlib.Path, typer.Argument(help=\"Path to create project\")],\n",
227227
" **kwargs):\n",
228228
" \"\"\"\n",
229-
" Create an nbdev project.\n",
229+
" Create an nbdev project. If the target directory does not exist, it creates it.\n",
230230
" \n",
231231
" Usage:\n",
232232
" \n",
@@ -238,18 +238,31 @@
238238
" \n",
239239
" Learn more [nbz.answer.ai/commands#new](https://nbz.answer.ai/commands#new)\n",
240240
" \"\"\"\n",
241-
" # kwargs['path'] = str(path)\n",
242-
" print(f'Changing directory to {target}')\n",
241+
" if not target.exists(): \n",
242+
" console.print(f'Creating {target} directory')\n",
243+
" target.mkdir()\n",
244+
" console.print(f'Changing directory to {target}')\n",
243245
" olddir = pathlib.Path('.')\n",
244246
" os.chdir(target)\n",
245247
" resp=nbdev_new(**kwargs)\n",
246248
" os.chdir(olddir)\n",
247-
" print(f'Changing directory back')\n",
249+
" console.print(f'Changing directory back')\n",
248250
" return resp\n",
249251
"new.rich_help_panel = 'Getting started'\n",
250252
"new.no_args_is_help=False\n"
251253
]
252254
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": null,
258+
"id": "5f1dda42",
259+
"metadata": {},
260+
"outputs": [],
261+
"source": [
262+
"path = pathlib.Path()\n",
263+
"path.exists"
264+
]
265+
},
253266
{
254267
"cell_type": "code",
255268
"execution_count": null,

nbz/commands.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from nbdev import test as nbtest
2525

2626
# %% ../nbs/01_commands.ipynb 4
27-
console = Console()
27+
console = Console(style='bold')
2828
error_console = Console(stderr=True, style="bold red")
2929

3030
# %% ../nbs/01_commands.ipynb 6
@@ -132,7 +132,7 @@ def new(
132132
target: Annotated[pathlib.Path, typer.Argument(help="Path to create project")],
133133
**kwargs):
134134
"""
135-
Create an nbdev project.
135+
Create an nbdev project. If the target directory does not exist, it creates it.
136136
137137
Usage:
138138
@@ -144,13 +144,15 @@ def new(
144144
145145
Learn more [nbz.answer.ai/commands#new](https://nbz.answer.ai/commands#new)
146146
"""
147-
# kwargs['path'] = str(path)
148-
print(f'Changing directory to {target}')
147+
if not target.exists():
148+
console.print(f'Creating {target} directory')
149+
target.mkdir()
150+
console.print(f'Changing directory to {target}')
149151
olddir = pathlib.Path('.')
150152
os.chdir(target)
151153
resp=nbdev_new(**kwargs)
152154
os.chdir(olddir)
153-
print(f'Changing directory back')
155+
console.print(f'Changing directory back')
154156
return resp
155157
new.rich_help_panel = 'Getting started'
156158
new.no_args_is_help=False

0 commit comments

Comments
 (0)