0.2.3 • Published 4 years ago
safe-merge-files v0.2.3
safe-merge-files
Merge two files like git
Install
npm install safe-merge-files
API
safeMergeFiles(oldFile, newFile, options)
Content of oldFile
and newFile
will be merged and written into newFile
.
If conflict occurs, it will create git like merge conflict result which should be resolved manually. To resolve conflict automatically, use force
option.
oldFile
<string> Old filenamenewFile
<string> New filenameoptions
<object> -outputFile
<string> Defaults tonull
. If specified, the merged output will be written inoutputFile
instead ofnewFile
-force
<boolean> Defaults tofalse
. If set totrue
, conflicts will be resolved by prefering new changes.callback
<Function> - Error <Error> Emitted when an error occurs. - Change Flag <Integer> 0 = no change, 1 = no conflict, -1 = conflict
safeMergeFiles.Sync(oldFile, newFile, options)
Content of oldFile
and newFile
will be merged and written into newFile
.
If conflict occurs, it will create git like merge conflict result which should be resolved manually. To resolve conflict automatically, use force
option.
oldFile
<string> | <ReadStream> Old filename or a Readable StreamnewFile
<string> | <ReadStream> New filename or a Readable Streamoptions
<object> -outputFile
<string> Defaults tonull
. If specified, the merged output will be written inoutputFile
instead ofnewFile
-force
<boolean> Defaults tofalse
. If set totrue
, conflicts will be resolved by prefering new changes. -stream
<boolean> Defaults tofalse
. If set to true, it returns a Readable Stream that can be consumed or piped. - Returns: <ReadStream> | <Integer> Ifoptions.stream
isfalse
, any of 0 = no change, 1 = no conflict, -1 = conflict is returned.
Usage
Simple
var safeMergeFiles = require('safe-merge-files');
safeMergeFiles('before-change.txt', 'after-change.txt',function(err,change){
if(change==0) console.log('No change');
else if(change==1) console.log('Modified');
else console.log('Conflict - please resolve it mannually');
})
Force Apply Incoming Changes
var safeMergeFiles = require('safe-merge-files');
safeMergeFiles('before-change.txt', 'after-change.txt',{
force: true
},function(err,change){
if(change==0) console.log('No change');
else if(change==1) console.log('Modified');
else console.log('Conflict - resolved');
})
Sync
var safeMergeFiles = require('safe-merge-files');
var stream= safeMergeFiles.Sync(fs.createReadStream("old_file"), fs.createReadStream("new_file"), {
stream:true
});
var output=fs.createWriteStream("output_file");
stream.pipe(output);
Run Test
npm test