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

18.92% Statements 14/74
0% Branches 0/46
0% Functions 0/11
18.92% Lines 14/74
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                          9x 9x 9x 9x 9x 9x 9x 9x   9x 9x 9x 9x 9x                                                                                                                                                                                                                                             9x            
/* 
 * 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 _ = require('lodash')
const chalk = require('chalk')
const path = require('path')
const ora = require('ora')
const util = require('util')
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
    if(projectInfo.BackendProjectID && projectInfo.BackendProjectID.length > 0){
        retrieveAWSExportFile(awsDetails, callback)
    }
}
 
function onClearBackend(projectInfo){
    _projectInfo = projectInfo
    removeLocalAWSExportFiles()
}
 
function onProjectConfigChange(projectInfo_old, projectInfo){
    if(projectInfo_old.SourceDir != projectInfo.SourceDir){
        let srcDirExportFilePath_old = pathManager.getSrcDirExportFilePath(projectInfo_old)
        if(srcDirExportFilePath_old && fs.existsSync(srcDirExportFilePath_old)){
            fs.removeSync(srcDirExportFilePath_old)
        }
        let awsExportFilePath = pathManager.getAWSExportFilePath(projectInfo.ProjectPath) 
        if(awsExportFilePath && fs.existsSync(awsExportFilePath)){
            let srcDir = pathManager.getSrcDirPath(projectInfo)
            if(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()
        if(err){
            awsExceptionHandler.handleMobileException(err)
        }else if(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) {
                if(err){
                    console.log(err)
                }else{
                    let tempExportFilePath = dfops.findFile(tempExtractDirPath, awsmobileJSConstant.AWSExportFileName)
                    if(tempExportFilePath){
                        updateLocalAWSExportFiles(tempExportFilePath)
                        fs.removeSync(tempExtractDirPath) 
                        fs.removeSync(tempZipFilePath)
                        if(callback){
                            callback()
                        }
                    }
                }
            })
        })
    })
}
 
function updateLocalAWSExportFiles(sourceFilePath){ 
    let awsExportFilePath = pathManager.getAWSExportFilePath(_projectInfo.ProjectPath) 
    if(awsExportFilePath){
        fs.copySync(sourceFilePath, awsExportFilePath)
        console.log('awsmobile project\'s access information logged at: ' + 
        chalk.blue(pathManager.getAWSExportFilePath_relative(_projectInfo.ProjectPath) ))    
    }
 
    let srcDir = pathManager.getSrcDirPath(_projectInfo)
    if(srcDir && fs.existsSync(srcDir)){
        let srcDirExportFilePath = pathManager.getSrcDirExportFilePath(_projectInfo)
        fs.copySync(sourceFilePath, srcDirExportFilePath)
        console.log('awsmobile project\'s access information copied to: ' + 
        chalk.blue(pathManager.getSrcDirExportFilePath_relative(_projectInfo)))    
    }
}
 
function removeLocalAWSExportFiles(){ 
    let awsExportFilePath = pathManager.getAWSExportFilePath(_projectInfo.ProjectPath) 
    if(fs.existsSync(awsExportFilePath)){
        fs.removeSync(awsExportFilePath)
    }
 
    let srcDir = pathManager.getSrcDirPath(_projectInfo)
    if(srcDir && fs.existsSync(srcDir)){
        let srcDirExportFilePath = pathManager.getSrcDirExportFilePath(_projectInfo)
        if(srcDirExportFilePath && fs.existsSync(srcDirExportFilePath)){
            fs.removeSync(srcDirExportFilePath)
        }
    }
}
 
module.exports = {
    getAWSExportFile,
    onClearBackend,
    onProjectConfigChange
}