-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathconflicter.js
208 lines (175 loc) · 4.94 KB
/
conflicter.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
var logger = process.logging || require('./log');
var fs = require('fs');
var path = require('path');
var events = require('events');
var diff = require('diff');
var prompt = require('../actions/prompt');
var log = logger('conflicter');
var async = require('async');
var isBinaryFile = require('isbinaryfile');
var chalk = require('chalk');
var conflicter = module.exports = Object.create(events.EventEmitter.prototype);
conflicter.conflicts = [];
conflicter.add = function add(conflict) {
if (typeof conflict === 'string') {
conflict = {
file: conflict,
content: fs.readFileSync(conflict, 'utf8')
};
}
if (!conflict.file) {
throw new Error('Missing conflict.file option');
}
if (conflict.content === undefined) {
throw new Error('Missing conflict.content option');
}
this.conflicts.push(conflict);
return this;
};
conflicter.reset = function reset() {
this.conflicts = [];
return this;
};
conflicter.pop = function pop() {
return this.conflicts.pop();
};
conflicter.shift = function shift() {
return this.conflicts.shift();
};
conflicter.resolve = function resolve(cb) {
var resolveConflicts = function (conflict) {
return function (next) {
if (!conflict) {
return next();
}
conflicter.collision(conflict.file, conflict.content, function (status) {
conflicter.emit('resolved:' + conflict.file, {
status: status,
callback: next
});
});
};
};
async.series(this.conflicts.map(resolveConflicts), function (err) {
if (err) {
cb();
return self.emit('error', err);
}
conflicter.reset();
cb();
}.bind(this));
};
conflicter._ask = function (filepath, content, cb) {
// for this particular use case, might use prompt module directly to avoid
// the additional "Are you sure?" prompt
var self = this;
var config = [{
type: 'expand',
message: 'Overwrite ' + filepath + '?',
choices: [{
key: 'y',
name: 'overwrite',
value: function (cb) {
log.force(filepath);
return cb('force');
}
}, {
key: 'n',
name: 'do not overwrite',
value: function (cb) {
log.skip(filepath);
return cb('skip');
}
}, {
key: 'a',
name: 'overwrite this and all others',
value: function (cb) {
log.force(filepath);
self.force = true;
return cb('force');
}
}, {
key: 'x',
name: 'abort',
value: function (cb) {
log.writeln('Aborting ...');
return process.exit(0);
}
}, {
key: 'd',
name: 'show the differences between the old and the new',
value: function (cb) {
console.log(conflicter.diff(fs.readFileSync(filepath, 'utf8'), content));
return self._ask(filepath, content, cb);
}
}],
name: 'overwrite'
}];
process.nextTick(function () {
this.emit('prompt', config);
this.emit('conflict', filepath);
}.bind(this));
prompt(config, function (result) {
result.overwrite(function (action) {
cb(action);
});
});
};
conflicter.collision = function collision(filepath, content, cb) {
var self = this;
if (!fs.existsSync(filepath)) {
log.create(filepath);
return cb('create');
}
if (!fs.statSync(path.resolve(filepath)).isDirectory()) {
var encoding = null;
if (!isBinaryFile(path.resolve(filepath))) {
encoding = 'utf8';
}
var actual = fs.readFileSync(path.resolve(filepath), encoding);
// In case of binary content, `actual` and `content` are `Buffer` objects,
// we just can't compare those 2 objects with standard `===`,
// so we convert each binary content to an hexadecimal string first, and then compare them with standard `===`
//
// For not binary content, we can directly compare the 2 strings this way
if ((!encoding && (actual.toString('hex') === content.toString('hex'))) ||
(actual === content)) {
log.identical(filepath);
return cb('identical');
}
}
if (self.force) {
log.force(filepath);
return cb('force');
}
log.conflict(filepath);
conflicter._ask(filepath, content, cb);
};
conflicter.colorDiffAdded = chalk.black.bgGreen;
conflicter.colorDiffRemoved = chalk.bgRed;
// below is borrowed code from visionmedia's excellent mocha (and its reporter)
conflicter.diff = function _diff(actual, expected) {
var msg = diff.diffLines(actual, expected).map(function (str) {
if (str.added) {
return conflicter.colorLines('Added', str.value);
}
if (str.removed) {
return conflicter.colorLines('Removed', str.value);
}
return str.value;
}).join('');
// legend
msg = '\n' +
conflicter.colorDiffRemoved('removed') +
' ' +
conflicter.colorDiffAdded('added') +
'\n\n' +
msg +
'\n';
return msg;
};
conflicter.colorLines = function colorLines(name, str) {
return str.split('\n').map(function (str) {
return conflicter['colorDiff' + name](str);
}).join('\n');
};