All files / lib/init-steps s2-choose-strategy.js

70.09% Statements 82/117
51.22% Branches 21/41
81.82% Functions 18/22
70.09% Lines 82/117

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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300                          2x 2x     2x               9x   9x 4x   5x     9x     2x     2x   2x 2x   1x 1x   2x 2x     9x       2x   2x 1x   1x 1x     2x       1x 1x   1x 1x 1x   1x                         1x     1x       2x 2x               2x 2x   2x 2x 2x   2x 1x 1x         1x     2x       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                 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 chalk = require('chalk')
const inquirer = require('inquirer')
 
//if initInfo.strategy is not set, the setup-backend step will not carry out any functions
const strategy = [
    'create', //use the default yaml to create a mobile project as the backend
    'import', //if a valid backend spec is already in the project, create a mobile project based on it as the backend
    'link', //if a mobile project id is in the command arguments, link to it as the backend
    'conform' //if there is an update in the cli, run init on a valid awsmobilejs project setup by the cli of previous version
]
 
function run(initInfo){
    let result = initInfo
 
    if(initInfo.mobileProjectID){
        initInfo.strategy = 'link'
    }else{
        initInfo.strategy = 'create'
    }
 
    switch(initInfo.initialStage){
        case 'clean-slate':
            //nothing extra
        break
        case 'invalid': 
            //nothing extra
        break
        case 'backend-valid':
            result = chooseForBackendValid(initInfo)
        break
        case 'project-info-valid':
            result = chooseForProjectInfoValid(initInfo)
        break
        case 'valid': 
            result = chooseForValid(initInfo)
        break
    }
 
    return result
}
 
function chooseForBackendValid(initInfo){
    let result = initInfo
 
    if(initInfo.mobileProjectID){
        initInfo.strategy = 'link'
    }else{ //if no mobile project id, will import.
        console.log('A valid backend specification is detected in this project')
        result = chooseImportOrCreate(initInfo)
    }
 
    return result
}
 
function chooseForProjectInfoValid(initInfo){
    let result = initInfo
    initInfo.strategy = undefined
 
    console.log('this project\'s backend is currently set to be ' + chalk.blue(initInfo.projectInfo.BackendProjectName))
    console.log('with mobile project id = ' + chalk.blue(initInfo.projectInfo.BackendProjectID))
    console.log('and was initialized at ' + chalk.blue(initInfo.projectInfo.InitializationTime))
 
    Iif(initInfo.mobileProjectID){
        if(initInfo.mobileProjectID != initInfo.projectInfo.BackendProjectID){
            result = confirmToSwitchBackend(initInfo).then((initInfo)=>{
                if(!initInfo.strategy){ //user declined to switch backend
                    return confirmToReEstablishBackend(initInfo)
                }else{
                    return initInfo
                }
            })
        }else{
            prepReEstablish(initInfo)
        }
    }else{
        result = chooseReEstablishOrCreate(initInfo)
    }
 
    return result
}
 
function chooseForValid(initInfo){
   return inquirerForValid(initInfo).then((initInfo)=>{
       if(!initInfo.strategy){
            prepConform(initInfo)
       }
       return initInfo
   })
}
 
async function inquirerForValid(initInfo){
        let result = initInfo
        initInfo.strategy = undefined
    
        console.log('this project\'s current backend is ' + chalk.blue(initInfo.projectInfo.BackendProjectName))
        console.log('with mobile project id = ' + chalk.blue(initInfo.projectInfo.BackendProjectID))
        console.log('and was initialized at ' + chalk.blue(initInfo.projectInfo.InitializationTime))
    
        if(initInfo.mobileProjectID){
            Eif(initInfo.mobileProjectID != initInfo.projectInfo.BackendProjectID){
                result = await confirmToSwitchBackend(initInfo)
            }else{
                console.log('you have specified the same id: ' + chalk.blue(initInfo.projectInfo.BackendProjectID))
            }
        }else{
            result = await confirmToCreateNewBackend(initInfo)
        }
    
        return result
}
 
