All files / lib/utils dependency-manager.js

13.79% Statements 8/58
10.53% Branches 2/19
0% Functions 0/18
13.79% Lines 8/58
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                          2x 2x 2x 2x   2x   2x 2x                                                                                                                                                                                                                                 2x        
/* 
 * 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 } = 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 npmInstall(projectPath, callback){
    let packageJsonFilePath = path.normalize(path.join(projectPath, 'package.json'))
    if(fs.existsSync(packageJsonFilePath)){
        console.log()
        console.log('Executing ' + npm + ' install...')
        let childProcess = spawn(npm, ['install'], {cwd: projectPath, env: process.env, stdio: 'inherit'})
        childProcess.on('exit', function(code) {
            let codeString = code.toString()
            if(code != 0){
                codeString = chalk.red(code.toString())
            }
            console.log(npm + ' install returned ' + codeString)
            console.log()
            if(callback){
                callback()
            }
        })
    }else{
        if(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
            default: 
                result = npmInstallAmplify(projectPath)
                        .then((cwd)=>{
                            return initInfo
                        })
            break
        }
    }
 
    return result
}
 
function npmInit(cwd){
    return spawnChildProcess(npm, ['init', '-y'], cwd)
}
 
function yarnInit(cwd){
    return spawnChildProcess(yarn, ['init', '-y'], cwd)
}
 
function npmInstallAmplify(cwd){
    return spawnChildProcess(npm, ['install', 'aws-amplify'], cwd)
}
 
function npmInstallAmplifyReact(cwd){
    return spawnChildProcess(npm, ['install', 'aws-amplify-react'], cwd)
}
 
function npmInstallAmplifyReactNative(cwd){
    return spawnChildProcess(npm, ['install', 'aws-amplify-react-native'], cwd)
}
 
function yarnAddAmplify(cwd){
    return spawnChildProcess(yarn, ['add', 'aws-amplify'], cwd)
}
 
function yarnAddAmplifyReact(cwd){
    return spawnChildProcess(yarn, ['add', 'aws-amplify-react'], cwd)
}
 
function yarnAddAmplifyReactNative(cwd){
    return spawnChildProcess(yarn, ['add', 'aws-amplify-react-native'], cwd)
}
 
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()
            if(code != 0){
                codeString = chalk.red(code.toString())
            }
            console.log(command + ' ' + args.join(' ') + ' returned ' + codeString)
            resolve(cwd)
        })
    })
}
 
module.exports = {
    npmInstall, 
    setupAmplifyDependency
}