All files / lib/backend-operations cloud-api-lambda-uploader.js

89.66% Statements 52/58
60% Branches 15/25
100% Functions 10/10
89.66% Lines 52/58

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                          11x 11x 11x 11x   11x 11x               2x 2x 2x   2x 2x   1x 1x   1x 1x 1x   1x 1x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x     2x 2x   2x 2x 2x     2x   2x 2x 1x                               1x         2x 2x           1x         4x 4x   4x         1x 1x   1x       1x 1x 1x           1x     11x      
/* 
 * 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 mime = require('mime-types')
 
const awsClient = require('../aws-operations/aws-client.js')
const pathManager = require('../utils/awsmobilejs-path-manager.js')
 
let _featureBuildDirPath
let _projectInfo
let _backendProjectDetails
let _callback
 
function uploadLambdaZipFiles(projectInfo, awsDetails, backendProjectDetails, featureName, callback){
    _projectInfo = projectInfo
    _backendProjectDetails = backendProjectDetails
    _callback = callback
 
    _featureBuildDirPath = pathManager.getBackendBuildFeatureDirPath(_projectInfo.ProjectPath, featureName)
    if(fs.existsSync(_featureBuildDirPath)){
 
        let deploymentBucket = getDeploymentBucket()
        let s3 = awsClient.S3(awsDetails, getDeploymentBucketRegion(deploymentBucket))
 
        let fileList = fs.readdirSync(_featureBuildDirPath)
        let uploadCount = 0
        let totalFileCount = fileList.length
        
        Eif(totalFileCount > 0){
            fileList.forEach(function(fileName) {
                let filePath = path.join(_featureBuildDirPath, fileName)
                let current = fs.lstatSync(filePath)
                Eif(current.isFile()) {
                    let key = 'uploads/' + fileName
                    
                    //first check if the file is already uploaded
                    let headRequestParams = {Bucket: getDeploymentBucketName(deploymentBucket), Key: key}
                    s3.headObject(headRequestParams, function(err, data){
                        Eif(err){ //no such file in S3, the file has not been uploaded, so go ahead with the upload
                            let fileStream = fs.createReadStream(filePath)
                            fileStream.on('error', function(err) {
                                console.log(fileName + ' read stream error', err)
                            })                    
                            
                            let contentType = mime.lookup(fileName)
                            let uploadParams = {Bucket: getDeploymentBucketName(deploymentBucket), Key: key, Body: fileStream, ContentType: contentType? contentType: "text/plain"}
                            
                            console.log('   uploading ' + fileName)
                            s3.upload (uploadParams, function (err, data) {
                                Iif (err) {
                                    console.log(chalk.red(fileName + ": upload error: ", err))
                                }else{
                                    console.log("   upload Successful ", fileName)
                                }
                                uploadCount++ 
                                if(uploadCount == totalFileCount){
                                    onPreBackendUpdateComplete()
                                }
                            })
                        }else{ //the file has already been uploaded
                            uploadCount++ 
                            if(uploadCount == totalFileCount){
                                onPreBackendUpdateComplete()
                            }
                        }
                    })
                }
            }, this)
        }else{
            onPreBackendUpdateComplete()
        }
    }else{
        onPreBackendUpdateComplete()
    }
}
 
function onPreBackendUpdateComplete(){
    Eif(_callback){
        _callback()
    }
}
 
 
function getDeploymentBucket(){
    return _backendProjectDetails.resources.find(isDeploymentBucket)
}
 
function getDeploymentBucketName(deploymentBucket){
    let deploymentBucketName 
    Eif(deploymentBucket){
        deploymentBucketName = deploymentBucket.name
    }
    return deploymentBucketName
}
 
function getDeploymentBucketRegion(deploymentBucket){
    let deploymentBucketRegion
    Eif(deploymentBucket){
        deploymentBucketRegion = deploymentBucket.attributes.region
    }
    return deploymentBucketRegion
}
 
function isDeploymentBucket(resource){
    let result = false
    try{
        result = (resource.type == "AWS::S3::Bucket" && 
                    resource.feature == 'common' &&
                    resource.name.includes('-deployments-'))
    }catch(e){
        result = false
    }
    return result
}
 
module.exports = {
    uploadLambdaZipFiles
}