Node.js Directory Comparison Utility

5.0.0 · active · verified Sun Apr 19

dir-compare is a Node.js library for comparing the contents and structure of two directories. It provides both synchronous (`compareSync`) and asynchronous (`compare`) comparison methods, supporting various strategies like size, content, and date comparison, along with advanced filtering options including glob patterns and `.gitignore` rules. The current stable version is 5.0.0, with regular updates addressing features, performance, and bug fixes. Key differentiators include its TypeScript support (since v4.0.0), significant performance improvements for large directory structures (e.g., 3x reduced heap usage and 2x faster content comparison since v4.0.0), and flexible extension points for custom comparators and result builders. The command-line interface (CLI) was moved to a separate package, `dir-compare-cli`, in v3.0.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates both synchronous and asynchronous directory comparison using `dir-compare`. It creates temporary directories, populates them with files, and then compares them using options for size and content, while excluding a specific file. It logs the comparison summary and detailed differences.

import { compare, compareSync, Options, Result } from 'dir-compare';
import * as path from 'path';
import * * as fs from 'fs';

// Create dummy directories and files for demonstration
const dir1 = path.join(__dirname, 'test-dir1');
const dir2 = path.join(__dirname, 'test-dir2');

fs.mkdirSync(dir1, { recursive: true });
fs.mkdirSync(dir2, { recursive: true });
fs.writeFileSync(path.join(dir1, 'fileA.txt'), 'Content A');
fs.writeFileSync(path.join(dir1, 'fileB.txt'), 'Content B');
fs.writeFileSync(path.join(dir2, 'fileA.txt'), 'Content A');
fs.writeFileSync(path.join(dir2, 'fileC.txt'), 'Content C');

const options: Options = { compareSize: true, compareContent: true, excludeFilter: 'fileB.txt' };

console.log('--- Synchronous Comparison ---');
try {
  const resSync: Result = compareSync(dir1, dir2, options);
  console.log('Directories are %s', resSync.same ? 'identical' : 'different');
  console.log('Equal entries: %s, Distinct entries: %s, Left only: %s, Right only: %s', 
    resSync.equal, resSync.distinct, resSync.left, resSync.right);
  resSync.diffSet.forEach(dif => {
    console.log(`- Path: ${dif.relativePath}, Name1: ${dif.name1}, Name2: ${dif.name2}, State: ${dif.state}`);
  });
} catch (error) {
  console.error('Synchronous comparison failed:', error);
}

console.log('\n--- Asynchronous Comparison ---');
compare(dir1, dir2, options)
  .then(resAsync => {
    console.log('Directories are %s', resAsync.same ? 'identical' : 'different');
    console.log('Equal entries: %s, Distinct entries: %s, Left only: %s, Right only: %s', 
      resAsync.equal, resAsync.distinct, resAsync.left, resAsync.right);
    resAsync.diffSet.forEach(dif => {
      console.log(`- Path: ${dif.relativePath}, Name1: ${dif.name1}, Name2: ${dif.name2}, State: ${dif.state}`);
    });
  })
  .catch(error => console.error('Asynchronous comparison failed:', error))
  .finally(() => {
    // Cleanup dummy directories
    fs.rmSync(dir1, { recursive: true, force: true });
    fs.rmSync(dir2, { recursive: true, force: true });
  });

view raw JSON →