aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli/actions.js
diff options
context:
space:
mode:
Diffstat (limited to 'cli/actions.js')
-rw-r--r--cli/actions.js61
1 files changed, 37 insertions, 24 deletions
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() {
128 }); 128 });
129} 129}
130 130
131function put(filePath, otherFilePaths, options) { 131function putOne(filePath, destination, options, callback) {
132 checkConfig(options); 132 const absoluteFilePath = path.resolve(filePath);
133 133 const stat = safe.fs.statSync(absoluteFilePath);
134 var destination = ''; 134 if (!stat) return callback(`Could not stat ${filePath}: ${safe.error.message}`);
135 135
136 // take the last argument as destination 136 let files, base;
137 if (otherFilePaths.length > 0) { 137
138 destination = otherFilePaths.pop(); 138 if (stat.isFile()) {
139 if (otherFilePaths.length > 0 && destination[destination.length-1] !== '/') destination += '/'; 139 base = destination + path.basename(filePath);
140 files = [ absoluteFilePath ];
141 } else if (stat.isDirectory()) {
142 base = destination + (filePath.endsWith('.') ? '' : path.basename(filePath) + '/');
143 files = collectFiles([ absoluteFilePath ], options);
144 } else {
145 return callback(); // ignore
140 } 146 }
141 147
142 var files = collectFiles([ filePath ].concat(otherFilePaths), options);
143
144 async.eachSeries(files, function (file, callback) { 148 async.eachSeries(files, function (file, callback) {
145 var relativeFilePath; 149 let relativeFilePath = file.slice(absoluteFilePath.length + 1); // will be '' when filePath is a file
146 150 let destinationPath = base + relativeFilePath;
147 if (path.isAbsolute(file)) { 151 console.log('Uploading file %s -> %s', file.cyan, destinationPath.cyan);
148 relativeFilePath = path.basename(file);
149 } else if (path.resolve(file).indexOf(process.cwd()) === 0) { // relative to current dir
150 relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
151 } else { // relative but somewhere else
152 relativeFilePath = path.basename(file);
153 }
154
155 var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath;
156 console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan);
157 152
158 superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { 153 superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) {
159 if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed')); 154 if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed'));
@@ -164,7 +159,25 @@ function put(filePath, otherFilePaths, options) {
164 159
165 callback(null); 160 callback(null);
166 }); 161 });
167 }, function (error) { 162 }, callback);
163}
164
165function put(filePaths, options) {
166 checkConfig(options);
167
168 if (filePaths.length < 2) {
169 console.log('target directory is required.'.red);
170 process.exit(1);
171 }
172
173 let destination = filePaths.pop();
174 if (!path.isAbsolute(destination)) {
175 console.log('target directory must be absolute'.red);
176 process.exit(1);
177 }
178 if (!destination.endsWith('/')) destination += '/';
179
180 async.eachSeries(filePaths, (filePath, iteratorDone) => putOne(filePath, destination, options, iteratorDone), function (error) {
168 if (error) { 181 if (error) {
169 console.log('Failed to put file.', error.message.red); 182 console.log('Failed to put file.', error.message.red);
170 process.exit(1); 183 process.exit(1);