-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
279 lines (277 loc) · 8.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
var diff = require("diff");
var NodeStream = require('stream');
var fs = require('fs');
var EE = require('events').EventEmitter;
var sleep=require('deasync').sleep;
function readStreamSync(readStream){
var output='';
var loop=true;
readStream.on('error',function(e){
loop=false;
throw e;
})
readStream.on('readable',function(){
var chunk;
if(chunk=readStream.read())
output+=chunk.toString();
else
loop=false;
});
readStream.on('close',function(){
loop=false;
})
while(loop) sleep(100);
return output;
}
function Async(oldFile,newFile,options,callback){
if(typeof options=='function'){
callback=options;
options={};
}
var op=Object.assign({
outputFile:null,
force:false
},options||{});
var source=fs.readFileSync(oldFile,{encoding:'utf8'});
var patch=diff.structuredPatch('A','B',source,fs.readFileSync(newFile,{encoding:'utf8'}),'','');
if(patch.hunks.length==0)
callback(null,0);
else{
//console.log('patch.hunks',patch.hunks);
source=source.split('\n');
var writer=fs.createWriteStream(op.outputFile || newFile);
var alreadyWritten=false;
var lastSymbol=null;
var hunkPending=Boolean(patch.hunks[0].oldStart>1);
var pendingRemoves=[];
var pendingAdditions=[];
var reader=new EE();
var conflict = false;
var needNewLineAtEnd=false;
var writePendings=function(normalLine,addToPendingRemoves,cb){
var strToBeWritten=[];
if(hunkPending){
hunkPending=false;
var preArr=[];
if(hunkIndex==0){
preArr=source.slice(0,patch.hunks[hunkIndex].oldStart-1);
}else if(hunkIndex<patch.hunks.length){
preArr=source.slice(patch.hunks[hunkIndex-1].oldStart+patch.hunks[hunkIndex-1].oldLines-1,patch.hunks[hunkIndex].oldStart-1);
}else{//lastHunk
let lastStart=patch.hunks[hunkIndex-1].oldStart+patch.hunks[hunkIndex-1].oldLines-1;
if(lastStart==source.length-1){
if(source[source.length-1]){
preArr=[source[source.length-1]];
}else{
needNewLineAtEnd=true;
}
}else if(lastStart<source.length-1){
preArr=source.slice(lastStart);
}
}
if(preArr.length)
strToBeWritten.push(preArr.join('\n'));
}
//console.log('writePendings(',normalLine,',',addToPendingRemoves,',',cb);
//console.log('pendingRemoves',pendingRemoves);
//console.log('pendingAdditions',pendingAdditions);
if(!pendingRemoves.length && pendingAdditions.length){//only addition
strToBeWritten.push(pendingAdditions.join('\n'));
}else if(pendingRemoves.length && pendingAdditions.length){//conflict
conflict=true;
if(options.force){//prefer additions
strToBeWritten.push(pendingAdditions.join('\n'));
}else{
strToBeWritten.push(`<<<<<<< HEAD\n${pendingRemoves.join('\n')}\n=======\n${pendingAdditions.join('\n')}\n>>>>>>> New-HEAD`);
}
}
if(normalLine)
strToBeWritten.push(normalLine.substr(1));
//console.log('strToBeWritten',strToBeWritten);
var writeFinishCallback=function(err){
pendingRemoves=[];
pendingAdditions=[];
if(addToPendingRemoves) pendingRemoves.push(addToPendingRemoves);
if(cb) cb();
else reader.emit('data');
};
if(!strToBeWritten.length){
if(needNewLineAtEnd)
writer.write('\n',writeFinishCallback);
else
writeFinishCallback(null);
}else{
let lastCharacter=needNewLineAtEnd?'\n':'';
writer.write((alreadyWritten?'\n':'')+strToBeWritten.join('\n')+(cb?lastCharacter:''),writeFinishCallback);
alreadyWritten=true;
}
}
var hunkIndex=0;
var hunkLineIndex=-1;
reader.on('data',function(){
++hunkLineIndex;
if(hunkLineIndex>=patch.hunks[hunkIndex].lines.length){
++hunkIndex; hunkLineIndex=0;
hunkPending=true;
}
if(hunkIndex>=patch.hunks.length)
return reader.emit('end');//may need resume
var line=patch.hunks[hunkIndex].lines[hunkLineIndex];
if(line[0]==' '){
writePendings(line);
lastSymbol=null;
}else if(line[0]=='-'){
if(!lastSymbol || lastSymbol=='-'){//was normal or same
pendingRemoves.push(line.substr(1));
lastSymbol='-';
return reader.emit('data');
}else{//was +
lastSymbol='-';
writePendings(false,line.substr(1));
}
}else if(line[0]=='+'){
pendingAdditions.push(line.substr(1));
lastSymbol='+';
return reader.emit('data');
}else{//skip \ No newline at end of file
//if(lastSymbol){
// lastSymbol=null;
// writePendings();
//}else
return reader.emit('data');
}
})
reader.on('end',function(){
writePendings(false,false,function(){
callback(null,conflict?-1:1);
});
});
reader.emit('data');
}
}
function Sync(oldFile, newFile, options){
var op=Object.assign({
outputFile:null,
force:false,
stream:false
},options||{});
var source='';
var patch;
if(typeof oldFile=="string"){
source=fs.readFileSync(oldFile,{encoding:'utf8'});
patch=diff.structuredPatch('A','B',source,fs.readFileSync(newFile,{encoding:'utf8'}),'','');
}else{//readstream
source=readStreamSync(oldFile);
var dest=readStreamSync(newFile);
patch=diff.structuredPatch('A','B',source,dest,'','');
}
if(patch.hunks.length==0)
return op.stream?null:0;
else{
//console.log('hunks',patch)
source=source.split('\n');
var alreadyWritten=false;
var lastSymbol=null;
var hunkPending=Boolean(patch.hunks[0].oldStart>1);
var pendingRemoves=[];
var pendingAdditions=[];
var conflict = false;
var writer;
var needNewLineAtEnd=false;
if(op.stream){
writer=new NodeStream.Readable();
}else{
writer=fs.createWriteStream(op.outputFile || newFile);
}
var writePendings=function(normalLine,addToPendingRemoves,last){
var strToBeWritten=[];
if(hunkPending){
hunkPending=false;
var preArr=[];
if(hunkIndex==0){
preArr=source.slice(0,patch.hunks[hunkIndex].oldStart-1);
}else if(hunkIndex<patch.hunks.length){
preArr=source.slice(patch.hunks[hunkIndex-1].oldStart+patch.hunks[hunkIndex-1].oldLines-1,patch.hunks[hunkIndex].oldStart-1);
}else{//lastHunk
let lastStart=patch.hunks[hunkIndex-1].oldStart+patch.hunks[hunkIndex-1].oldLines-1;
if(lastStart==source.length-1){
if(source[source.length-1]){
preArr=[source[source.length-1]];
}else{
needNewLineAtEnd=true;
}
}else if(lastStart<source.length-1){
preArr=source.slice(lastStart);
}
}
if(preArr.length)
strToBeWritten.push(preArr.join('\n'));
}
// console.log('writePendings(',normalLine,',',addToPendingRemoves);
// console.log('pendingRemoves',pendingRemoves);
// console.log('pendingAdditions',pendingAdditions);
if(!pendingRemoves.length && pendingAdditions.length){//only addition
strToBeWritten.push(pendingAdditions.join('\n'));
}else if(pendingRemoves.length && pendingAdditions.length){//conflict
conflict=true;
if(options.force){//prefer additions
strToBeWritten.push(pendingAdditions.join('\n'));
}else{
strToBeWritten.push(`<<<<<<< HEAD\n${pendingRemoves.join('\n')}\n=======\n${pendingAdditions.join('\n')}\n>>>>>>> New-HEAD`);
}
}
if(normalLine)
strToBeWritten.push(normalLine.substr(1));
var writeFinishCallback=function(err){
pendingRemoves=[];
pendingAdditions=[];
if(addToPendingRemoves) pendingRemoves.push(addToPendingRemoves);
};
if(!strToBeWritten.length)
writeFinishCallback(null);
else{
let lastCharacter=needNewLineAtEnd?'\n':'';
if(op.stream){
writer.push((alreadyWritten?'\n':'')+strToBeWritten.join('\n')+(last?lastCharacter:''));
writeFinishCallback();
}else
writer.write((alreadyWritten?'\n':'')+strToBeWritten.join('\n')+(last?lastCharacter:''),writeFinishCallback);
alreadyWritten=true;
}
}
for(var hunkIndex=0;hunkIndex<patch.hunks.length;hunkIndex++){
for(var hunkLineIndex=0;hunkLineIndex<patch.hunks[hunkIndex].lines.length;hunkLineIndex++){
var line=patch.hunks[hunkIndex].lines[hunkLineIndex];
if(line[0]==' '){
writePendings(line);
lastSymbol=null;
}else if(line[0]=='-'){
if(!lastSymbol || lastSymbol=='-'){//was normal or same
pendingRemoves.push(line.substr(1));
lastSymbol='-';
}else{//was +
lastSymbol='-';
writePendings(false,line.substr(1));
}
}else if(line[0]=='+'){
pendingAdditions.push(line.substr(1));
lastSymbol='+';
}else{//skip \ No newline at end of file
//if(lastSymbol){
// lastSymbol=null;
// writePendings();
//}
}
}
hunkPending=true;
}
writePendings(false,false,true);
if(op.stream){
writer.push(null);
return writer;
}else
return conflict?-1:1;
}
}
module.exports=Async;
module.exports.Sync=Sync;