CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-nrwl--tao

CLI compatibility layer for older Nx workspaces that serves as a bridge to core nx functionality

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

package-manager.mddocs/

Package Manager

Cross-platform package manager utilities for detecting and working with npm, yarn, and pnpm. Provides automatic detection, command generation, and registry operations across different package management systems.

Capabilities

Package Manager Detection

Automatically detect which package manager is being used in a project based on lock files.

/**
 * Detects package manager based on lock files in the directory
 * @param dir - Directory to check for lock files (defaults to current working directory)
 * @returns The detected package manager type
 */
export function detectPackageManager(dir?: string): PackageManager;

/** Supported package managers */
export type PackageManager = 'yarn' | 'pnpm' | 'npm';

Usage Examples:

import { detectPackageManager } from "@nrwl/tao/shared/package-manager";

// Detect in current directory
const pm = detectPackageManager();
console.log(`Using ${pm} package manager`);

// Detect in specific directory
const projectPm = detectPackageManager("/path/to/project");
console.log(`Project uses ${projectPm}`);

Command Generation

Get package manager-specific commands for common operations.

/**
 * Returns command structure for the specified package manager
 * @param packageManager - Package manager to get commands for (auto-detected if not provided)
 * @returns Object containing commands for the package manager
 */
export function getPackageManagerCommand(packageManager?: PackageManager): PackageManagerCommands;

/** Command structure interface for package managers */
export interface PackageManagerCommands {
  /** Install command (e.g., 'npm install', 'yarn install') */
  install: string;
  /** Add dependency command */
  add: string;
  /** Add dev dependency command */
  addDev: string;
  /** Remove dependency command */
  rm: string;
  /** Execute command */
  exec: string;
  /** Download and execute command */
  dlx: string;
  /** List dependencies command */
  list: string;
}

Usage Examples:

import { getPackageManagerCommand, detectPackageManager } from "@nrwl/tao/shared/package-manager";

const pm = detectPackageManager();
const commands = getPackageManagerCommand(pm);

console.log(`Install command: ${commands.install}`);
console.log(`Add dev dependency: ${commands.addDev} jest`);
console.log(`Execute script: ${commands.exec} build`);

// Example outputs for different package managers:
// npm: { install: "npm install", addDev: "npm install --save-dev", exec: "npx" }
// yarn: { install: "yarn install", addDev: "yarn add --dev", exec: "yarn" }
// pnpm: { install: "pnpm install", addDev: "pnpm add --save-dev", exec: "pnpm exec" }

Version Detection

Get the version of the detected or specified package manager.

/**
 * Gets the version of the specified package manager
 * @param packageManager - Package manager to check (auto-detected if not provided)
 * @returns Version string of the package manager
 */
export function getPackageManagerVersion(packageManager?: PackageManager): string;

Usage Examples:

import { getPackageManagerVersion, detectPackageManager } from "@nrwl/tao/shared/package-manager";

const pm = detectPackageManager();
const version = getPackageManagerVersion(pm);
console.log(`${pm} version: ${version}`);

// Check specific package manager version
const yarnVersion = getPackageManagerVersion('yarn');
console.log(`Yarn version: ${yarnVersion}`);

Configuration Management

Find and work with package manager configuration files.

/**
 * Finds .npmrc files in the project hierarchy
 * @param directory - Directory to start searching from (defaults to current working directory)
 * @returns Path to the .npmrc file or null if not found
 */
export function checkForNPMRC(directory?: string): string | null;

Usage Examples:

import { checkForNPMRC } from "@nrwl/tao/shared/package-manager";

const npmrcPath = checkForNPMRC();
if (npmrcPath) {
  console.log(`Found .npmrc at: ${npmrcPath}`);
} else {
  console.log("No .npmrc file found");
}

// Check in specific directory
const projectNpmrc = checkForNPMRC("/path/to/project");

Temporary Operations

Create temporary directories for isolated package operations.

/**
 * Creates temporary directory for package operations
 * @returns Object with temporary directory path and cleanup function
 */
export function createTempNpmDirectory(): { dir: string; cleanup: () => Promise<void> };

Usage Examples:

import { createTempNpmDirectory } from "@nrwl/tao/shared/package-manager";

async function isolatedPackageOperation() {
  const { dir, cleanup } = createTempNpmDirectory();
  
  try {
    console.log(`Working in temporary directory: ${dir}`);
    // Perform package operations in isolated environment
  } finally {
    await cleanup(); // Always clean up
  }
}

Registry Operations

Resolve package versions and download packages from registries.

/**
 * Resolves package versions via npm registry
 * @param packageName - Name of the package to resolve
 * @param version - Version or version range to resolve
 * @returns Promise resolving to the exact version
 */
export function resolvePackageVersionUsingRegistry(packageName: string, version: string): Promise<string>;

/**
 * Resolves versions by installing in temp directory
 * @param packageName - Name of the package to resolve
 * @param version - Version or version range to resolve
 * @returns Promise resolving to the exact version
 */
export function resolvePackageVersionUsingInstallation(packageName: string, version: string): Promise<string>;

/**
 * Executes npm/pnpm view commands
 * @param pkg - Package name
 * @param version - Package version
 * @param args - Additional arguments for the view command
 * @returns Promise resolving to command output
 */
export function packageRegistryView(pkg: string, version: string, args: string): Promise<string>;

/**
 * Downloads package tarballs
 * @param cwd - Working directory
 * @param pkg - Package name
 * @param version - Package version
 * @returns Promise resolving to tarball path
 */
export function packageRegistryPack(cwd: string, pkg: string, version: string): Promise<{ tarballPath: string }>;

Usage Examples:

import { 
  resolvePackageVersionUsingRegistry, 
  packageRegistryView,
  packageRegistryPack 
} from "@nrwl/tao/shared/package-manager";

// Resolve exact version from registry
const exactVersion = await resolvePackageVersionUsingRegistry("@nx/workspace", "^15.0.0");
console.log(`Resolved version: ${exactVersion}`);

// Get package information
const packageInfo = await packageRegistryView("@nx/workspace", "latest", "description");
console.log(`Package description: ${packageInfo}`);

// Download package tarball
const { tarballPath } = await packageRegistryPack("/tmp", "@nx/workspace", "15.9.7");
console.log(`Downloaded tarball to: ${tarballPath}`);

Integration with Workspace Operations

The package manager utilities integrate with other @nrwl/tao APIs:

import { detectPackageManager, getPackageManagerCommand } from "@nrwl/tao/shared/package-manager";
import { Workspaces } from "@nrwl/tao/shared/workspace";
import { logger } from "@nrwl/tao/shared/logger";

async function setupWorkspaceDependencies(root: string) {
  const workspaces = new Workspaces(root);
  const nxConfig = workspaces.readNxJson();
  
  const pm = detectPackageManager(root);
  const commands = getPackageManagerCommand(pm);
  
  logger.info(`Using ${pm} package manager`);
  logger.info(`Install command: ${commands.install}`);
  
  // Configure CLI to use detected package manager
  if (nxConfig.cli) {
    nxConfig.cli.packageManager = pm;
  }
}

Install with Tessl CLI

npx tessl i tessl/npm-nrwl--tao

docs

angular-cli-adapter.md

configuration.md

index.md

logging.md

package-manager.md

project-graph.md

tree-api.md

workspace-management.md

workspace-root.md

tile.json