From 0ca0f5c5b7373e46216f435d04e0155728bed0de Mon Sep 17 00:00:00 2001 From: wxzhang Date: Wed, 15 Jan 2025 11:34:28 +0800 Subject: [PATCH] fix: update package/getPackageEntry.ts --- src/package/getPackageEntry.ts | 20 +++++++++++--------- src/package/getPackageJson.ts | 9 +++++++-- src/package/getPackageRootPath.ts | 15 +++++++++++---- src/package/installPackage.ts | 14 +++++++------- src/package/packageIsInstalled.ts | 2 +- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/package/getPackageEntry.ts b/src/package/getPackageEntry.ts index 3ae3639..bd37d0b 100644 --- a/src/package/getPackageEntry.ts +++ b/src/package/getPackageEntry.ts @@ -7,18 +7,20 @@ import {getPackageJson} from "./getPackageJson" import path from "node:path" export interface GetPackageEntryOptions{ - entry?:string - absolute?:boolean + entry? : string + absolute?: boolean } -export function getPackageEntry(options:GetPackageEntryOptions){ +export function getPackageEntry(options:GetPackageEntryOptions):string | undefined{ const { entry=process.cwd(),absolute } = options || {} let entryFile const packageJson = getPackageJson(entry) - if(packageJson.main){ - entryFile = packageJson.main - }else{ - entryFile ="./src/index.ts" - } - return absolute ? path.join(entry || process.cwd(),entryFile) : entryFile + if(packageJson){ + if(packageJson.main){ + entryFile = packageJson.main + }else{ + entryFile ="./src/index.ts" + } + return absolute ? path.join(entry || process.cwd(),entryFile) : entryFile + } } \ No newline at end of file diff --git a/src/package/getPackageJson.ts b/src/package/getPackageJson.ts index eb1f409..e679248 100644 --- a/src/package/getPackageJson.ts +++ b/src/package/getPackageJson.ts @@ -1,8 +1,13 @@ +import { PackageJson } from "type-fest"; import { getPackageRootPath } from "./getPackageRootPath"; import path from "node:path" -export function getPackageJson(entry?:string){ +export function getPackageJson(entry?:string):PackageJson | undefined{ const packageRoot = getPackageRootPath(entry) as string - return require(path.join(packageRoot,"package.json")) + try{ + return require(path.join(packageRoot,"package.json")) + }catch{ + return undefined + } } \ No newline at end of file diff --git a/src/package/getPackageRootPath.ts b/src/package/getPackageRootPath.ts index df7cc46..2d19005 100644 --- a/src/package/getPackageRootPath.ts +++ b/src/package/getPackageRootPath.ts @@ -2,12 +2,19 @@ import path from "node:path" import { InvalidProjectPathError } from "../errors" import fs from "node:fs" -export function getPackageRootPath(entryPath:string="./",exclueCurrent:boolean=false):string | null{ - if(!path.isAbsolute(entryPath)){ +/** + * 获取包的根路径。 + * @param entryPath - 入口路径,默认为当前目录。 + * @param excludeCurrent - 是否排除当前目录查找。 + * @returns 返回包的根路径字符串,如果未找到则返回 null。 + * @throws {InvalidProjectPathError} 当路径无效时抛出错误。 + */ +export function getPackageRootPath(entryPath: string = "./", excludeCurrent: boolean = false): string | null { + if (!path.isAbsolute(entryPath)) { entryPath = path.join(process.cwd(),entryPath || "./") } try{ - const pkgFile =exclueCurrent ? + const pkgFile = excludeCurrent ? path.join(entryPath, "..", "package.json") : path.join(entryPath, "package.json") if(fs.existsSync(pkgFile)){ @@ -16,7 +23,7 @@ export function getPackageRootPath(entryPath:string="./",exclueCurrent:boolean=f const parent = path.dirname(entryPath) if(parent===entryPath) return null return getPackageRootPath(parent,false) - }catch(e:any){ + }catch{ throw new InvalidProjectPathError() } } diff --git a/src/package/installPackage.ts b/src/package/installPackage.ts index 17568af..1db5785 100644 --- a/src/package/installPackage.ts +++ b/src/package/installPackage.ts @@ -5,13 +5,13 @@ import { packageIsInstalled } from "./packageIsInstalled" export interface installPackageOptions{ - location?:string // 安装位置 - silent?: boolean // 执行安装时静默输出 - type?:'prod' | 'dev' | 'peer' | 'optional' | 'bundle' // 安装开发依赖 - global?: boolean // 安装为全局依赖 - upgrade?: boolean // 当依赖已经安装时是否进行升级 - use?:"auto" | string // 使用哪一个包工具 - ignoreError?:boolean // 忽略错误 + location? : string // 安装位置 + silent? : boolean // 执行安装时静默输出 + type? : 'prod' | 'dev' | 'peer' | 'optional' | 'bundle' // 安装开发依赖 + global? : boolean // 安装为全局依赖 + upgrade? : boolean // 当依赖已经安装时是否进行升级 + use? : "auto" | string // 使用哪一个包工具 + ignoreError?: boolean // 忽略错误 } /** * 在当前项目下安装指定的包 diff --git a/src/package/packageIsInstalled.ts b/src/package/packageIsInstalled.ts index 1424f64..32f9e5c 100644 --- a/src/package/packageIsInstalled.ts +++ b/src/package/packageIsInstalled.ts @@ -23,6 +23,6 @@ export async function packageIsInstalled(packageName:string,options?:PackageIsIn try{ const pkgJsonFile = path.join(packageRoot,"node_modules",packageName,"package.json") installed = fs.existsSync(pkgJsonFile) - }catch(e){} + }catch{} return installed } \ No newline at end of file