UNPKG

@rxap/node-utilities

Version:

Provides a set of utility functions for Node.js development, including file system operations, git integration, and package.json manipulation. It offers functionalities like copying folders, reading JSON files with retry logic, retrieving the current git

140 lines 7.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GetLatestPackageVersion = GetLatestPackageVersion; const tslib_1 = require("tslib"); const fs_1 = require("fs"); const os_1 = require("os"); const path_1 = require("path"); const get_package_info_1 = require("./get-package-info"); const json_file_1 = require("./json-file"); const CACHE_FILE = (0, path_1.join)((0, os_1.tmpdir)(), 'rxap', 'latest-package-versions.json'); const LATEST_PACKAGE_VERSIONS = (() => { if ((0, fs_1.existsSync)(CACHE_FILE)) { return JSON.parse((0, fs_1.readFileSync)(CACHE_FILE).toString()); } return {}; })(); /** * Updates the cache with the latest version of a specified package and persists the updated cache to a file. * * This function updates the in-memory cache of latest package versions, setting the specified package's version. * It ensures that the directory for the cache file exists, creating it if necessary, and then writes the updated * cache to a file in a human-readable JSON format. * * @param packageName - The name of the package to update. * @param version - The new version of the package to be set in the cache. */ function updateLastPackageVersionCache(packageName, version) { LATEST_PACKAGE_VERSIONS[packageName] = version; (0, fs_1.mkdirSync)((0, path_1.dirname)(CACHE_FILE), { recursive: true }); (0, fs_1.writeFileSync)(CACHE_FILE, JSON.stringify(LATEST_PACKAGE_VERSIONS, null, 2)); } /** * Retrieves the file path for the `package.json` of the `rxap` package within a Node.js project. * * The function first checks if the `NX_WORKSPACE_ROOT` environment variable is set, which indicates * the root directory of an Nx workspace. If set, it constructs a path to the `package.json` of the * `rxap` package within the `node_modules` directory of the workspace. If the file exists at the * constructed path, it returns this path. * * If the `NX_WORKSPACE_ROOT` is not set or the file does not exist at the constructed path, the function * then attempts to resolve the path using Node.js's `require.resolve` method, which looks up the path * of the main entry file of the `rxap` package as specified in its `package.json`. If successful, it returns * the resolved path. * * If neither method can locate the `package.json`, the function logs an error message and returns `null`. * * @returns {string | null} The file path to the `package.json` of the `rxap` package if found, otherwise `null`. */ function getRxapPackageJsonFilePath() { if (process.env['NX_WORKSPACE_ROOT']) { const workspaceRoot = process.env['NX_WORKSPACE_ROOT']; const packageJsonFilePath = (0, path_1.join)(workspaceRoot, 'node_modules', 'rxap', 'package.json'); if ((0, fs_1.existsSync)(packageJsonFilePath)) { return packageJsonFilePath; } } try { return require.resolve('rxap'); } catch (e) { console.log('Could not resolve the package rxap'); } return null; } /** * Checks if the RxAP package is present in the project. * * This function determines the existence of the RxAP package by attempting to locate its package.json file. * It utilizes the `getRxapPackageJsonFilePath` function to retrieve the path to the package.json file of the RxAP package. * If the path exists, it returns true, indicating that the RxAP package is installed; otherwise, it returns false. * * @returns {boolean} True if the RxAP package is found, otherwise false. */ function hasRxapPackage() { return !!getRxapPackageJsonFilePath(); } /** * Retrieves the `package.json` content for the `rxap` package. * * This function first determines the file path of the `package.json` for the `rxap` package by calling `getRxapPackageJsonFilePath()`. * If the file path is not found, it throws an error indicating the absence of the `package.json` file. * If the file path is found, it reads and returns the content of the `package.json` file as a JSON object of type `PackageJson`. * * @returns {PackageJson} The JSON object representing the `package.json` of the `rxap` package. * @throws {Error} If the `package.json` file path for the `rxap` package cannot be found. */ function getRxapPackageJson() { const packageJsonFilePath = getRxapPackageJsonFilePath(); if (!packageJsonFilePath) { throw new Error('Could not find the package json file for the package rxap'); } return (0, json_file_1.jsonFile)(packageJsonFilePath); } /** * Asynchronously retrieves the latest version of a specified package, optionally bypassing the cache. * * This function first checks if the package version is available in a local cache (`LATEST_PACKAGE_VERSIONS`) unless * explicitly instructed to skip this cache. If the package version is not cached or cache is skipped, it attempts to * fetch the version from a predefined package group (`nx-migrations` in `rxapPackageJson`). If the package is not found * in the predefined group, it then fetches the package information from a remote source using `GetPackageInfo`. * * If the remote fetch is successful and the package information includes distribution tags, it updates the cache with * the latest version and returns it. If the fetched package information is invalid (i.e., lacks distribution tags), * the function logs the detailed package information and throws an error. * * @param {string} packageName - The name of the package for which the latest version is to be retrieved. * @param {boolean} [skipCache=false] - Optional flag to bypass the local cache check and force a fetch from remote sources. * @returns {Promise<string | null>} A promise that resolves to the latest version string of the package, or null if the * package cannot be found or if the package information is invalid. * @throws {Error} Throws an error if the package information fetched from the remote source is invalid. */ function GetLatestPackageVersion(packageName, skipCache) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b; if (!skipCache && LATEST_PACKAGE_VERSIONS[packageName]) { return LATEST_PACKAGE_VERSIONS[packageName]; } if (hasRxapPackage()) { const rxapPackageJson = getRxapPackageJson(); const packageGroupList = (_b = (_a = rxapPackageJson['nx-migrations']) === null || _a === void 0 ? void 0 : _a.packageGroup) !== null && _b !== void 0 ? _b : []; const packageGroup = packageGroupList.find(group => group.package === packageName); if (packageGroup) { updateLastPackageVersionCache(packageName, packageGroup.version); return packageGroup.version; } } const info = yield (0, get_package_info_1.GetPackageInfo)(packageName, skipCache); if (info) { if (!info['dist-tags']) { console.log(JSON.stringify(info, null, 2)); throw new Error(`Invalid package info for ${packageName}`); } const latestVersion = info['dist-tags'].latest; updateLastPackageVersionCache(packageName, latestVersion); return latestVersion; } return null; }); } //# sourceMappingURL=get-latest-package-version.js.map