-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
274 lines (213 loc) · 6.22 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
'use strict';
const fs = require('fs');
const path = require('path');
const stream = require('stream');
const crypto = require('crypto');
const mkdirp = require('mkdirp');
const del = require('del');
const vfs = require('vinyl-fs');
const File = require('vinyl');
const through = require('through2');
let posixPath = function(path) {
return path;
};
let localizedPath = posixPath;
if (path.sep === '\\') {
posixPath = function(path) {
return path.replace(/\\/g, '/');
};
localizedPath = function(path) {
return path.replace(/\//g, '\\');
};
}
function getOptions(options, defaultOptions) {
return Object.assign(defaultOptions, options || {});
}
function getHash(content) {
return new Promise(function(resolve, reject) {
let tempStream;
if (content instanceof stream.Stream)
tempStream = content;
else if (content instanceof Buffer) {
tempStream = new stream.PassThrough();
tempStream.end(content);
}
else {
reject();
return;
}
const hash = crypto.createHash('sha1')
.setEncoding('hex')
.on('finish', function() {
resolve(this.read());
})
.on('error', reject);
tempStream.pipe(hash);
});
}
function patch(comparedFolder, options) {
const existingFiles = {};
const counter = createCounter();
options = getOptions(options, {
highWaterMark: 16384
});
function transform(file, encoding, callback) {
if (file.stat.isDirectory())
return callback();
const comparedFilePath = path.join(comparedFolder, file.relative);
const that = this;
fs.stat(comparedFilePath, function(err, comparedStat) {
if (!comparedStat)
return callback(null, that.addPatchedFile(file, patch.NEW));
existingFiles[comparedFilePath] = true;
if (comparedStat.size !== file.stat.size)
return callback(null, that.addPatchedFile(file, patch.CHANGED));
Promise.all([
getHash(fs.createReadStream(comparedFilePath)),
getHash(file.contents)
]).then(function(hashes) {
callback(null, hashes[0] === hashes[1] ? null : that.addPatchedFile(file, patch.CHANGED));
}).catch(callback);
});
}
function flush(callback) {
this.findDeletedFiles(comparedFolder);
console.log('Patched files:', counter.toString());
callback();
}
const plugin = through({ objectMode: true, highWaterMark: options.highWaterMark }, transform, flush);
plugin.addPatchedFile = function(file, status) {
patch.setFileStatus(file, status);
++counter[status];
return file;
};
plugin.findDeletedFiles = function(folder) {
const files = fs.readdirSync(folder);
for (let file of files) {
const filePath = path.join(folder, file);
if (fs.statSync(filePath).isDirectory())
this.findDeletedFiles(filePath);
else if (!(filePath in existingFiles))
this.push(this.addPatchedFile(
new File({
base: comparedFolder,
path: filePath
}),
patch.DELETED
));
}
};
return plugin;
}
patch.write = function(patchFolder, options) {
del.sync(patchFolder);
mkdirp.sync(patchFolder);
options = getOptions(options, {
passthrough: false
});
const patchFiles = {};
function transform(file, encoding, callback) {
const status = patch.getFileStatus(file);
if (status !== patch.DELETED) {
const filePath = path.join(patchFolder, file.relative);
mkdirp.sync(path.dirname(filePath));
file.pipe(fs.createWriteStream(filePath));
}
patchFiles[posixPath(file.relative)] = status;
callback(null, options.passthrough ? file : null);
}
function flush(callback) {
const filePath = path.join(patchFolder, patch.FILE_MAME);
fs.writeFileSync(filePath, JSON.stringify(patchFiles, null, '\t'));
console.log('Patch successfully created');
callback();
}
return through.obj(transform, flush).on('finish', function () {
this.emit('end');
});
};
patch.read = function(patchFolder, destFolder, options) {
const filePath = path.join(patchFolder, patch.FILE_MAME);
const patchFiles = {};
const vinylFiles = [];
const counter = createCounter();
destFolder = getDestinationFolder(destFolder);
JSON.parse(fs.readFileSync(filePath), function(file, status) {
patchFiles[localizedPath(file)] = status;
return status;
});
options = getOptions(options, {
base: patchFolder
});
for (const file in patchFiles) {
const status = patchFiles[file];
if (status === patch.NEW || status === patch.CHANGED)
vinylFiles.push(path.join(patchFolder, file));
else if (status === patch.DELETED) {
const delPath = path.join(destFolder, file);
try {
fs.statSync(delPath);
vinylFiles.push(delPath);
}
catch (e) {
console.log('File ' + delPath + ' does not exist');
}
}
++counter[status];
}
console.log('Patched files:', counter.toString());
return vfs.src(vinylFiles, options).pipe(through.obj(function(file, encoding, callback) {
patch.setFileStatus(file, patchFiles[file.relative] || patch.DELETED);
callback(null, file);
}));
};
patch.apply = function(destFolder, options) {
destFolder = getDestinationFolder(destFolder);
options = getOptions(options, {
passthrough: false
});
function transform(file, encoding, callback) {
const status = patch.getFileStatus(file);
if (status === patch.DELETED)
fs.unlinkSync(file.path);
else if (status === patch.NEW || status === patch.CHANGED) {
const targetPath = path.join(destFolder, file.relative);
if (status === patch.NEW)
mkdirp.sync(path.dirname(targetPath));
file.pipe(fs.createWriteStream(targetPath));
}
callback(null, options.passthrough ? file : null);
}
function flush(callback) {
console.log('Patch successfully applied');
callback();
}
return through.obj(transform, flush).on('finish', function () {
this.emit('end');
});
};
patch.NEW = 'new';
patch.CHANGED = 'changed';
patch.DELETED = 'deleted';
patch.FILE_MAME = 'patch.json';
patch.getFileStatus = function(file) {
return file.patchStatus;
};
patch.setFileStatus = function(file, status) {
file.patchStatus = status;
};
function createCounter() {
const counter = {
toString: function() {
return counter[patch.CHANGED] + ' changed, ' + counter[patch.DELETED] + ' deleted, ' + counter[patch.NEW] + ' new';
}
};
counter[patch.NEW] = 0;
counter[patch.CHANGED] = 0;
counter[patch.DELETED] = 0;
return counter;
}
function getDestinationFolder(destFolder) {
return destFolder || '.';
}
module.exports = patch;