]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - cli/actions.js
8 var superagent
= require('superagent'),
9 config
= require('./config'),
10 async
= require('async'),
12 path
= require('path');
16 var API
= '/api/files/';
18 function checkConfig() {
19 if (!config
.server()) {
20 console
.log('You have run "login" first');
24 console
.log('Using server %s', config
.server().yellow
);
27 function collectFiles(filesOrFolders
) {
30 filesOrFolders
.forEach(function (filePath
) {
31 var stat
= fs
.statSync(filePath
);
35 } else if (stat
.isDirectory()) {
36 var files
= fs
.readdirSync(filePath
).map(function (file
) { return path
.join(filePath
, file
); });
37 tmp
= tmp
.concat(collectFiles(files
));
39 console
.log('Skipping %s', filePath
.cyan
);
46 function login(server
) {
47 console
.log('Using server', server
);
48 config
.set('server', server
);
51 function put(filePath
, otherFilePaths
, options
) {
54 var files
= collectFiles([ filePath
].concat(otherFilePaths
));
56 async
.eachSeries(files
, function (file
, callback
) {
57 var relativeFilePath
= path
.resolve(file
).slice(process
.cwd().length
+ 1);
59 console
.log('Uploading file %s -> %s', relativeFilePath
.cyan
, ((options
.destination
? options
.destination : '') + '/' + relativeFilePath
).cyan
);
61 superagent
.put(config
.server() + API
+ relativeFilePath
).attach('file', file
).end(callback
);
64 console
.log('Failed to put file.', error
);
72 function get(filePath
) {
75 var relativeFilePath
= path
.resolve(filePath
).slice(process
.cwd().length
+ 1);
76 superagent
.get(config
.server() + API
+ relativeFilePath
).end(function (error
, result
) {
77 if (error
) return console
.log('Failed', result
? result
.body : error
);
79 if (result
.body
&& result
.body
.entries
) {
80 console
.log('Files:');
81 result
.body
.entries
.forEach(function (entry
) {
82 console
.log('\t %s', entry
);
85 console
.log(result
.text
);
90 function del(filePath
) {
93 var relativeFilePath
= path
.resolve(filePath
).slice(process
.cwd().length
+ 1);
94 superagent
.del(config
.server() + API
+ relativeFilePath
).end(function (error
, result
) {
95 if (error
.status
=== 404) return console
.log('No such file or directory');
96 if (error
) return console
.log('Failed', result
? result
.body : error
);
97 console
.log('Success', result
.body
);