aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli/actions.js
blob: 36296ae17a9c64a037ae7ce232a2b621f968e3ea (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
'use strict';

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

var superagent = require('superagent'),
    config = require('./config'),
    async = require('async'),
    fs = require('fs'),
    path = require('path');

require('colors');

var API = '/api/files/';

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

    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) {
    console.log('Using server', server);
    config.set('server', server);
}

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).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();

    var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
    superagent.get(config.server() + API + relativeFilePath).end(function (error, result) {
        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).end(function (error, result) {
        if (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);
    });
}