All files / bin feature-command.js

92.31% Statements 72/78
75.76% Branches 25/33
88.89% Functions 8/9
92.31% Lines 72/78
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                            1x 1x 1x 1x   1x 1x 1x 1x             1x             9x 9x 9x 9x 9x 9x 9x 9x 7x   3x 3x   2x 2x   2x 2x     2x 1x 1x 1x     1x                       3x 2x 1x 1x 1x     1x 1x     1x 1x 1x           2x 1x   1x 1x 1x           2x 1x   1x 1x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x   1x 1x         3x 3x 3x 3x 3x 3x 3x 3x                       1x      
#!/usr/bin/env node
/* 
 * 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 program = require('commander')
const chalk = require('chalk')
const _ = require('lodash')
 
const projectInfoManager = require('../lib/project-info-manager.js')
const pathManager = require('../lib/utils/awsmobilejs-path-manager.js')
const backendSpecManager = require('../lib/backend-operations/backend-spec-manager.js')
const mobileFeatures = require('../lib/aws-operations/mobile-features.js')
 
let _featureName
let _args
let _featureOps
 
const _commonCommands = 
{
    'enable': 'enable {featureName} in the backend awsmobile project',
    'disable': 'disable {featureName} in the backend awsmobile project',
    'configure': 'configure the feature specifics for {featureName}'
}
  
function run(featureName, args) {
    _featureName = featureName
    _args = args
    let featureOpsFilePath = pathManager.getOpsFeatureFilePath(featureName)
    Eif(fs.existsSync(featureOpsFilePath)){
        _featureOps = require(featureOpsFilePath)
        Eif(args.length > 3){
            let command = args[3]
            if(_commonCommands.hasOwnProperty(command)){
                switch(command){
                    case 'enable':
                        enable()
                    break
                    case 'disable':
                        disable()
                    break
                    case 'configure':
                        configure()
                    break;
                }
            }else{
                if(_featureOps.hasCommand(command)){
                    let projectInfo = projectInfoManager.getProjectInfo()
                    Eif(projectInfo){
                        _featureOps.runCommand(command, projectInfo, args)
                    }
                }else{
                    displayGeneralHelp()
                }
            }
        }else{
            displayGeneralHelp()
        }
    }else{
        console.log(chalk.red('awsmobile cli is corrupted, please re-instal it'))
    }
}
 
function enable(){
    if( _args.length > 4){
        if(_args[4] == '-p' || _args[4] == '--prompt'){
            let projectInfo = projectInfoManager.getProjectInfo()
            Eif(projectInfo){
                backendSpecManager.enableFeature(projectInfo, _featureName, true)
            }
        }else{
            displayHelp('enable')
            console.log('   ' + '   -p, --prompt  prompt to configure the enabled feature')
        }
    }else{
        let projectInfo = projectInfoManager.getProjectInfo()
        Eif(projectInfo){
            backendSpecManager.enableFeature(projectInfo, _featureName, false)
        }
    }
}
 
function disable(){
    if( _args.length > 4){
        displayHelp('disable')
    }else{
        let projectInfo = projectInfoManager.getProjectInfo()
        Eif(projectInfo){
            backendSpecManager.disableFeature(projectInfo, _featureName)
        }
    }
}
 
function configure(){
    if( _args.length > 4){
        displayHelp('configure')
    }else{
        let projectInfo = projectInfoManager.getProjectInfo()
        Eif(projectInfo){
            backendSpecManager.configureFeature(projectInfo, _featureName)
        }
    }
}
 
function displayGeneralHelp(){
    console.log('  Usage: awsmobile {featureName} <command> [options]'.replace('{featureName}', _featureName))
    console.log()
    console.log('  contains subcommands of the awsmobile feature ' + _featureName)
    console.log()
    console.log('  Options:'  )
    console.log()
    console.log('   ' + '   -h, --help  output usage information')
    console.log()
    console.log('  Commands:'  )
    console.log()
    _.keys(_commonCommands).forEach(function(command) {
        console.log('   ' + (command + '               ').slice(0,12) + _commonCommands[command].replace('{featureName}', _featureName))
    }, this)
    _.keys(_featureOps.featureCommands).forEach(function(command) {
        console.log('   ' + (command + '               ').slice(0,12) + _featureOps.featureCommands[command])
    }, this)
}
 
function displayHelp(subcommand){
    Eif(_commonCommands.hasOwnProperty(subcommand)){
        console.log('   Usage: awsmobile {featureName} {subcommand} [options]'.replace('{featureName}', _featureName).replace('{subcommand}', subcommand))
        console.log()
        console.log('   ' + _commonCommands[subcommand].replace('{featureName}', _featureName))
        console.log()
        console.log('   Options:'  )
        console.log()
        console.log('   ' + '   -h, --help  output usage information')
    }
}
 
function list(){
    console.log()
    console.log(chalk.gray('    #awsmobile features'))
    for(let i = 0; i < mobileFeatures.length; i++){
        console.log('      ' + mobileFeatures[i])
    }
}
 
module.exports = {
    run, 
    list
}