aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli/actions.js
diff options
context:
space:
mode:
Diffstat (limited to 'cli/actions.js')
-rw-r--r--cli/actions.js96
1 files changed, 96 insertions, 0 deletions
diff --git a/cli/actions.js b/cli/actions.js
new file mode 100644
index 0000000..592d809
--- /dev/null
+++ b/cli/actions.js
@@ -0,0 +1,96 @@
1'use strict';
2
3exports.login = login;
4exports.put = put;
5exports.get = get;
6exports.del = del;
7
8var superagent = require('superagent'),
9 config = require('./config'),
10 async = require('async'),
11 fs = require('fs'),
12 path = require('path');
13
14require('colors');
15
16var API = '/api/files/';
17
18function checkConfig() {
19 if (!config.server()) {
20 console.log('You have run "login" first');
21 process.exit(1);
22 }
23}
24
25function collectFiles(filesOrFolders) {
26 var tmp = [];
27
28 filesOrFolders.forEach(function (filePath) {
29 var stat = fs.statSync(filePath);
30
31 if (stat.isFile()) {
32 tmp.push(filePath);
33 } else if (stat.isDirectory()) {
34 var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); });
35 tmp = tmp.concat(collectFiles(files));
36 } else {
37 console.log('Skipping %s', filePath.cyan);
38 }
39 });
40
41 return tmp;
42}
43
44function login(server) {
45 console.log('Using server', server);
46 config.set('server', server);
47}
48
49function put(filePath, otherFilePaths) {
50 checkConfig();
51
52 var files = collectFiles([ filePath ].concat(otherFilePaths));
53
54 async.eachSeries(files, function (file, callback) {
55 var relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
56
57 console.log('Uploading file %s', relativeFilePath.cyan);
58
59 superagent.put(config.server() + API + relativeFilePath).attach('file', file).end(callback);
60 }, function (error) {
61 if (error) {
62 console.log('Failed to put file.', error);
63 process.exit(1);
64 }
65
66 console.log('Done');
67 });
68}
69
70function get(filePath) {
71 checkConfig();
72
73 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
74 superagent.get(config.server() + API + relativeFilePath).end(function (error, result) {
75 if (error) return console.log('Failed', result ? result.body : error);
76
77 if (result.body && result.body.entries) {
78 console.log('Files:');
79 result.body.entries.forEach(function (entry) {
80 console.log('\t %s', entry);
81 });
82 } else {
83 console.log(result.text);
84 }
85 });
86}
87
88function del(filePath) {
89 checkConfig();
90
91 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
92 superagent.del(config.server() + API + relativeFilePath).end(function (error, result) {
93 if (error) return console.log('Failed', result ? result.body : error);
94 console.log('Success', result.body);
95 });
96}