]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blobdiff - cli/actions.js
fix path resolution
[perso/Immae/Projets/Nodejs/Surfer.git] / cli / actions.js
index 119b2babd3cb5ba703ee97f2b72e3efe743b880e..735b631600092ef4054895bd1577d9ffca22cb97 100644 (file)
@@ -66,7 +66,7 @@ function login(uri) {
             console.log('No such server %s'.red, server.bold);
             process.exit(1);
         }
-        if (error.code) {
+        if (error && error.code) {
             console.log('Failed to connect to server %s'.red, server.bold, error.code);
             process.exit(1);
         }
@@ -93,11 +93,24 @@ function put(filePath, otherFilePaths, options) {
     var files = collectFiles([ filePath ].concat(otherFilePaths));
 
     async.eachSeries(files, function (file, callback) {
-        var relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
+        var relativeFilePath;
+        if (path.isAbsolute(file)) {
+            relativeFilePath = path.basename(file);
+        } else if (path.resolve(file).indexOf(process.cwd().length) === 0) { // relative to current dir
+            relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
+        } else { // relative but somewhere else
+            relativeFilePath = path.basename(file);
+        }
+
+        var destinationPath = (options.destination ? '/' + options.destination : '') + '/' + relativeFilePath;
+        console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan);
 
-        console.log('Uploading file %s -> %s', relativeFilePath.cyan, ((options.destination ? options.destination : '') + '/' + relativeFilePath).cyan);
+        superagent.put(config.server() + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) {
+            if (error) return callback(error);
+            if (result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode));
 
-        superagent.put(config.server() + API + relativeFilePath).query(gQuery).attach('file', file).end(callback);
+            console.log('Uploaded to ' + config.server() + destinationPath);
+        });
     }, function (error) {
         if (error) {
             console.log('Failed to put file.', error);