diff options
Diffstat (limited to 'cli/actions.js')
-rw-r--r-- | cli/actions.js | 96 |
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 | |||
3 | exports.login = login; | ||
4 | exports.put = put; | ||
5 | exports.get = get; | ||
6 | exports.del = del; | ||
7 | |||
8 | var superagent = require('superagent'), | ||
9 | config = require('./config'), | ||
10 | async = require('async'), | ||
11 | fs = require('fs'), | ||
12 | path = require('path'); | ||
13 | |||
14 | require('colors'); | ||
15 | |||
16 | var API = '/api/files/'; | ||
17 | |||
18 | function checkConfig() { | ||
19 | if (!config.server()) { | ||
20 | console.log('You have run "login" first'); | ||
21 | process.exit(1); | ||
22 | } | ||
23 | } | ||
24 | |||
25 | function 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 | |||
44 | function login(server) { | ||
45 | console.log('Using server', server); | ||
46 | config.set('server', server); | ||
47 | } | ||
48 | |||
49 | function 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 | |||
70 | function 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 | |||
88 | function 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 | } | ||