UNPKG

genezio

Version:

Command line utility to interact with Genezio infrastructure.

53 lines (52 loc) 2.56 kB
import { Mutex } from "async-mutex"; import { debugLogger } from "../../utils/logging.js"; import { NODE_DEFAULT_PACKAGE_MANAGER, packageManagers, } from "../../packageManagers/packageManager.js"; /** * The `DependencyInstaller` class provides a thread-safe way to install dependencies for a Node.js project. * It keeps track of which dependencies have already been installed and only installs each dependency once. */ class DependencyInstaller { constructor() { this.alreadyInstalled = new Set(); this.mutex = new Mutex(); return DependencyInstaller.instance; } /** * Installs the specified dependencies if they have not been installed yet. * This method is thread-safe and will only install dependencies once. * @param {string[]} dependencyList - The list of dependencies to install. * @param {boolean} [noSave] - If true, the installed dependencies will not be saved to the package.json file. * @returns {Promise<void>} A promise that resolves when all specified dependencies have been installed. */ // eslint-disable-next-line @typescript-eslint/no-unused-vars async install(dependencyList, cwd, noSave, packageManagerType = NODE_DEFAULT_PACKAGE_MANAGER) { const toBeInstalled = []; await this.mutex.runExclusive(async () => { dependencyList.forEach((dependency) => { if (!this.alreadyInstalled.has(dependency)) { this.alreadyInstalled.add(dependency); toBeInstalled.push(dependency); } }); if (toBeInstalled.length === 0) { return; } debugLogger.debug(`Installing dependencies: ${toBeInstalled.join(", ")}`); // TODO: Add support for no-save for all package managers await packageManagers[packageManagerType].install(toBeInstalled, cwd); }); } /** * Installs all dependencies that have not been installed yet. * This method is thread-safe and will only install dependencies once. * @returns {Promise<void>} A promise that resolves when all dependencies have been installed. */ async installAll(cwd, packageManagerType = NODE_DEFAULT_PACKAGE_MANAGER) { await this.mutex.runExclusive(async () => { debugLogger.debug(`Installing all dependencies`); await packageManagers[packageManagerType].install([], cwd); }); } } DependencyInstaller.instance = new DependencyInstaller(); export { DependencyInstaller };