Detect Conflict

1.0.1 · active · verified Sun Apr 19

detect-conflict is a focused utility library designed to determine if new content for a file can be safely merged with an existing file on disk without introducing conflicts. It achieves this by performing a content-based comparison, returning `true` if a conflict is detected and `false` otherwise. The current stable version is 1.0.1. As a specialized utility, its release cadence is generally slow, with updates primarily addressing bug fixes or minor enhancements rather than frequent new features. It differentiates itself by providing a simple, direct API for conflict *detection* rather than full merging capabilities, making it suitable for use cases like code generators, scaffolding tools, or deployment scripts that need to prevent overwriting user modifications.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `detect-conflict` to check if new content for a file will conflict with the content currently on disk, showing both a conflicting and non-conflicting scenario.

import conflict from 'detect-conflict';
import fs from 'fs';
import path from 'path';

const filePath = path.join(process.cwd(), 'example-file.js');
const existingContent = 'console.log("Hello world!");\nconst foo = 1;';
const newContent = 'console.log("Greetings!");\nconst foo = 2;'; // This will conflict on 'foo'
const safeContent = 'console.log("New message!");\nconst bar = 3;'; // This should not conflict

// Ensure the file exists for demonstration
fs.writeFileSync(filePath, existingContent, 'utf8');

console.log(`Checking for conflict in ${filePath}`);

const isConflicting = conflict(filePath, newContent);
console.log(`New content conflicts with existing: ${isConflicting}`);

const isConflictingSafe = conflict(filePath, safeContent);
console.log(`Safe content conflicts with existing: ${isConflictingSafe}`);

// Clean up the created file
fs.unlinkSync(filePath);

view raw JSON →