import path from "path" ; import type { PackageRef } from "./package" ; import { findPackageDir , readPackage , parsePackageRef } from "./package" ; /**
 * Module reference relative to a package, such as `react-native` or
 * `@rnx-kit/tools/node/index`.
 */ export type PackageModuleRef = PackageRef & { path ? : string ; } ; /**
 * Module reference rooted to a file system location, either relative
 * to a directory, or as an absolute path. For example, `./index` or
 * `/repos/rnx-kit/packages/tools/src/index`.
 */ export type FileModuleRef = { path : string ; } ; param moduleRef return export function parseModuleRef ( r ) { if ( r . startsWith ( "." ) || path . isAbsolute ( r ) ) { return { path : r } ; } const ref = parsePackageRef ( r ) ; const indexPath = ref . name . indexOf ( "/" ) ; if ( indexPath > - 1 ) { const p = ref . name . substring ( indexPath + 1 ) ; if ( p ) { ref . path = p ; } ref . name = ref . name . substring ( 0 , indexPath ) ; } return ref ; } param r returns export function isPackageModuleRef ( r ) { return "name" in r ; } param r returns export function isFileModuleRef ( r ) { return ! isPackageModuleRef ( r ) ; } param modulePath returns export function getPackageModuleRefFromModulePath ( modulePath ) { const pkgDir = findPackageDir ( modulePath ) ; if ( pkgDir ) { const manifest = readPackage ( pkgDir ) ; const ref = parsePackageRef ( manifest . name ) ; ref . path = path . relative ( pkgDir , modulePath ) ; return ref ; } return undefined ; }