]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blobdiff - cli/actions.js
Add missing callback in upload
[perso/Immae/Projets/Nodejs/Surfer.git] / cli / actions.js
index 119b2babd3cb5ba703ee97f2b72e3efe743b880e..ab982a7a14418d6ea977ae5f84d1ddba6b6ec889 100644 (file)
@@ -31,17 +31,20 @@ function checkConfig() {
     console.error('Using server %s', config.server().yellow);
 }
 
-function collectFiles(filesOrFolders) {
+function collectFiles(filesOrFolders, options) {
     var tmp = [];
 
     filesOrFolders.forEach(function (filePath) {
+        var baseName = path.basename(filePath);
+        if (!options.all && baseName[0] === '.' && baseName.length > 1) return;
+
         var stat = fs.statSync(filePath);
 
         if (stat.isFile()) {
             tmp.push(filePath);
         } else if (stat.isDirectory()) {
             var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); });
-            tmp = tmp.concat(collectFiles(files));
+            tmp = tmp.concat(collectFiles(files, options));
         } else {
             console.log('Skipping %s', filePath.cyan);
         }
@@ -66,7 +69,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);
         }
@@ -90,14 +93,30 @@ function login(uri) {
 function put(filePath, otherFilePaths, options) {
     checkConfig();
 
-    var files = collectFiles([ filePath ].concat(otherFilePaths));
+    var files = collectFiles([ filePath ].concat(otherFilePaths), options);
 
     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()) === 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);
+
+        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));
 
-        console.log('Uploading file %s -> %s', relativeFilePath.cyan, ((options.destination ? options.destination : '') + '/' + relativeFilePath).cyan);
+            console.log('Uploaded to ' + config.server() + destinationPath);
 
-        superagent.put(config.server() + API + relativeFilePath).query(gQuery).attach('file', file).end(callback);
+            callback(null);
+        });
     }, function (error) {
         if (error) {
             console.log('Failed to put file.', error);
@@ -126,7 +145,7 @@ function get(filePath) {
                 console.log('\t %s', entry);
             });
         } else {
-            console.log(body);
+            process.stdout.write(body);
         }
     });
     // var req = superagent.get(config.server() + API + filePath);