refactor(put) patchFile

This commit is contained in:
coderaiser 2014-03-18 08:35:23 -04:00
parent e8c9754f19
commit bf283549dd

View file

@ -56,7 +56,7 @@
if (error)
func(error);
else
patchFile(patch);
patchFile(name, patch, func);
});
else
error = 'File is to big. ' +
@ -66,39 +66,40 @@
if (error)
func(error);
});
function patchFile(patch) {
fs.readFile(name, 'utf8', read);
function read(error, data) {
var diffResult;
if (error)
func(error);
else {
error = Util.tryCatchLog(function() {
diffResult = diff.applyPatch(data, patch);
});
if (diffResult && !error)
fs.writeFile(name, diffResult, write);
else {
msg = CloudFunc.formatMsg('patch', baseName, 'fail');
func(null, msg);
}
}
}
function write(name, error) {
var msg;
if (!error)
msg = CloudFunc.formatMsg('patch', baseName);
func(error, msg);
}
}
break;
}
}
function patchFile(name, patch, func) {
fs.readFile(name, 'utf8', read);
function read(error, data) {
var diffResult;
if (error)
func(error);
else {
error = Util.tryCatchLog(function() {
diffResult = diff.applyPatch(data, patch);
});
if (diffResult && !error)
fs.writeFile(name, diffResult, write);
else {
msg = CloudFunc.formatMsg('patch', baseName, 'fail');
func(null, msg);
}
}
}
function write(name, error) {
var msg,
baseName = path.basename(name);
if (!error)
msg = CloudFunc.formatMsg('patch', baseName);
func(error, msg);
}
}
})();