mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
refactor(put) patchFile
This commit is contained in:
parent
e8c9754f19
commit
bf283549dd
1 changed files with 34 additions and 33 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue