From a4bd46cafb4a0762db47217d4d77f95432456976 Mon Sep 17 00:00:00 2001 From: 苏向夜 Date: Sat, 30 Mar 2024 14:18:00 +0800 Subject: feat(doc): add env check for doc cli and add submodule option --- src/ipm/__main__.py | 3 ++- src/ipm/api.py | 31 +++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/ipm/__main__.py b/src/ipm/__main__.py index 6e24b00..4c57221 100644 --- a/src/ipm/__main__.py +++ b/src/ipm/__main__.py @@ -199,10 +199,11 @@ def remove(name: str = typer.Argument(help="Infini 包名")): def doc( type: str = typer.Argument("vue", help="前端框架名称"), dist: str = typer.Argument("doc", help="产出路径"), + submodule: bool = typer.Argument(False, help="部署为子模块"), ): """生成项目文档""" try: - if api.doc(Path.cwd(), type, dist, echo=True): + if api.doc(Path.cwd(), type, dist, submodule=submodule, echo=True): tada() except IPMException as err: error(str(err), echo=True) diff --git a/src/ipm/api.py b/src/ipm/api.py index ab1ec0a..88b15b4 100644 --- a/src/ipm/api.py +++ b/src/ipm/api.py @@ -3,7 +3,7 @@ from pathlib import Path from typing import Optional from ipm import project -from ipm.const import INDEX +from ipm.const import INDEX, VUE_CODE from ipm.models.lock import PackageLock, ProjectLock from ipm.project.env import new_virtualenv from ipm.project.toml_file import ( @@ -554,16 +554,27 @@ def install(target_path: StrPath, echo: bool = False) -> bool: return True -def doc(target_path: StrPath, type: str, dist: StrPath, echo: bool = False) -> bool: +def doc( + target_path: StrPath, + type: str, + dist: StrPath, + submodule: bool = False, + echo: bool = False, +) -> bool: info("构建项目文档...", echo) if type.lower() != "vue": - raise EnvironmentError("目前仅支持 Vue!") + raise EnvironmentError("目前仅支持 Vue 部署!") update("准备环境中...", echo) if not (toml_path := Path(target_path).joinpath("infini.toml")).exists(): raise FileNotFoundError( f"文件 [green]infini.toml[/green] 尚未被初始化, 你可以使用[bold green]`ipm init`[/bold green]来初始化项目." ) + if not submodule and not shutil.which("npm"): + raise EnvironmentError( + "IPM 未能在环境中找到 [bold green]Node.js[/] 安装, 请确保 NPM 在环境中被正确安装. " + "你可以前往`[green]https://nodejs.org/en/download[/]`来安装此包管理器." + ) project = InfiniProject(toml_path.parent) loader = Loader() loader.close() @@ -574,9 +585,13 @@ def doc(target_path: StrPath, type: str, dist: StrPath, echo: bool = False) -> b dist_path.mkdir(parents=True, exist_ok=True) success("环境准备完毕.", echo) - docs = json.loads(loader.doc.dumps()) - docs["metadata"] = project.metadata - docs["readme"] = project.readme - docs["doc_create_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - dist_path.joinpath("index.json").write_text(json.dumps(docs), encoding="utf-8") + if submodule: + docs = json.loads(loader.doc.dumps()) + docs["metadata"] = project.metadata + docs["readme"] = project.readme + docs["doc_create_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + dist_path.joinpath("index.json").write_text(json.dumps(docs), encoding="utf-8") + dist_path.joinpath("index.vue").write_text(VUE_CODE, encoding="utf-8") + else: + raise ProjectError("未被支持。") return True -- cgit v1.2.3-70-g09d2