Skip to content

Commit

Permalink
🎨 refactor(cli): enhance pack directory initialization logic
Browse files Browse the repository at this point in the history
🔖 chore(pyproject): update project version to 0.1.8 and description
  • Loading branch information
sudoskys committed Sep 20, 2024
1 parent 9637864 commit 3c55de0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "tsticker"
version = "0.1.7"
description = "Default template for PDM package"
version = "0.1.8"
description = "Telegram sticker management tool"
authors = [
{ name = "sudoskys", email = "coldlando@hotmail.com" },
]
Expand Down
43 changes: 22 additions & 21 deletions src/tsticker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,28 @@ async def init(
f"[bold blue]Pack Title:[/] {pack_title}, "
f"[bold blue]Sticker Type:[/] {sticker_type}"
)
root = pathlib.Path(os.getcwd())
directories = [item for item in root.iterdir() if item.is_dir()]
# 检查是否存在多个文件夹
if len(directories) > 1:
console.print("[bold red]Multiple directories found. Please initialize in an empty directory.[/]")
return
console.print(f"[bold blue]Pack directory inited:[/] {root}")
index_file = root.joinpath("index.json")
if index_file.exists():
console.print(f"[bold red]Index file already exists:[/] {index_file}")
root_dir = pathlib.Path(os.getcwd())
# 尝试使用 Packname 创建文件夹
try:
sticker_dir = root_dir.joinpath(pack_name)
if sticker_dir.exists():
console.print(f"[bold red]Pack directory already exists:[/] {sticker_dir}")
return
sticker_dir.mkdir(exist_ok=False)
except Exception as e:
console.print(f"[bold red]Failed to create pack directory: {e}[/]")
return

console.print(f"[bold blue]Pack directory inited:[/] {sticker_dir}")
index_file = sticker_dir.joinpath("index.json")
index_file.write_text(
StickerPack.create(
title=bot_setting.pack_title,
name=bot_setting.make_set_name(bot_setting.pack_name, bot_setting.bot_user.username),
sticker_type=bot_setting.sticker_type,
operator_id=str(bot_setting.bot_user.id)
).model_dump_json(indent=2)
)
# 创建 App
app = StickerApp(bot_setting)
with console.status("[bold blue]Retrieving sticker...[/]", spinner='dots'):
try:
Expand All @@ -324,17 +334,8 @@ async def init(
else:
console.print(f"[bold red]Failed to get sticker set {bot_setting.pack_name}: {e}[/]")
return

index_file.write_text(
StickerPack.create(
title=bot_setting.pack_title,
name=bot_setting.make_set_name(bot_setting.pack_name, bot_setting.bot_user.username),
sticker_type=bot_setting.sticker_type,
operator_id=str(bot_setting.bot_user.id)
).model_dump_json(indent=2)
)
# 创建资源文件夹
sticker_table_dir = root.joinpath("stickers")
sticker_table_dir = sticker_dir.joinpath("stickers")
sticker_table_dir.mkdir(exist_ok=True)
if not sticker_set:
console.print(f"[bold blue]Empty pack, and index file created:[/] {index_file}")
Expand Down

0 comments on commit 3c55de0

Please sign in to comment.