aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli/actions.js
blob: 043909d4a7b5db529f83a8747f8a4c5950efe104 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';

exports.login = login;
exports.put = put;
exports.get = get;
exports.del = del;

var superagent = require('superagent'),
    config = require('./config.js'),
    readlineSync = require('readline-sync'),
    async = require('async'),
    fs = require('fs'),
    path = require('path');

require('colors');

var API = '/api/files/';

var gQuery = {};

function checkConfig() {
    if (!config.server() || !config.username() || !config.password()) {
        console.log('You have run "login" first');
        process.exit(1);
    }

    gQuery = { username: config.username(), password: config.password() };

    console.log('Using server %s', config.server().yellow);
}

function collectFiles(filesOrFolders) {
    var tmp = [];

    filesOrFolders.forEach(function (filePath) {
        var stat = fs.statSync(filePath);

        if (stat.isFile()) {
            tmp.push(filePath);
        } else if (stat.isDirectory()) {
            var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); });
            tmp = tmp.concat(collectFiles(files));
        } else {
            console.log('Skipping %s', filePath.cyan);
        }
    });

    return tmp;
}

function login(server) {
    if (server[server.length-1] === '/') server = server.slice(0, -1);

    console.log('Using server', server);

    var username = readlineSync.question('Username: ', { hideEchoBack: false });
    var password = readlineSync.question('Password: ', { hideEchoBack: true });

    superagent.get(server + API + '/').query({ username: username, password: password }).end(function (error, result) {
        if (result.status === 401) {
            console.log('Login failed.');
            process.exit(1);
        }

        config.set('server', server);
        config.set('username', username);

        // TODO this is clearly bad and needs fixing
        config.set('password', password);

        gQuery = { username: username, password: password };

        console.log('Done');
    });
}

function put(filePath, otherFilePaths, options) {
    checkConfig();

    var files = collectFiles([ filePath ].concat(otherFilePaths));

    async.eachSeries(files, function (file, callback) {
        var relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);

        console.log('Uploading file %s -> %s', relativeFilePath.cyan, ((options.destination ? options.destination : '') + '/' + relativeFilePath).cyan);

        superagent.put(config.server() + API + relativeFilePath).query(gQuery).attach('file', file).end(callback);
    }, function (error) {
        if (error) {
            console.log('Failed to put file.', error);
            process.exit(1);
        }

        console.log('Done');
    });
}

function get(filePath) {
    checkConfig();

    superagent.get(config.server() + API + filePath).query(gQuery).end(function (error, result) {
        if (error && error.status === 401) return console.log('Login failed');
        if (error && error.status === 404) return console.log('No such file or directory');
        if (error) return console.log('Failed', result ? result.body : error);

        if (result.body && result.body.entries) {
            console.log('Files:');
            result.body.entries.forEach(function (entry) {
                console.log('\t %s', entry);
            });
        } else {
            console.log(result.text);
        }
    });
}

function del(filePath) {
    checkConfig();

    var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
    superagent.del(config.server() + API + relativeFilePath).query(gQuery).end(function (error, result) {
        if (error && error.status === 401) return console.log('Login failed');
        if (error && error.status === 404) return console.log('No such file or directory');
        if (error) return console.log('Failed', result ? result.body : error);
        console.log('Success', result.body);
    });
}