Skip to content

Commit

Permalink
Bug fixes and sync indicators!
Browse files Browse the repository at this point in the history
Fixes and Features
 * Status indicators on commands. (Yellow circle for ongoing, Green
disc for completed, red square for error)
 * quote wraps to fix bug with files having space characters in their
names
 * Check connection of --remote_base at startup, saves a lot of hassle
and initial confusion.
  • Loading branch information
bensmoif committed Feb 16, 2015
1 parent 19ffa70 commit ecb338d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 37 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ Show help:
Options:
--remote_base URL for the remote endpoint [required]
--local_base Path to the local directory [required]
--curl Default curl command [default: "curl --insecure --verbose"]
--curl Default curl command [default: "curl --insecure -s -S"]
--ignored Comma separated list of ignored paths [default: ".hg,.git,.svn,.DS_Store"]
--help Displays this help
--verbose Makes webdav-sync more talkative with complete curl command and timestamp
--help, -h Displays this help


Syncing /var/src/code with https://user:[email protected]/webdav/cartridge/
Expand Down Expand Up @@ -59,6 +60,8 @@ Simply execute `cake dev` to start continuous compilation. You may also want to

- 0.1.1: First release
- 0.2.4: Upgrading to node 0.6.x
- 0.3.0: Cleanup and --verbose flag added
- 0.3.1: File transfer fixes and transfer status icons

### TODO

Expand Down
101 changes: 71 additions & 30 deletions lib/webdav_sync.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
/*
webdav_sync.js
* This is where the business happens.
*
Fixes and Features
* Status indicators on commands. (Yellow circle for ongoing, Green disc for completed, red square for error)
* quote wraps to fix bug with files having space characters in their names
* Check connection of --remote_base at startup, saves a lot of hassle and initial confusion.
*
* TODO:
* -report on success/fail of curl command (errors are incomplete), need a "Could not resolve" or something
* -Check health of --remote_base at startup, can we connect successfully
* -interactive 'Clean All' command to force wipe & re-push all files.
*/

var reqStack = new Object(); //ongoing queue of active requests, pop on complete/error
// Something like: reqStack[request, lineNum] ?
var currLine = 0; //rolling iterator of how many lines have been printed

var colors, exec, fs, util, watch;
var __indexOf = [].indexOf || function (item) {
for (var i = 0, l = this.length; i < l; i++) {
if (i in this && this[i] === item)
return i;
}
return -1;
var i, l;
for (i = 0, l = this.length; i < l; i++) {
if (i in this && this[i] === item) {
return i;
}
}
return -1;
};

