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