Skip to content

Commit

Permalink
fix: update package/getPackageEntry.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Jan 15, 2025
1 parent 45bdb7e commit 0ca0f5c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
20 changes: 11 additions & 9 deletions src/package/getPackageEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
9 changes: 7 additions & 2 deletions src/package/getPackageJson.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
15 changes: 11 additions & 4 deletions src/package/getPackageRootPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)){
Expand All @@ -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()
}
}
14 changes: 7 additions & 7 deletions src/package/installPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 // 忽略错误
}
/**
* 在当前项目下安装指定的包
Expand Down
2 changes: 1 addition & 1 deletion src/package/packageIsInstalled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0ca0f5c

Please sign in to comment.