]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - cli/actions.js
b35b8dad4ee1f4bea83aafa1c3ced644398ca0ad
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 if (server
[server
.length
-1] === '/') server
= server
.slice(0, -1);
49 console
.log('Using server', server
);
50 config
.set('server', server
);
53 function put(filePath
, otherFilePaths
, options
) {
56 var files
= collectFiles([ filePath
].concat(otherFilePaths
));
58 async
.eachSeries(files
, function (file
, callback
) {
59 var relativeFilePath
= path
.resolve(file
).slice(process
.cwd().length
+ 1);
61 console
.log('Uploading file %s -> %s', relativeFilePath
.cyan
, ((options
.destination
? options
.destination : '') + '/' + relativeFilePath
).cyan
);
63 superagent
.put(config
.server() + API
+ relativeFilePath
).attach('file', file
).end(callback
);
66 console
.log('Failed to put file.', error
);
74 function get(filePath
) {
77 var relativeFilePath
= path
.resolve(filePath
).slice(process
.cwd().length
+ 1);
78 superagent
.get(config
.server() + API
+ relativeFilePath
).end(function (error
, result
) {
79 if (error
) return console
.log('Failed', result
? result
.body : error
);
81 if (result
.body
&& result
.body
.entries
) {
82 console
.log('Files:');
83 result
.body
.entries
.forEach(function (entry
) {
84 console
.log('\t %s', entry
);
87 console
.log(result
.text
);
92 function del(filePath
) {
95 var relativeFilePath
= path
.resolve(filePath
).slice(process
.cwd().length
+ 1);
96 superagent
.del(config
.server() + API
+ relativeFilePath
).end(function (error
, result
) {
97 if (error
.status
=== 404) return console
.log('No such file or directory');
98 if (error
) return console
.log('Failed', result
? result
.body : error
);
99 console
.log('Success', result
.body
);