]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blobdiff - cli/actions.js
Fix uri parsing
[perso/Immae/Projets/Nodejs/Surfer.git] / cli / actions.js
index e5b0c6ddc3a8f0a00324679967c2cade7bfc755a..6c4631cf341059b38bde836bfd4482dd45e00dcd 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);
         }
@@ -52,14 +55,14 @@ function collectFiles(filesOrFolders) {
 
 function login(uri) {
     var tmp = url.parse(uri);
-    if (!tmp.host) tmp = url.parse('https://' + uri);
+    if (!tmp.slashes) tmp = url.parse('https://' + uri);
 
     var server = tmp.protocol + '//' + tmp.host;
 
-    console.log('Using server', server.bold);
+    console.log('Using server', server.cyan);
 
-    var username = readlineSync.question('Username: ', { hideEchoBack: false });
-    var password = readlineSync.question('Password: ', { hideEchoBack: true });
+    var username = readlineSync.question('Username: ');
+    var password = readlineSync.question('Password: ', { noEchoBack: true });
 
     superagent.get(server + API + '/').query({ username: username, password: password }).end(function (error, result) {
         if (error && error.code === 'ENOTFOUND') {
@@ -83,25 +86,36 @@ function login(uri) {
 
         gQuery = { username: username, password: password };
 
-        console.log('Ok'.green);
+        console.log('Login successful'.green);
     });
 }
 
 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 + relativeFilePath).query(gQuery).attach('file', file).end(function (error, result) {
+        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('Uploaded to ' + config.server() + destinationPath);
+
+            callback(null);
         });
     }, function (error) {
         if (error) {
@@ -131,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);