or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-yarnpkg--cli

Modern package manager CLI for Node.js and other languages with plugin support, workspaces, and advanced dependency management features

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@yarnpkg/cli@4.9.x

To install, run

npx @tessl/cli install tessl/npm-yarnpkg--cli@4.9.0

index.mddocs/

Yarn CLI

Yarn CLI provides a programmatic interface for the Yarn package manager, enabling developers to integrate Yarn's functionality into their applications. It offers APIs for creating CLI instances, executing commands, and extending functionality through plugins.

Package Information

  • Package Name: @yarnpkg/cli
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @yarnpkg/cli

Core Imports

import { getCli, runExit, BaseCommand, type YarnCli, type CommandContext } from "@yarnpkg/cli";

For CommonJS:

const { getCli, runExit, BaseCommand } = require("@yarnpkg/cli");

Basic Usage

import { getCli, runExit, getPluginConfiguration } from "@yarnpkg/cli";
import { ppath } from "@yarnpkg/fslib";

// Create a Yarn CLI instance with default plugins
const cli = await getCli({
  cwd: ppath.cwd(),
  pluginConfiguration: getPluginConfiguration()
});

// Process and execute a command
const command = cli.process(["install"], cli.defaultContext);
const exitCode = await cli.run(command, cli.defaultContext);

// Or run and exit (for standalone applications)
await runExit(["install"], {
  cwd: ppath.cwd(),
  selfPath: null,
  pluginConfiguration: getPluginConfiguration()
});

Architecture

Yarn CLI is built around several key components:

  • CLI Factory: getCli() function creates configured CLI instances with plugin support
  • Command Runner: runExit() executes commands and handles process lifecycle
  • Base Command Class: BaseCommand provides foundation for creating custom Yarn commands
  • Plugin System: Dynamic plugin loading and configuration through getPluginConfiguration()
  • Workspace Tools: Utilities for working with Yarn workspaces and projects
  • Error Handling: Specialized error classes for workspace and command validation

Capabilities

CLI Instance Creation

Create and configure Yarn CLI instances with plugin support and workspace awareness.

/**
 * Creates a Yarn CLI instance with initialized commands
 * @param options - Configuration options
 * @returns Promise that resolves to a configured YarnCli instance
 */
function getCli(options?: {
  cwd?: PortablePath;
  pluginConfiguration?: PluginConfiguration;
}): Promise<YarnCli>;

/**
 * Yarn CLI instance with command registration and execution capabilities
 */
interface YarnCli extends Cli<CommandContext> {
  /** Default context object used for command execution */
  readonly defaultContext: CommandContext & {
    cwd: PortablePath;
    plugins: PluginConfiguration;
    quiet: boolean;
    stdin: NodeJS.ReadableStream;
    stdout: NodeJS.WritableStream;
    stderr: NodeJS.WritableStream;
  };
  
  /** Binary label displayed in help output */
  readonly binaryLabel: string;
  
  /** Binary name used in command line */
  readonly binaryName: string;
  
  /** Binary version string */
  readonly binaryVersion: string | null;
  
  /** Register a command class with the CLI */
  register(command: new () => Command<CommandContext>): void;
  
  /** Process command line arguments and return command instance */
  process(argv: Array<string>, context: CommandContext): Command<CommandContext>;
  
  /** Execute a command instance */
  run(command: Command<CommandContext>, context: CommandContext): Promise<number>;
  
  /** Format an error for display */
  error(error: Error): string;
}

Command Execution

Execute Yarn commands programmatically with full process lifecycle management.

/**
 * Runs Yarn CLI with the given arguments and exits the process
 * @param argv - Command line arguments array
 * @param options - Execution options
 */
function runExit(
  argv: Array<string>,
  options: {
    cwd: PortablePath;
    selfPath: PortablePath | null;
    pluginConfiguration: PluginConfiguration;
  }
): Promise<void>;

Command Development

Base class for creating custom Yarn commands with proper context handling.

/**
 * Base class for creating Yarn commands
 */
