import { UpdateJsonFileOptions } from './json-file'; import { PackageJson } from './package-json'; import { TreeLike } from './tree'; import 'colors'; /** * Retrieves the `package.json` file as a JSON object from a specified directory within a tree-like structure. * * This function is a generic utility that fetches the `package.json` file, which typically contains metadata about the project such as the project name, version, and dependencies. It uses a generic type `Tree` which must conform to the `TreeLike` interface, allowing for flexibility with different tree implementations. * * @param tree - The tree-like structure from which to retrieve the `package.json` file. This structure must implement the `TreeLike` interface. * @param basePath - An optional base path within the tree where the `package.json` file is located. Defaults to the root of the tree if not specified. * @returns The `package.json` file as a JSON object of type `PackageJson`. * * @example * // Assuming `projectTree` is an object conforming to `TreeLike` and contains a `package.json` at the root. * const packageJson = GetPackageJson(projectTree); * process.env['RXAP_GENERATOR_DEBUG'] === 'true' && console.log(packageJson.name); // Outputs the name property from package.json * * @example * // Assuming `projectTree` contains a `package.json` in the 'app' subdirectory. * const packageJson = GetPackageJson(projectTree, 'app'); * process.env['RXAP_GENERATOR_DEBUG'] === 'true' && console.log(packageJson.version); // Outputs the version property from package.json */ export declare function GetPackageJson(tree: Tree, basePath?: string): PackageJson; /** * Checks if a `package.json` file exists at the specified base path within the given tree structure. * * @param tree - The tree structure to search within. This should be an object that conforms to the `TreeLike` interface, which must have an `exists` method. * @param basePath - An optional base path where the search for `package.json` should start. Defaults to the root (''). * @returns `true` if a `package.json` file exists at the specified path within the tree, otherwise `false`. * * @typeparam Tree - The type of the tree structure, must extend `TreeLike`. * * @example * // Assuming `projectTree` is an object that implements `TreeLike`: * const hasPackage = HasPackageJson(projectTree, 'src/app'); * process.env['RXAP_GENERATOR_DEBUG'] === 'true' && console.log(hasPackage); // Outputs: true or false based on the existence of `package.json` in 'src/app' */ export declare function HasPackageJson(tree: Tree, basePath?: string): boolean; export interface UpdatePackageJsonOptions extends UpdateJsonFileOptions { basePath?: string; } export declare function UpdatePackageJson(tree: Tree, updaterOrJsonFile: ((packageJson: PackageJson) => void), options?: UpdatePackageJsonOptions): void; export declare function UpdatePackageJson(tree: Tree, updaterOrJsonFile: ((packageJson: PackageJson) => Promise), options?: UpdatePackageJsonOptions): Promise; /** * Adds a new script or updates an existing script in the `package.json` file of a project. * * This function leverages the `UpdatePackageJson` function to modify the `scripts` section of the `package.json`. * If the `scripts` object does not exist, it will be initialized. The specified script will then be added or updated * with the provided script content. * * @param tree - The tree-like structure representing the file system or project structure. * @param scriptName - The name of the script to add or update in the `package.json`. * @param script - The command string that the script will execute. * @param options - Optional. Configuration options for updating the `package.json` file. * @returns The result of the `UpdatePackageJson` function, typically a boolean indicating success or failure. * * @template Tree - A generic type that extends `TreeLike`, representing the structure to be manipulated. */ export declare function AddPackageJsonScript(tree: Tree, scriptName: string, script: string, options?: UpdatePackageJsonOptions): void; export interface AddPackageJsonDependencyOptions extends UpdatePackageJsonOptions { /** * true - only update the version if the current version is lower than the new version and the anticipated version is not set to latest */ soft?: boolean; /** * true (default) - also install the package peer dependencies */ withPeerDependencies?: boolean; /** * true (default) - do not install the package peer dependencies if they are not rxap packages */ withoutNonRxapPeerDependencies?: boolean; } /** * Asynchronously adds a specified package and its version to the `package.json` of a project tree, with options to handle peer dependencies and specific dependency types. * * @param tree - The project tree structure which contains the `package.json` file. * @param packageName - The name of the package to be added. * @param packageVersion - The version of the package to be added. Defaults to 'latest', which will fetch the latest version available. * @param options - Configuration options for adding the package. Includes flags for including peer dependencies and excluding non-Rxap peer dependencies. * @param propertyPath - The type of dependency under which the package should be added. Can be one of 'dependencies', 'devDependencies', 'peerDependencies', or 'optionalDependencies'. Defaults to 'dependencies'. * * The function performs several key operations: * - Resolves the actual version of the package if 'latest' is specified. * - Ensures that the package is not already present in the specified dependency category in `package.json`. * - Promotes the package to the specified dependency category if it exists under a different category. * - Cleans up any duplicate entries across different dependency categories. * - Optionally handles the addition of peer dependencies if specified in the options. * - Logs detailed information about the operations being performed, including any errors or important actions. * * Note: The function uses console logs to provide feedback and error handling. It also relies on external functions like `GetLatestPackageVersion`, `UpdatePackageJson`, and `GetPackagePeerDependencies` which are assumed to be implemented elsewhere in the application. * * @throws {Error} Throws an error if the package ends up in multiple dependency categories after cleanup. */ export declare function AddPackageJsonDependency(tree: Tree, packageName: string, packageVersion?: string | 'latest', options?: AddPackageJsonDependencyOptions, propertyPath?: 'dependencies' | 'devDependencies' | 'peerDependencies' | 'optionalDependencies'): Promise; /** * Adds a development dependency to the `package.json` file within a given project tree. * * This function is a wrapper around `AddPackageJsonDependency`, specifically setting the dependency type to 'devDependencies'. * It is designed to be used in scenarios where development-specific packages are needed, such as testing frameworks or build tools. * * @param tree - The project tree structure where the `package.json` file is located. * @param packageName - The name of the package to be added as a development dependency. * @param packageVersion - The version of the package to be added. Defaults to 'latest' if not specified. * @param options - Optional configuration settings for adding the dependency, such as custom registry configuration. * * @returns A promise that resolves when the dependency has been successfully added to the `package.json` file. * * @template Tree - The type of the tree structure that conforms to `TreeLike`, representing the project's file and folder structure. */ export declare function AddPackageJsonDevDependency(tree: Tree, packageName: string, packageVersion?: string | 'latest', options?: AddPackageJsonDependencyOptions): Promise; /** * Cleanup the packageJson object in place * @param content */ export declare function CleanupPackageJsonFile(content: PackageJson): T; /** * Retrieves the `package.json` file from the root of a project, either from a virtual file system represented by a `TreeLike` interface or from the actual file system. * * @param {TreeLike} [tree] - An optional parameter representing a tree structure. This can be either a schematic or generator tree. * @returns {PackageJson} The parsed contents of the `package.json` file as a JSON object. * * @throws {Error} Throws an error if the tree provided is neither a schematic nor a generator tree, if the `package.json` file does not exist at the expected location, or if the file is not a valid JSON object. * * ## Description * This function is designed to work with different types of project structures: * - **Schematic Tree**: If a `TreeLike` object that conforms to a schematic tree is provided, the function uses the tree's root path to locate the `package.json`. * - **Generator Tree**: For generator trees, the function uses the root path `/` to access files at the root of the virtual file system. * - **No Tree Provided**: If no tree is provided, the function defaults to using the current working directory of the process to find the `package.json`. * * The function first determines the correct root directory based on the input tree. It then constructs the path to the `package.json` file and checks for its existence. If the file exists, it reads and parses the JSON content. Finally, it validates that the parsed JSON is an object before returning it. * * This function is essential for tools and libraries that need to interact with a project's metadata stored in the `package.json` file, especially in environments where file structures are abstracted, such as during schematic or generator executions. */ export declare function GetRootPackageJson(tree?: TreeLike): PackageJson;