watch = require("watch");
Expand All @@ -26,7 +37,7 @@ module.exports = function(options) {
if (options == null) {
options = {};
}
processed = [];
processed = new Object(); //History of processed execs in the format of "d"+theCommand+mtime of file

options.monitor || (options.monitor = {
ignoreDotFiles: true
Expand All @@ -38,30 +49,39 @@ module.exports = function(options) {
throw new Error("local_base can't be ommited");
}
run = function(command, message) {
//show this first
util.print(message+" ");

//PRINT: disable line break, then the CIRCLE then message

util.puts('\033[?7l'+"○ ".yellow + message);
reqStack[command]=currLine;//push this command with currLine in
currLine++;//then go on...(?)

return _runCommand(command, function(err, success) {

moveUpRows = currLine - reqStack[command];
var goBackStr = new Array(moveUpRows+1).join("\n");

if (err) {
//util.print("■".red);
util.print('\n');
return util.puts(err.red);
//RED SQUARE
util.print('\033[' + moveUpRows + 'A\r' + "■".red + goBackStr);
currLine++;
return util.puts(err.red); //print the full curl error (does this throw off the currLine count?)
}else{
//GREEN DOT
//util.print("●".green);
util.print('\n');
util.print('\033[' + moveUpRows + 'A\r' + "●".green + goBackStr);
}
});
};
_runCommand = function(command, callback) {

if(options.verbose){
currLine++;
util.puts((new Date()).toString().underline);
currLine++;
util.puts(command.yellow);
}

return exec(command, function(error, stdout, stderr) {
return exec(command, function(error, stdout, stderr){
//BEN: need to get more out of curl about failed attempts to connect!
if (!stderr) {
return callback(null, true);
Expand All @@ -74,21 +94,25 @@ module.exports = function(options) {
};
created = function(path, stats) {
var command, destination, message, rel_path, _ref;

//check if we've tried this filechange already...
if (!stats || (_ref = "w" + path + stats.mtime, __indexOf.call(processed, _ref) >= 0)) {
return;
}
processed.push("w" + path + stats.mtime);

processed["w" + path + stats.mtime] = currLine;
rel_path = path.replace(options.local_base, "");
destination = options.remote_base + rel_path;

//ignore it?
if (ignoreFile(rel_path)) {
return;
}
if (stats.isFile()) {
command = "" + options.curl + " -T " + path + " " + destination;
command = options.curl + ' -T "' + path + '" "' + destination + '" ';
} else {
if (stats.isDirectory()) {
command = "" + options.curl + " -X MKCOL " + destination;
command = options.curl + ' -X MKCOL "' + destination + '" ';
}
}
if (command != null) {
Expand All @@ -99,15 +123,14 @@ module.exports = function(options) {
changed = function(path, stats) {
var command, destination, message, rel_path, _ref;

//check to see if we've tried to send this yet...
if (!stats || (_ref = "w" + path + stats.mtime, __indexOf.call(processed, _ref) >= 0)) {
util.puts("huh");
//util.puts("huh");
return;
}
processed.push("m" + path + stats.mtime);
if (stats.atime > stats.mtime) {
//util.puts("atime > mtime");
//return; //Ben: not sure why we would want this...
}

processed["m" + path + stats.mtime] = currLine; //remember that we modified path at mtime...

rel_path = path.replace(options.local_base, "");
destination = options.remote_base + rel_path;
if (ignoreFile(rel_path)) {
Expand All @@ -116,7 +139,7 @@ module.exports = function(options) {

//BEN: does this NOT work on folder modifications?
if (stats.isFile()) {
command = "" + options.curl + " -T " + path + " " + destination;
command = options.curl + ' -T "' + path + '" ' + destination;
}
if (command != null) {

Expand All @@ -129,13 +152,15 @@ module.exports = function(options) {
if (!stats || (_ref = "d" + path + stats.mtime, __indexOf.call(processed, _ref) >= 0)) {
return;
}
processed.push("d" + path + stats.mtime);

processed["d" + path + stats.mtime]=currLine;

rel_path = path.replace(options.local_base, "");
destination = options.remote_base + rel_path;
if (ignoreFile(rel_path)) {
return;
}
command = "" + options.curl + " -X DELETE " + destination;
command = options.curl + ' -X DELETE "' + destination + '" ';
if (command != null) {
message = "[removed] ".bold + rel_path.red;
return run(command, message);
Expand All @@ -149,8 +174,24 @@ module.exports = function(options) {

return {
start: function() {
//disable console line wrap (necessary for status icons)
util.puts('\033[0m\r');//reset any funny text modes
util.puts('\033[?7h\r');//enable line-break
currLine++;

util.puts("STARTING".bold.underline + " \nwebdav-sync from " + options.local_base.green + " \nto " + options.remote_base.yellow);

util.puts("STARTING".bold.underline + " webdav-sync from " + options.local_base.green + " to " + options.remote_base.yellow);
//first check to see if we can even get into options.remote_base
var command;
command = options.curl + " " + options.remote_base;
if (command != null) {
//Ben NEW FEATURE: testing the connection when we start!
//message = "[connection test] ".bold + options.remote_base.green;
message="[CONNECTION TEST]".bold;
run(command, message);
}

//rig the watch events to local_base
return watch.createMonitor(options.local_base, options.monitor, function(monitor) {
monitor.on("created", created);
monitor.on("changed", changed);
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webdav-sync",
"version": "0.3.0",
"version": "0.3.1",

This comment has been minimized.

Copy link
@bermi

bermi Feb 17, 2015

Owner

Since the stdout has changed, wouldn't it make sense to make this a minor release instead of a patch release? If someone complains (I don't think there's anyone using the stdout of this project programatically) we might have to pull off the version from npm.

This comment has been minimized.

Copy link
@bensmoif

bensmoif Feb 17, 2015

Collaborator

Yes! I never really think of minor releases until substantial features changes, but you're right. I've been talking to the people who maintain dwre-sync (the only dependent I've seen on npmjs.com), they seem pleased with the status indicators.

I'll bump the package.json and ReadMe to 0.4.0 then.

"description": "Basic local sync to WebDAV servers",
"author": {
"name": "Bermi Ferrer",
Expand Down Expand Up @@ -43,9 +43,6 @@
"optimist": "0.3.x",
"pkginfo": "0.2.x"
},
"devDependencies": {
"coffee-script": "1.2.x"
},
"engines": {
"node": ">=0.6"
},
Expand All @@ -55,7 +52,7 @@
"type": "git",
"url": "git://github.com/bermi/webdav-sync.git"
},
"_id": "webdav-sync@0.2.4",
"_id": "webdav-sync@0.3.1",
"dist": {
"shasum": "1974b0da4c82c6481098a78b6c7541dbbb9f0f4d"
},
Expand Down

0 comments on commit ecb338d

Please sign in to comment.