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

86 lines 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GetPackageInfo = GetPackageInfo; const tslib_1 = require("tslib"); const fs_1 = require("fs"); const https_1 = require("https"); const os_1 = require("os"); const path_1 = require("path"); const search_file_in_directory_1 = require("./search-file-in-directory"); const CACHE_FOLDER = (0, path_1.join)((0, os_1.tmpdir)(), 'rxap', 'package-info'); const PACKAGE_INFO_CACHE = (() => { const cache = {}; if ((0, fs_1.existsSync)(CACHE_FOLDER)) { for (const { content, filePath } of (0, search_file_in_directory_1.SearchFileInDirectory)(CACHE_FOLDER)) { const packageName = (0, path_1.basename)(filePath).replace('.json', '').replace(/___/g, '/'); cache[packageName] = JSON.parse(content); } } return cache; })(); /** * Updates the local cache with the provided package information and writes it to a file. * * This function updates an in-memory cache and persists the package information to the filesystem. * It ensures that the cache directory exists by creating it if necessary, and then writes the * package information to a JSON file named after the package, with special characters in the * package name replaced to ensure file system compatibility. * * @param packageName - The name of the package to update in the cache. * @param content - The NpmPackageInfo object containing the metadata of the package. */ function updatePackageInfoCache(packageName, content) { PACKAGE_INFO_CACHE[packageName] = content; (0, fs_1.mkdirSync)(CACHE_FOLDER, { recursive: true }); (0, fs_1.writeFileSync)((0, path_1.join)(CACHE_FOLDER, `${packageName.replace(/\//g, '___')}.json`), JSON.stringify(content, null, 2)); } /** * Asynchronously retrieves information about a specified npm package. * * This function fetches package information from the npm registry. If caching is enabled and the package * information is already cached, it returns the cached data to reduce network calls. If the package information * is not cached or caching is skipped, it makes an HTTP request to the npm registry. * * @param packageName The name of the npm package for which information is required. * @param skipCache Optional. If true, the function will bypass the cache and fetch data directly from the npm registry. * @returns A Promise that resolves to an `NpmPackageInfo` object containing the package information, or null if an error occurs * during the fetch operation or if the package does not exist. */ function GetPackageInfo(packageName, skipCache) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!skipCache && PACKAGE_INFO_CACHE[packageName]) { return PACKAGE_INFO_CACHE[packageName]; } return new Promise((resolve, reject) => { (0, https_1.get)(`https://registry.npmjs.org/${packageName}`, (res) => { let data = ''; // A chunk of data has been received. res.on('data', (chunk) => { data += chunk; }); // The whole response has been received. res.on('end', () => { try { const jsonData = JSON.parse(data); if (jsonData.error) { console.error(`Error getting npm package version: ${jsonData.error}`); resolve(null); } else { updatePackageInfoCache(packageName, jsonData); resolve(jsonData); } } catch (error) { console.error(`Error parsing npm JSON response: ${error.message}`); resolve(null); } }); }).on('error', (error) => { console.error(`Network Error getting npm package version: ${error.message}`); resolve(null); }); }); }); } //# sourceMappingURL=get-package-info.js.map