X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git;a=blobdiff_plain;f=cli%2Factions.js;h=3fd240302a3287cb214176021740cc0520f96f3d;hp=0c621f674784da462ab00247f60a9ebc8ea28ec7;hb=511ce661de41d42ab427ab1e2f43cc9f2c84b6c7;hpb=38932f15c25f28296b53ee2d7ce738a20c9f7b15 diff --git a/cli/actions.js b/cli/actions.js index 0c621f6..3fd2403 100644 --- a/cli/actions.js +++ b/cli/actions.js @@ -128,32 +128,27 @@ function logout() { }); } -function put(filePath, otherFilePaths, options) { - checkConfig(options); - - var destination = ''; - - // take the last argument as destination - if (otherFilePaths.length > 0) { - destination = otherFilePaths.pop(); - if (otherFilePaths.length > 0 && destination[destination.length-1] !== '/') destination += '/'; +function putOne(filePath, destination, options, callback) { + const absoluteFilePath = path.resolve(filePath); + const stat = safe.fs.statSync(absoluteFilePath); + if (!stat) return callback(`Could not stat ${filePath}: ${safe.error.message}`); + + let files, base; + + if (stat.isFile()) { + base = destination + path.basename(filePath); + files = [ absoluteFilePath ]; + } else if (stat.isDirectory()) { + base = destination + (filePath.endsWith('.') ? '' : path.basename(filePath) + '/'); + files = collectFiles([ absoluteFilePath ], options); + } else { + return callback(); // ignore } - var files = collectFiles([ filePath ].concat(otherFilePaths), options); - async.eachSeries(files, function (file, callback) { - 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 = (destination ? '/' + destination : '') + '/' + relativeFilePath; - console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan); + let relativeFilePath = file.slice(absoluteFilePath.length + 1); // will be '' when filePath is a file + let destinationPath = base + relativeFilePath; + console.log('Uploading file %s -> %s', file.cyan, destinationPath.cyan); superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed')); @@ -164,7 +159,25 @@ function put(filePath, otherFilePaths, options) { callback(null); }); - }, function (error) { + }, callback); +} + +function put(filePaths, options) { + checkConfig(options); + + if (filePaths.length < 2) { + console.log('target directory is required.'.red); + process.exit(1); + } + + let destination = filePaths.pop(); + if (!path.isAbsolute(destination)) { + console.log('target directory must be absolute'.red); + process.exit(1); + } + if (!destination.endsWith('/')) destination += '/'; + + async.eachSeries(filePaths, (filePath, iteratorDone) => putOne(filePath, destination, options, iteratorDone), function (error) { if (error) { console.log('Failed to put file.', error.message.red); process.exit(1);