or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lerna--diff

Lerna command that provides git diff functionality for monorepo projects to show changes since the last release

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@lerna/diff@6.6.x

To install, run

npx @tessl/cli install tessl/npm-lerna--diff@6.6.0

index.mddocs/

@lerna/diff

@lerna/diff is a Lerna command that provides git diff functionality for monorepo projects. It allows developers to see changes in all packages or a specific package since the last release, integrating seamlessly with Lerna's package graph system and git operations.

Package Information

  • Package Name: @lerna/diff
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @lerna/diff or included with lerna

Core Imports

// Main factory function (default export)
const factory = require("@lerna/diff");

// Access DiffCommand class from factory module
const { DiffCommand } = require("@lerna/diff");

// Command module for yargs integration  
const command = require("@lerna/diff/command");

// Utility functions
const { getLastCommit } = require("@lerna/diff/lib/get-last-commit");
const { hasCommit } = require("@lerna/diff/lib/has-commit");

Basic Usage

CLI Usage

# Diff all packages since last release
lerna diff

# Diff specific package since last release
lerna diff my-package

# Ignore certain file patterns
lerna diff --ignore-changes "*.md" "test/**"

# Include ignored files (overrides lerna.json ignoreChanges configuration)
lerna diff --no-ignore-changes

Programmatic Usage

const factory = require("@lerna/diff");
const { DiffCommand } = require("@lerna/diff");

// Create command instance using factory
const diffCommand = factory(process.argv);

// Or directly instantiate DiffCommand
const command = new DiffCommand(process.argv);
await command.runner();

Capabilities

Factory Function

Creates a new DiffCommand instance for programmatic usage.

/**
 * Factory function to create DiffCommand instance
 * @param argv - Process arguments array
 * @returns DiffCommand instance ready for execution
 */
function factory(argv: NodeJS.Process["argv"]): DiffCommand;

DiffCommand Class

Main command class that handles git diff operations for Lerna packages.

/**
 * Lerna command for showing git diff since last release
 * Extends base Command class with diff-specific functionality
 */
class DiffCommand extends Command<DiffCommandOptions> {
  /**
   * Initialize the diff command
   * - Validates package name if provided
   * - Checks repository has commits
   * - Builds git diff arguments with appropriate filters
   */
  initialize(): void;

  /**
   * Execute the git diff command
   * Spawns git diff process with constructed arguments and displays output
   * @returns Promise that resolves when git diff completes successfully or rejects on error
   */
  execute(): Promise<void>;
}

interface DiffCommandOptions extends CommandConfigOptions {
  /** Optional package name to filter diff output */
  pkgName: string;
  /** Array of glob patterns to ignore in diff output */
  ignoreChanges: string[];
}

Command Definition

Yargs command module for CLI integration.

/**
 * Yargs CommandModule for lerna diff command
 * Defines CLI interface with arguments and options
 */
const command: CommandModule = {
  command: "diff [pkgName]";
  describe: "Diff all packages or a single package since the last release";
  
  /**
   * Configure command arguments and options
   * Configures pkgName positional argument and ignore-changes option
   * Includes epilogue help text about ignoreChanges configuration
   * @param yargs - Yargs instance for configuration
   * @returns Configured yargs instance with positional and options
   */
  builder(yargs: any): any;
  
  /**
   * Handle command execution
   * Calls the factory function with parsed arguments
   * @param argv - Parsed command arguments
   * @returns Result from factory function execution
   */
  handler(argv: any): any;
};

CLI Configuration Details

The builder function configures the following options:

  • pkgName (positional): Optional package name to filter diff output
  • ignore-changes (option): Array type option to ignore files matched by glob patterns
  • epilogue: Help text explaining --no-ignore-changes flag to override lerna.json settings

Utility Functions

Get Last Commit

Determines the reference point for diff comparison.

/**
 * Get the last commit or tag for diff comparison
 * Returns latest reachable tag if available, otherwise first commit
 * @param execOpts - Child process execution options
 * @returns Commit SHA or tag reference string
 */
function getLastCommit(execOpts: ExecOpts): string;

Has Commit

Validates repository has commit history before running diff.

/**
 * Check if repository has any commits
 * Used to validate diff operation is possible
 * @param opts - Child process execution options  
 * @returns True if repository has commits, false otherwise
 */
function hasCommit(opts: ExecOpts): boolean;

Types

interface DiffCommandOptions extends CommandConfigOptions {
  /** Optional package name to filter diff output to single package */
  pkgName: string;
  /** Array of glob patterns to exclude from diff output */
  ignoreChanges: string[];
}

/**
 * Child process execution options
 * Based on @lerna/child-process ExecOpts
 */
interface ExecOpts {
  /** Working directory for command execution */
  cwd: string;
  /** Maximum buffer size for command output */
  maxBuffer?: number;
}

Error Handling

The command throws specific validation errors during initialization:

  • ENOPKG: Thrown when specified package name doesn't exist in the monorepo with message "Cannot diff, the package '{packageName}' does not exist."
  • ENOCOMMITS: Thrown when repository has no commit history to diff against with message "Cannot diff, there are no commits in this repository yet."

The execute method handles git diff process errors by:

  • Catching process spawn errors and re-throwing if exitCode is truthy (non-zero)
  • Allowing successful exits (exitCode 0 or falsy) to complete normally
  • Treats quitting the diff viewer as a normal operation, not an error
const { ValidationError } = require("@lerna/core");

// Example error handling
try {
  const command = new DiffCommand(argv);
  await command.runner();
} catch (error) {
  if (error instanceof ValidationError) {
    if (error.code === "ENOPKG") {
      console.error(`Package not found: ${error.message}`);
    } else if (error.code === "ENOCOMMITS") {
      console.error("No commits to diff against");
    }
  }
}

Implementation Details

Git Command Construction

The command constructs git diff arguments as follows:

  1. Base command: git diff [lastCommit] --color=auto
  2. Target paths: Either specific package location or all package parent directories
  3. Exclusion patterns: Applied using :(exclude,glob)pattern syntax for ignoreChanges

Integration Notes

  • Integrates with Lerna's package graph system for package validation
  • Uses @lerna/child-process for git command execution
  • Supports Lerna project configuration including ignore patterns from lerna.json
  • Compatible with Lerna's execution options and logging systems
  • Automatically detects latest tag or falls back to first commit for diff baseline
  • Uses --color=auto flag to preserve git's color output in terminals