-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (38 loc) · 1.35 KB
/
Copy pathindex.js
File metadata and controls
45 lines (38 loc) · 1.35 KB
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
#!/usr/bin/env node
var diff = require("diff");
var fs = require("fs");
function applyPatch(patchFile) {
var patch = fs.readFileSync(patchFile, "utf8");
var sourceFileMatch = /--- ([^ \n\r\t]+).*/.exec(patch);
var sourceFile;
if (sourceFileMatch && sourceFileMatch[1]) {
sourceFile = sourceFileMatch[1];
} else {
throw Error("Unable to find source file in '" + patchFile + "'");
}
var destinationFileMatch = /\+\+\+ ([^ \n\r\t]+).*/.exec(patch);
var destinationFile;
if (destinationFileMatch && destinationFileMatch[1]) {
destinationFile = destinationFileMatch[1];
} else {
throw Error("Unable to find destination file in '" + patchFile + "'");
}
var original = fs.readFileSync(sourceFile, "utf8");
var patched = diff.applyPatch(original, patch);
if (patched === false) {
throw Error("Failed to apply patch '" + patchFile + "' to '" + sourceFile + "'");
} else if (sourceFile !== destinationFile) {
console.log("Applied '" + patchFile + "' to '" + sourceFile + "' and stored it as '" +
destinationFile + "'");
} else {
console.log("Applied '" + patchFile + "' to '" + sourceFile + "'");
}
fs.writeFileSync(destinationFile, patched);
}
module.exports.applyPatch = applyPatch;
if (require.main === module) {
var argv = process.argv;
for (var i = 2; i < argv.length; i++) {
applyPatch(argv[i]);
}
}