X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=cli%2Factions.js;h=25643da705a0070153af8ec6bff6f3f44f88a432;hb=d53a1669766a1a77bda34ee52109bee9c8735bda;hp=77672817d88f227e6f7c46a9887cf21c293b6eb2;hpb=4f89763120ee40cf1e9ac27b55fa8cc4e7ed51e1;p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git diff --git a/cli/actions.js b/cli/actions.js index 7767281..25643da 100644 --- a/cli/actions.js +++ b/cli/actions.js @@ -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); } @@ -90,14 +93,27 @@ 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().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); @@ -126,7 +142,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);