abstract class BaseCommand extends Command<CommandContext> {
  /** Hidden cwd option for command-line usage */
  cwd: string | undefined;
  
  /** Must be implemented by subclasses */
  abstract execute(): Promise<number | void>;
  
  /** Validates cwd option and executes command */
  validateAndExecute(): Promise<number>;
}

Workspace Management

Utilities for working with Yarn workspaces and handling workspace-specific operations.

/**
 * Opens a workspace at the given directory
 * @param configuration - Configuration object
 * @param cwd - Working directory path
 * @returns Promise that resolves to Workspace object
 * @throws WorkspaceRequiredError if not in a workspace
 */
function openWorkspace(
  configuration: Configuration,
  cwd: PortablePath
): Promise<Workspace>;

/**
 * Error thrown when a command requires a workspace context
 */
class WorkspaceRequiredError extends UsageError {
  constructor(projectCwd: PortablePath, cwd: PortablePath);
}

Plugin Configuration

Manage Yarn's plugin system and dynamic library loading.

/**
 * Gets the standard plugin configuration for Yarn CLI
 * @returns Configuration object with plugins and modules
 */
function getPluginConfiguration(): PluginConfiguration;

/**
 * Returns a map of dynamic libraries available to plugins
 * @returns Map containing Yarn modules and dependencies
 */
function getDynamicLibs(): Map<string, any>;

/**
 * Map of plugin names to their command arrays (auto-generated)
 */
const pluginCommands: Map<string, Array<string>>;

Types

/**
 * Context object passed to Yarn commands with execution environment
 */
interface CommandContext {
  /** Current working directory */
  cwd: PortablePath;
  /** Plugin configuration */
  plugins: PluginConfiguration;
  /** Whether to suppress output */
  quiet: boolean;
  /** Standard input stream */
  stdin: NodeJS.ReadableStream;
  /** Standard output stream */
  stdout: NodeJS.WritableStream;
  /** Standard error stream */
  stderr: NodeJS.WritableStream;
  /** Additional properties from @yarnpkg/core CommandContext */
  [key: string]: any;
}

/**
 * Plugin configuration interface for managing Yarn plugins
 */
interface PluginConfiguration {
  /** Set of plugin names to load */
  plugins: Set<string>;
  /** Map of module names to their implementations */
  modules: Map<string, any>;
}

/**
 * Portable path type from @yarnpkg/fslib
 */
type PortablePath = import("@yarnpkg/fslib").PortablePath;

/**
 * Configuration object from @yarnpkg/core
 */
type Configuration = import("@yarnpkg/core").Configuration;

/**
 * Workspace object from @yarnpkg/core
 */
type Workspace = import("@yarnpkg/core").Workspace;

Error Handling

The CLI properly handles various error conditions:

  • Node Version Validation: Ensures compatible Node.js version (>=18.12.0)
  • Workspace Errors: WorkspaceRequiredError when workspace context is required
  • Configuration Errors: Proper error reporting for invalid configurations
  • Plugin Errors: Graceful handling of plugin loading failures

Advanced Usage

Creating Custom Commands

import { BaseCommand } from "@yarnpkg/cli";
import { Option } from "clipanion";

class MyCustomCommand extends BaseCommand {
  static paths = [["my", "command"]];
  
  myOption = Option.String("--option", "default-value");
  
  async execute() {
    this.context.stdout.write(`Option value: ${this.myOption}\n`);
    return 0;
  }
}

// Register with CLI instance
const cli = await getCli();
cli.register(MyCustomCommand);

Working with Workspaces

import { openWorkspace, getPluginConfiguration } from "@yarnpkg/cli";
import { Configuration } from "@yarnpkg/core";
import { ppath } from "@yarnpkg/fslib";

try {
  const configuration = await Configuration.find(
    ppath.cwd(),
    getPluginConfiguration()
  );
  
  const workspace = await openWorkspace(configuration, ppath.cwd());
  console.log(`Workspace: ${workspace.locator.name}`);
} catch (error) {
  if (error instanceof WorkspaceRequiredError) {
    console.error("Not in a workspace directory");
  }
}