Lerna command that provides git diff functionality for monorepo projects to show changes since the last release
npx @tessl/cli install tessl/npm-lerna--diff@6.6.0@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.
npm install @lerna/diff or included with lerna// 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");# 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-changesconst 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();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;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[];
}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;
};The builder function configures the following options:
--no-ignore-changes flag to override lerna.json settingsDetermines 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;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;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;
}The command throws specific validation errors during initialization:
"Cannot diff, the package '{packageName}' does not exist.""Cannot diff, there are no commits in this repository yet."The execute method handles git diff process errors by:
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");
}
}
}The command constructs git diff arguments as follows:
git diff [lastCommit] --color=auto:(exclude,glob)pattern syntax for ignoreChanges@lerna/child-process for git command executionlerna.json--color=auto flag to preserve git's color output in terminals