All files / lib/aws-operations mobile-exportjs-file-manager.js

97.01% Statements 65/67
66.67% Branches 28/42
100% Functions 11/11
97.01% Lines 65/67

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                          12x 12x 12x 12x 12x   12x 12x 12x 12x 12x         1x 1x 1x         1x 1x       1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x               1x 1x   1x   1x         1x 1x 1x   1x 1x             1x 1x 1x 1x 1x   1x 1x     1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x         1x 1x 1x     1x 1x 1x 1x 1x         12x            
/* 
 * 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 chalk = require('chalk')
const ora = require('ora')
const https = require('https')
const extract = require('extract-zip')
 
const awsClient = require('./aws-client.js')
const awsExceptionHandler = require('./aws-exception-handler.js')
const awsmobilejsConstant = require('../utils/awsmobilejs-constant.js')
const dfops = require('../utils/directory-file-ops.js')
const pathManager = require('../utils/awsmobilejs-path-manager.js')
 
let _projectInfo
 
function getAWSExportFile(projectInfo, awsDetails, callback){
    _projectInfo = projectInfo
    Eif(projectInfo.BackendProjectID && projectInfo.BackendProjectID.length > 0){
        retrieveAWSExportFile(awsDetails, callback)
    }
}
 
function onClearBackend(projectInfo){
    _projectInfo = projectInfo
    removeLocalAWSExportFiles()
}
 
function onProjectConfigChange(projectInfo_old, projectInfo){
    Eif(projectInfo_old.SourceDir != projectInfo.SourceDir){
        let srcDirExportFilePath_old = pathManager.getSrcDirExportFilePath(projectInfo_old)
        Eif(srcDirExportFilePath_old && fs.existsSync(srcDirExportFilePath_old)){
            fs.removeSync(srcDirExportFilePath_old)
        }
        let awsExportFilePath = pathManager.getAWSExportFilePath(projectInfo.ProjectPath) 
        Eif(awsExportFilePath && fs.existsSync(awsExportFilePath)){
            let srcDir = pathManager.getSrcDirPath(projectInfo)
            Eif(srcDir && fs.existsSync(srcDir)){
                let srcDirExportFilePath = pathManager.getSrcDirExportFilePath(projectInfo)
 
                fs.copySync(awsExportFilePath, srcDirExportFilePath)
                console.log()
                console.log('aws-exports.js file is copied into your project\'s source directory')
                console.log(chalk.blue(srcDirExportFilePath))
            }
        }
    }
}
 
function retrieveAWSExportFile(awsDetails, callback)
{
    let spinner = ora('retrieving aws-exports.js')
    spinner.start()
 
    let mobile = awsClient.Mobile(awsDetails)
 
    let exportBundleParams = { 
        bundleId: 'app-config',
        projectId: _projectInfo.BackendProjectID,
        platform: 'JAVASCRIPT'
    }
    mobile.exportBundle(exportBundleParams, function (err, data) {
        spinner.stop()
        Iif(err){
            awsExceptionHandler.handleMobileException(err)
        }else Eif(data && data.downloadUrl){
            downloadAWSExportFile(data.downloadUrl, callback)
        }
    })
}
 
 
function downloadAWSExportFile(downloadUrl, callback) { 
    let tempExtractDirPath = pathManager.getAWSExportExtractTempDirPath(_projectInfo.ProjectPath)
    let tempZipFilePath = pathManager.getAWSExportTempZipFilePath(_projectInfo.ProjectPath)
    let tempZipFile = fs.createWriteStream(tempZipFilePath)
    let request = https.get(downloadUrl, function(response) {
        response.pipe(tempZipFile)
        .on('close',()=>{
            extract(tempZipFilePath, {dir: tempExtractDirPath}, function (err) {
                Iif(err){
                    console.log(err)
                }else{
                    let tempExportFilePath = dfops.findFile(tempExtractDirPath, awsmobilejsConstant.AWSExportFileName)
                    Eif(tempExportFilePath){
                        updateCurrentAWSExportFiles(tempExportFilePath)
                        fs.removeSync(tempExtractDirPath) 
                        fs.removeSync(tempZipFilePath)
                        Eif(callback){
                            callback()
                        }
                    }
                }
            })
        })
    })
}
 
function updateCurrentAWSExportFiles(sourceFilePath){ 
    let awsExportFilePath = pathManager.getAWSExportFilePath(_projectInfo.ProjectPath) 
    Eif(awsExportFilePath){
        fs.copySync(sourceFilePath, awsExportFilePath)
        console.log('awsmobile project\'s access information logged at: ')
        console.log('    ' + chalk.blue(pathManager.getAWSExportFilePath_relative(_projectInfo.ProjectPath) ))    
    }
}
 
function removeLocalAWSExportFiles(){ 
    let awsExportFilePath = pathManager.getAWSExportFilePath(_projectInfo.ProjectPath) 
    Eif(fs.existsSync(awsExportFilePath)){
        fs.removeSync(awsExportFilePath)
    }
 
    let srcDir = pathManager.getSrcDirPath(_projectInfo)
    Eif(srcDir && fs.existsSync(srcDir)){
        let srcDirExportFilePath = pathManager.getSrcDirExportFilePath(_projectInfo)
        Eif(srcDirExportFilePath && fs.existsSync(srcDirExportFilePath)){
            fs.removeSync(srcDirExportFilePath)
        }
    }
}
 
module.exports = {
    getAWSExportFile,
    onClearBackend,
    onProjectConfigChange
}