# all files / cli/init/ install-tractor-dependencies-locally.js

100% Statements 38/38
100% Branches 6/6
100% Functions 12/12
100% Lines 24/24
1 branch Ignored

    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

    1

    1

    1
    6

    6
    6

    1
    6

    6
    6
    6

    6
    6

    6

    1
    6

    1
    6
    5

    1

    6
    39
    39

     'use strict';

    // Constants;
    import constants from '../../constants';
    const TO_INSTALL = [
        'bluebird@2.10.2',
        'chai@2.3.0',
        'chai-as-promised@5.1.0',
        'cucumber@1.3.1',
        'cucumber-html-reporter@0.3.5',
        'httpbackend@1.2.1',
        'protractor@4.0.11',
        'protractor-cucumber-framework@0.6.0'
    ];

    // Utilities:
    import log from 'npmlog';
    import Promise from 'bluebird';

    // Dependencies:
    const childProcess = Promise.promisifyAll(require('child_process'));

    export default {
        run: installTractorDependenciesLocally
    };

    function installTractorDependenciesLocally () {
        return getInstalledDependencies()
        .then((installed) => {
            let toInstall = filterAreadyInstalledDependencies(installed, TO_INSTALL);
            return installDependencies(toInstall);
        });
    }

    function getInstalledDependencies () {
        log.info('Checking installed npm dependencies...');

        let resolve;
        let deferred = new Promise((...args) => {
            resolve = args[0];
        });

        let ls = childProcess.exec(constants.GET_INSTALLED_DEPENDENCIES_COMMAND);
        ls.stdout.on('data', data => resolve(data));

        return deferred;
    }

    function filterAreadyInstalledDependencies (installed, dependencies) {
        return dependencies.filter((dependency) => !new RegExp(dependency, 'gm').test(installed));
    }

    function installDependencies (modules) {
        if (modules.length) {
            log.info('Installing npm dependencies for tractor...');
        } else {
            log.info('All npm dependencies for tractor already installed.');
        }

        return Promise.all(modules.map((module) => {
            log.info(`Installing "${module}"...`);
            return childProcess.execAsync(`${constants.INSTALL_DEPENDENCIES_COMMAND}${module}`)
            .then(() => log.verbose(`Installed "${module}".`))
            .catch(() => log.error(`Couldn't install "${module}". Either run "tractor init" again, or install it manually by running "npm install ${module}"`));
        }));
    }