all files / src/components/ packager.js

91.89% Statements 34/37
87.5% Branches 7/8
100% Functions 0/0
91.43% Lines 32/35
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 7617×                                                                                        
import fs from 'fs-extra'
import path from 'path'
import createDebugger from 'debug'
import childProcess from 'child_process'
import { handleError } from '../actions/creators.js'
import { ENV_PATH } from '../constants/paths.js'
import {
    ENV_NPM_PACKAGES_PATH,
    ENV_JSPM_PACKAGES_PATH
} from '../constants/paths.js'
import {
    SET_PACKAGER_INSTALLATION_PREPARATION,
    CLEAR_PACKAGER_INSTALLATION_PREPARATION,
} from '../constants/actionTypes.js'
import { UNKNOWN_PACKAGE_INSTALL_ERROR } from '../constants/language/errors.js'
 
const debug = createDebugger('unistack:packager')
 
const Packager = ({ getState, dispatch }) => {
    const state = getState()
    const installing = getStateLeaf(state, 'INSTALLING')
    const install = getStateLeaf(state, 'INSTALL')
    if (install && !installing) {
        try {
            debug('Checking if packages are already installed...')
            fs.accessSync(ENV_NPM_PACKAGES_PATH, fs.F_OK)
            fs.accessSync(ENV_JSPM_PACKAGES_PATH, fs.F_OK)
            debug('Yep, their are installed.')
            return dispatch({type: CLEAR_PACKAGER_INSTALLATION_PREPARATION })
        } catch (e) {
            debug('!!!!--Preparing to install packages...')
            dispatch({type: SET_PACKAGER_INSTALLATION_PREPARATION })
        }
        return installPackages()
        .then(() => {
            debug('@@@@--Installed packages')
            return dispatch({type: CLEAR_PACKAGER_INSTALLATION_PREPARATION })
        })
        .catch(error => {
            debug('@@@@--Failed to install packages %s', error.stack)
            return dispatch(makePackageInstallError(error))
        })
    }
}
 
const getStateLeaf = (state, leaf) => {
    switch(leaf) {
        case 'INSTALL':
            return state.packager.install
        case 'INSTALLING':
            return state.packager.installing
    }
}
 
const installPackages = () => {
    const jspmExecutable = path.join(ENV_NPM_PACKAGES_PATH, '.bin', 'jspm')
    const command = `npm install && yes | ${jspmExecutable} install`
    const envPath = ENV_PATH
    const options = { cwd: envPath }
    return new Promise((resolve, reject) => {
        debug('Running packages install command: %s', command)
        debug('This will likely take a few minutes...')
        childProcess.exec(command, options, (error, stdout, stderr) => {
            Eif (error) return reject(error)
            resolve(true)
        })
    })
}
 
const makePackageInstallError = error => {
    const message = UNKNOWN_PACKAGE_INSTALL_ERROR
    return handleError(error, { message, fatal: true })
}
 
export default Packager