import { pickValues } from "@rnx-kit/tools-language/properties" ; import findUp from "find-up" ; import fs from "fs" ; import path from "path" ; import pkgDir from "pkg-dir" ; import pkgUp from "pkg-up" ; /**
 * Components of a package reference.
 */ export type PackageRef = { scope ? : string ; name : string ; } ; param r returns export function parsePackageRef ( r ) { if ( r . startsWith ( "@" ) ) { const indexSeparator = r . indexOf ( "/" ) ; if ( indexSeparator < 2 || indexSeparator + 2 >= r . length ) { throw new Error ( `Invalid package reference: "${ r }"` ) ; } return { scope : r . substring ( 0 , indexSeparator ) , name : r . substring ( indexSeparator + 1 ) } ; } if ( ! r ) { throw new Error ( `Invalid package reference: "${ r }"` ) ; } return { name : r } ; } param ref returns export function getMangledPackageName ( ref ) { if ( ref . scope ) { return ref . scope . slice ( 1 ) + "__" + ref . name ; } return ref . name ; } /**
 * Schema for a reference to a person in `package.json`.
 */ export type PackagePerson = string | { name : string ; email ? : string ; url ? : string ; } ; /**
 * Schema for the contents of a `package.json` manifest file.
 */ export type PackageManifest = { name : string ; version : string ; private ? : boolean ; typings ? : string ; types ? : string ; scripts ? : Record < string , string > ; dependencies ? : Record < string , string > ; peerDependencies ? : Record < string , string > ; devDependencies ? : Record < string , string > ; author ? : PackagePerson ; "rnx-kit" ? : Record < string , unknown > ; [ key ] : string | boolean | string [ ] | Record < string , unknown > | undefined ; } ; param manifest returns export function isPackageManifest ( manifest ) { return ( typeof manifest === "object" && manifest !== null && "name" in manifest && "version" in manifest ) ; } param pkgPath returns function resolvePackagePath ( pkgPath ) { if ( path . basename ( pkgPath ) . toLowerCase ( ) !== "package.json" ) { return path . join ( pkgPath , "package.json" ) ; } return pkgPath ; } param pkgPath returns export function readPackage ( pkgPath ) { const pkgFile = resolvePackagePath ( pkgPath ) ; return JSON . parse ( fs . readFileSync ( pkgFile , "utf-8" ) ) ; } param pkgPath param manifest param space export function writePackage ( pkgPath , manifest , space = "  " ) { const pkgFile = resolvePackagePath ( pkgPath ) ; fs . writeFileSync ( pkgFile , JSON . stringify ( manifest , undefined , space ) + "\n" , "utf-8" ) ; } param startDir returns export function findPackage ( startDir ? ) { return pkgUp . sync ( { cwd : startDir ? startDir : process . cwd ( ) } ) ?? undefined ; } param startDir returns export function findPackageDir ( startDir ? ) { return pkgDir . sync ( startDir ? startDir : process . cwd ( ) ) ?? undefined ; } /**
 * Options which control how package dependecies are located.
 */ export type FindPackageDependencyOptions = { /**
     * Optional starting directory for the search. Defaults to `process.cwd()`.
     */ startDir ? : string ; /**
     * Optional flag controlling whether or symlinks can be found. Defaults to `true`.
     * When `false`, and the package dependency directory is a symlink, it will not
     * be found.
     */ allowSymlinks ? : boolean ; } ; param ref param options returns export function findPackageDependencyDir ( ref , options ? ) { return findUp . sync ( path . join ( "node_modules" , ref . scope ?? "" , ref . name ) , { ... pickValues ( options ?? { } , [ "startDir" , "allowSymlinks" ] , [ "cwd" , "allowSymlinks" ] ) , type : "directory" } ) ; }