All files / lib/utils dependency-manager.js

91.89% Statements 68/74
69.7% Branches 23/33
100% Functions 18/18
91.89% Lines 68/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171                          26x 26x 26x 26x   26x   26x 26x     3x 3x 2x 2x 2x 1x 1x     2x 2x   2x 2x 2x     2x 2x 2x 2x       1x 1x           5x   5x 5x 1x     1x 1x     4x   1x     1x   1x   1x     1x   1x     1x     1x   1x   1x   1x   1x       5x       1x       3x       1x       2x 2x             1x 1x             1x 1x               5x 5x 5x       5x         9x 9x 9x 9x 9x 9x 9x     9x 9x         26x          
/* 
 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
 * the License. A copy of the License is located at
 *
 *     http://aws.amazon.com/apache2.0/
 *
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
*/
"use strict";
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const { spawn, execSync } = require('child_process')
 
const dfOps = require('./directory-file-ops.js')
 
const npm = /^win/.test(process.platform) ? "npm.cmd" : "npm" 
const yarn = /^win/.test(process.platform) ? "yarn.cmd" : "yarn" 
 
function installDependencies(projectInfo, callback){
    let packageJsonFilePath = path.normalize(path.join(projectInfo.ProjectPath, 'package.json'))
    if(fs.existsSync(packageJsonFilePath)){
        console.log()
        let command = npm
        if(projectInfo.Framework=='react' || projectInfo.Framework == 'react-native'){
            Eif(isYarnInstalled()){
                command = yarn
            }
        }
        console.log('Executing ' + command + ' install...')
        let childProcess = spawn(command, ['install'], {cwd: projectInfo.ProjectPath, env: process.env, stdio: 'inherit'})
       
        childProcess.on('exit', function(code) {
            let codeString = code.toString()
            Iif(code != 0){
                codeString = chalk.red(code.toString())
            }
            console.log(command + ' install returned ' + codeString)
            console.log()
            Eif(callback){
                callback()
            }
        })
    }else{
        Eif(callback){
            callback()
        }
    }
}
 
function setupAmplifyDependency(initInfo){
    let result = initInfo
 
    let projectPath = initInfo.projectPath
    if(!initInfo.packageJson){
        result = npmInit(projectPath)
        .then(npmInstallAmplify)
        .then((cwd)=>{
            initInfo.packageJson = dfOps.readJsonFile(path.normalize(path.join(projectPath, 'package.json')))
            return initInfo
        })
    }else{
        switch(initInfo.framework){
            case 'react': 
                result = yarnAddAmplify(projectPath)
                        .then(yarnAddAmplifyReact)
                        .then((cwd)=>{
                            return initInfo
                        })
            break
            case 'react-native': 
                result = yarnAddAmplify(projectPath)
                        .then(yarnAddAmplifyReactNative)
                        .then((cwd)=>{
                            return initInfo
                        })
            break
            case 'angular': 
            case 'ionic': 
                result = npmInstallAmplify(projectPath)
                        .then(npmInstallAmplifyAngular)
                        .then((cwd)=>{
                            return initInfo
                        })
            break
            default: 
                result = npmInstallAmplify(projectPath)
                        .then((cwd)=>{
                            return initInfo
                        })
            break
        }
    }
 
    return result
}
 
function npmInit(cwd){
    return spawnChildProcess(npm, ['init', '-y'], cwd)
}
 
function npmInstallAmplify(cwd){
    return spawnChildProcess(npm, ['install', 'aws-amplify'], cwd)
}
 
function npmInstallAmplifyAngular(cwd){
    return spawnChildProcess(npm, ['install', 'aws-amplify-angular'], cwd)
}
 
function yarnAddAmplify(cwd){
    Eif(isYarnInstalled()){
        return spawnChildProcess(yarn, ['add', 'aws-amplify'], cwd)
    }else{
        return spawnChildProcess(npm, ['install', 'aws-amplify'], cwd)
    }
}
 
function yarnAddAmplifyReact(cwd){
    Eif(isYarnInstalled()){
        return spawnChildProcess(yarn, ['add', 'aws-amplify-react'], cwd)
    }else{
        return spawnChildProcess(npm, ['install', 'aws-amplify-react'], cwd)
    }
}
 
function yarnAddAmplifyReactNative(cwd){
    Eif(isYarnInstalled()){
        return spawnChildProcess(yarn, ['add', 'aws-amplify-react-native'], cwd)
    }else{
        return spawnChildProcess(npm, ['install', 'aws-amplify-react-native'], cwd)
    }
}
 
function isYarnInstalled(){
    var isYarnInstalled
    try {
        execSync('yarn --version', {stdio: 'ignore'})
        isYarnInstalled = true
    } catch (err) {
        isYarnInstalled = false
    }
    return isYarnInstalled
}
 
function spawnChildProcess(command, args, cwd){
 
    return new Promise((resolve, reject)=>{
        console.log()
        console.log('Executing ' + command + ' ' + args.join(' ') + ' ...')
        let childProcess = spawn(command, args, {cwd: cwd, env: process.env, stdio: 'inherit'})
        childProcess.on('exit', function(code) {
            let codeString = code.toString()
            Iif(code != 0){
                codeString = chalk.red(code.toString())
            }
            console.log(command + ' ' + args.join(' ') + ' returned ' + codeString)
            resolve(cwd)
        })
    })
}
 
module.exports = {
    installDependencies, 
    setupAmplifyDependency, 
    isYarnInstalled
}