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

var which = require('which'),
    colors = require('colors'),
    glob = require('glob');

module.exports = function configure(argv, callback) {

    'use strict';

    var python = argv.py || process.env.PYTHON || 'python',
        isWin = /^win/.test(process.platform);

    function checkPython() {

        which(python, function whichTest(err) {
            if (err !== null) {
                if (isWin) { guessPython(); } else { failNoPython(); }
            } else {
                process.env.PYTHON = python;
                callback(null);
            }
        });
    }

    function guessPython() {

        glob('\\Python**\\python.exe', function find(err, files) {

            if (err !== null) { return callback(err); }

            if (!files.length) { return failNoPython(); }

            python = files.shift();
            process.env.PYTHON = python;
            callback(null);

        });
    }

    function failNoPython() {
        console.log('[' + 'i'.red + '] Can\'t find Python executable ' + python.red + '. Check if Python installed ... if so set the ' + 'PYTHON'.green + ' env variable.');
        process.exit(0);
    }

    checkPython();
};