]>
Commit | Line | Data |
---|---|---|
8c3ae071 JZ |
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'), | |
a90a633f JZ |
9 | config = require('./config.js'), |
10 | readlineSync = require('readline-sync'), | |
8c3ae071 JZ |
11 | async = require('async'), |
12 | fs = require('fs'), | |
13 | path = require('path'); | |
14 | ||
15 | require('colors'); | |
16 | ||
17 | var API = '/api/files/'; | |
18 | ||
a90a633f JZ |
19 | var gQuery = {}; |
20 | ||
8c3ae071 | 21 | function checkConfig() { |
a90a633f | 22 | if (!config.server() || !config.username() || !config.password()) { |
8c3ae071 JZ |
23 | console.log('You have run "login" first'); |
24 | process.exit(1); | |
25 | } | |
c10e69bf | 26 | |
a90a633f JZ |
27 | gQuery = { username: config.username(), password: config.password() }; |
28 | ||
c10e69bf | 29 | console.log('Using server %s', config.server().yellow); |
8c3ae071 JZ |
30 | } |
31 | ||
32 | function collectFiles(filesOrFolders) { | |
33 | var tmp = []; | |
34 | ||
35 | filesOrFolders.forEach(function (filePath) { | |
36 | var stat = fs.statSync(filePath); | |
37 | ||
38 | if (stat.isFile()) { | |
39 | tmp.push(filePath); | |
40 | } else if (stat.isDirectory()) { | |
41 | var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); }); | |
42 | tmp = tmp.concat(collectFiles(files)); | |
43 | } else { | |
44 | console.log('Skipping %s', filePath.cyan); | |
45 | } | |
46 | }); | |
47 | ||
48 | return tmp; | |
49 | } | |
50 | ||
51 | function login(server) { | |
591ad40c JZ |
52 | if (server[server.length-1] === '/') server = server.slice(0, -1); |
53 | ||
8c3ae071 | 54 | console.log('Using server', server); |
a90a633f JZ |
55 | |
56 | var username = readlineSync.question('Username: ', { hideEchoBack: false }); | |
57 | var password = readlineSync.question('Password: ', { hideEchoBack: true }); | |
58 | ||
59 | superagent.get(server + API + '/').query({ username: username, password: password }).end(function (error, result) { | |
a90a633f JZ |
60 | if (result.status === 401) { |
61 | console.log('Login failed.'); | |
62 | process.exit(1); | |
63 | } | |
64 | ||
65 | config.set('server', server); | |
66 | config.set('username', username); | |
67 | ||
68 | // TODO this is clearly bad and needs fixing | |
69 | config.set('password', password); | |
70 | ||
71 | gQuery = { username: username, password: password }; | |
c7576037 JZ |
72 | |
73 | console.log('Done'); | |
a90a633f | 74 | }); |
8c3ae071 JZ |
75 | } |
76 | ||
eb83e4da | 77 | function put(filePath, otherFilePaths, options) { |
8c3ae071 JZ |
78 | checkConfig(); |
79 | ||
80 | var files = collectFiles([ filePath ].concat(otherFilePaths)); | |
81 | ||
82 | async.eachSeries(files, function (file, callback) { | |
83 | var relativeFilePath = path.resolve(file).slice(process.cwd().length + 1); | |
84 | ||
eb83e4da | 85 | console.log('Uploading file %s -> %s', relativeFilePath.cyan, ((options.destination ? options.destination : '') + '/' + relativeFilePath).cyan); |
8c3ae071 | 86 | |
a90a633f | 87 | superagent.put(config.server() + API + relativeFilePath).query(gQuery).attach('file', file).end(callback); |
8c3ae071 JZ |
88 | }, function (error) { |
89 | if (error) { | |
90 | console.log('Failed to put file.', error); | |
91 | process.exit(1); | |
92 | } | |
93 | ||
94 | console.log('Done'); | |
95 | }); | |
96 | } | |
97 | ||
98 | function get(filePath) { | |
99 | checkConfig(); | |
100 | ||
a90a633f JZ |
101 | superagent.get(config.server() + API + filePath).query(gQuery).end(function (error, result) { |
102 | if (error && error.status === 401) return console.log('Login failed'); | |
103 | if (error && error.status === 404) return console.log('No such file or directory'); | |
8c3ae071 JZ |
104 | if (error) return console.log('Failed', result ? result.body : error); |
105 | ||
106 | if (result.body && result.body.entries) { | |
107 | console.log('Files:'); | |
108 | result.body.entries.forEach(function (entry) { | |
109 | console.log('\t %s', entry); | |
110 | }); | |
111 | } else { | |
112 | console.log(result.text); | |
113 | } | |
114 | }); | |
115 | } | |
116 | ||
117 | function del(filePath) { | |
118 | checkConfig(); | |
119 | ||
120 | var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); | |
a90a633f JZ |
121 | superagent.del(config.server() + API + relativeFilePath).query(gQuery).end(function (error, result) { |
122 | if (error && error.status === 401) return console.log('Login failed'); | |
123 | if (error && error.status === 404) return console.log('No such file or directory'); | |
8c3ae071 JZ |
124 | if (error) return console.log('Failed', result ? result.body : error); |
125 | console.log('Success', result.body); | |
126 | }); | |
127 | } |