function confirmToCreateNewBackend(initInfo){
    initInfo.strategy = undefined
 
    let question = {
        type: 'confirm',
        name: 'confirmCreateNew',
        message: 'create a new awsmobile project as the backend',
        default: false
    }
 
    Iif(initInfo.yesFlag){
        return initInfo
    }else{
        return inquirer.prompt(question).then(function (answers) {
            Eif(answers.confirmCreateNew){
                prepCreateNew(initInfo)
            }
            return initInfo
        })
    }
}
 
function confirmToReEstablishBackend(initInfo){
    initInfo.strategy = undefined
 
    let message = 're-establish association with the original backend awsmobile project'
    let question = {
        type: 'confirm',
        name: 'confirmReEstablish',
        message: message,
        default: true
    }
 
    if(initInfo.yesFlag){
        console.log(message)
        prepReEstablish(initInfo)
        return initInfo
    }else{
        return inquirer.prompt(question).then(function (answers) {
            if(answers.confirmReEstablish){
                prepReEstablish(initInfo)
            }
            return initInfo
        })
    }
}
 
function confirmToSwitchBackend(initInfo){
    initInfo.strategy = undefined
    
    let message = 'switch backend to awsmobile project with id = ' + initInfo.mobileProjectID
    let question = {
        type: 'confirm',
        name: 'confirmSwitch',
        message: message,
        default: false
    }
 
    Iif(initInfo.yesFlag){
        return initInfo
    }else{
        return inquirer.prompt(question).then(function (answers) {
            Eif(answers.confirmSwitch){
                prepSwitch(initInfo)
            }
            return initInfo
        })
    }
}
 
function chooseReEstablishOrCreate(initInfo){
    initInfo.strategy = undefined
 
    let question = {
        type: 'list',
        name: 'ReEstablishOrNew',
        message: 'create a new backend or re-establish association with the original backend',
        choices: [
            {
              name: 're-establish association',
              value: 'reestablish'
            },
            {
              name: 'create a new backend',
              value: 'create'
            }
          ]
        }
 
    Iif(initInfo.yesFlag){
        console.log('re-establish association with the original backend')
        prepReEstablish(initInfo)
        return initInfo
    }else{
        return inquirer.prompt(question).then(function (answers) {
            switch (answers.ReEstablishOrNew) {
                case 'reestablish': 
                    prepReEstablish(initInfo)
                break
                case 'create': 
                    prepCreateNew(initInfo)
                break
            }
            return initInfo
        })
    }
}
 
function chooseImportOrCreate(initInfo){
    initInfo.strategy = undefined
 
    let question = {
        type: 'list',
        name: 'ImportOrNew',
        message: 'create the new backend with default features or specified features',
        choices: [
            {
              name: 'specified features',
              value: 'import'
            },
            {
              name: 'default features',
              value: 'create'
            }
          ]
        }
 
    Iif(initInfo.yesFlag){
        console.log('create the new backend with the specified features')
        prepImport(initInfo)
        return initInfo
    }else{
        return inquirer.prompt(question).then(function (answers) {
            switch (answers.ImportOrNew) {
                case 'import': 
                    prepImport(initInfo)
                break
                case 'create': 
                    prepCreateNew(initInfo)
                break
              }
            return initInfo
        })
    }
}
 
function prepCreateNew(initInfo){
    // console.log('init will now try to create a new backend awsmobile project')
    initInfo.strategy = 'create'
}
 
function prepSwitch(initInfo){
    console.log('init will now try to switch to the newly specified backend')
    initInfo.strategy = 'link'
}
 
function prepReEstablish(initInfo){
    console.log('init will now try to re-establish the association with the backend awsmobile project')
    initInfo.strategy = 'link'
    initInfo.mobileProjectID = initInfo.projectInfo.BackendProjectID
}
 
function prepImport(initInfo){
    initInfo.strategy = 'import'
}
 
function prepConform(initInfo){
    initInfo.yesFlag = true
    initInfo.mobileProjectID = initInfo.projectInfo.BackendProjectID
    initInfo.strategy = 'conform'
}
 
module.exports = {
    run
}