UNPKG

@ts-dev-tools/core

Version:
66 lines (65 loc) 2.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NpmPackageManagerAdapter = void 0; const AbstractPackageManagerAdapter_1 = require("./AbstractPackageManagerAdapter"); class NpmPackageManagerAdapter extends AbstractPackageManagerAdapter_1.AbstractPackageManagerAdapter { async addDevPackage(packageName, dirPath) { const isMonorepo = await this.isMonorepo(dirPath); const args = ["npm", "install", "--save-dev"]; if (isMonorepo) { args.push("--no-workspaces"); } args.push(packageName); await this.execCommand(args, dirPath, true); } async isMonorepo(dirPath) { try { await this.execCommand(["npm", "--workspaces", "list", "--json"], dirPath, true); return true; } catch { return false; } } async isPackageInstalled(packageName, dirPath) { const args = [ "npm", "list", "--depth=1", "--json", "--no-progress", "--non-interactive", packageName, ]; let output; try { output = await this.execCommand(args, dirPath, true); } catch (error) { if (typeof error === "string") { try { JSON.parse(error.trim()); output = error; } catch { return false; } } else { return false; } } const installedPackages = JSON.parse(output); return installedPackages.dependencies ? Object.hasOwn(installedPackages.dependencies, packageName) : false; } async getNodeModulesPath(dirPath) { const nodeModulesPath = (await this.execCommand(["npm", "root", "--no-progress", "--non-interactive"], dirPath, true)).trim(); if (nodeModulesPath) { return nodeModulesPath; } throw new Error("Node modules path not found for package manager npm"); } } exports.NpmPackageManagerAdapter = NpmPackageManagerAdapter;