summaryrefslogtreecommitdiff
path: root/mongo-edu.js
blob: 78cae7b3491968a2ceaea3bccbf3adfd361c1017 (plain)
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
/*
 * mongo-edu
 *
 * Copyright (c) 2014 Przemyslaw Pluta
 * Licensed under the MIT license.
 * https://github.com/przemyslawpluta/mongo-edu/blob/master/LICENSE
 */

var pkg = require('./package'),
    mdbvideos = require('./lib/login'),
    configure = require('./lib/configure'),
    optArgs = require('./lib/options'),
    validate = require('./lib/validate'),
    videoHandler = require('./lib/videos'),
    initialize = require('./lib/initialize'),
    prompts = require('./lib/prompts'),
    yargs = optArgs.build(),
    url = require('url'),
    path = require('path'),
    colors = require('colors'),
    inquirer = require('inquirer');

process.title = pkg.name;

exports.create = function start() {

    'use strict';

    console.log('\n[ ' + pkg.name.toUpperCase() + ' ' + pkg.version + ' ]\n');

    var argv = yargs.argv, slash = (/^win/.test(process.platform)) ? '\\' : '/';

    if (argv.help) { return yargs.showHelp(); }

    argv.d = path.normalize(argv.d);

    if (argv.d.substr(-1) !== slash) { argv.d += slash; }

    if (argv.load) {
        if (typeof argv.load === 'string') {
            optArgs.load(argv.load, function load(err, data, status) {
                if (err !== null || !status) { return console.log('i'.red + ' Preset: ' + argv.load.green + ' not found.'); }
                argv = data;
                initRun();
            });
        } else {
            optArgs.get(function get(err, data) {
                if (err !== null || !data.length) { return console.log('i'.magenta + ' No Presets Found.'); }
                inquirer.prompt(prompts.loadPreset(data), function prompt(answers) {
                    optArgs.load(answers.preset, function load(err, data, status) {
                        if (err !== null || !status) { return console.log('i'.red + ' Unable To Load Preset: ' + argv.load); }
                        argv = data;
                        initRun();
                    });
                });
            });
        }
    }

    function initRun() {
        validate.init(argv, function init(err, profile) {
            if (err !== null) { throw err; }

            if (argv.save) {
                if (typeof argv.save === 'string') {
                    optArgs.save(argv.save);
                } else {
                    return inquirer.prompt(prompts.savePrompt, function savePrompt(answers) {
                        argv.save = answers.save;
                        optArgs.save(answers.save);
                        initAndConfigure(profile);
                    });
                }
            }

            initAndConfigure(profile);

        });
    }

    if (!argv.load) { return initRun(); }

    function initAndConfigure(profile) {
        configure(argv, function conf(err) {
            if (err !== null) { throw err; }

            if (!argv.proxy || argv.h) { return initialize(profile, argv); }

            var proxyDetails = url.parse(argv.proxy);

            console.log('i'.magenta + ' Proxy Host: '.bold + proxyDetails.hostname.cyan + ' Port: '.bold + proxyDetails.port.cyan + ' Protocol: '.bold + proxyDetails.protocol.replace(':', '').toUpperCase().cyan);

            mdbvideos.checkProxy(argv.proxy, function get(err, data) {
                if (err !== null) {
                    if (argv.verbose) {
                        console.log('i'.red + ' Proxy Error: '.red + err.stack);
                    } else {
                        console.log('i'.red + ' Proxy Might By Unusable.'.red);
                    }
                }

                if (data) { console.log('i'.magenta + ' ' + data); }
                if (argv.test) { return videoHandler.checkProxyDownload(argv); }

                initialize(profile, argv);
            });

        });
    }

};