]>
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');
25 function collectFiles(filesOrFolders
) {
28 filesOrFolders
.forEach(function (filePath
) {
29 var stat
= fs
.statSync(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
));
37 console
.log('Skipping %s', filePath
.cyan
);
44 function login(server
) {
45 console
.log('Using server', server
);
46 config
.set('server', server
);
49 function put(filePath
, otherFilePaths
) {
52 var files
= collectFiles([ filePath
].concat(otherFilePaths
));
54 async
.eachSeries(files
, function (file
, callback
) {
55 var relativeFilePath
= path
.resolve(file
).slice(process
.cwd().length
+ 1);
57 console
.log('Uploading file %s', relativeFilePath
.cyan
);
59 superagent
.put(config
.server() + API
+ relativeFilePath
).attach('file', file
).end(callback
);
62 console
.log('Failed to put file.', error
);
70 function get(filePath
) {
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
);
77 if (result
.body
&& result
.body
.entries
) {
78 console
.log('Files:');
79 result
.body
.entries
.forEach(function (entry
) {
80 console
.log('\t %s', entry
);
83 console
.log(result
.text
);
88 function del(filePath
) {
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
);