All files / lib project-info-manager.js

72.48% Statements 79/109
37.84% Branches 14/37
78.57% Functions 11/14
72.48% Lines 79/109
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                          19x 19x 19x 19x 19x 19x 19x   19x 19x 19x 19x 19x 19x       1x 1x   1x 1x 1x   1x 1x 1x   1x   1x   1x       1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x               3x 3x 3x 3x 3x 3x   3x 3x 3x                           3x       3x                                     4x 4x 4x 4x   4x 4x 4x 4x   4x               4x 4x 4x   4x 4x 4x 4x       1x 1x 5x             1x 1x       1x 5x   1x                               7x   7x 7x 7x 7x                     7x     19x                                     19x               19x                    
/* 
 * 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 path = require('path')
const chalk = require('chalk')
const inquirer = require('inquirer')
const fs = require('fs-extra')
const moment = require('moment')
const util = require('util')
const _ = require('lodash')
 
const projectValidator = require('./project-validator.js')
const projectConfigManager = require('./project-config-manager.js')
const pathManager = require('./utils/awsmobilejs-path-manager.js')
const backendFormats = require('./backend-operations/backend-formats.js')
const versionManager = require('./utils/version-manager.js')
const awsmobileJSConstant = require('./utils/awsmobilejs-constant.js')
 
function initialize(projectPath, initialConfig){
    let projectInfo
    Eif(projectPath){
        projectInfo = projectInfoTemplate
       
        projectInfo.ProjectName = path.basename(projectPath)
        projectInfo.ProjectPath = projectPath 
        projectInfo.InitializationTime = moment().format(awsmobileJSConstant.DateTimeFormatString)  
 
        const jsonString = JSON.stringify(projectInfo, null, '\t')
        const projectInfoFilePath = pathManager.getProjectInfoFilePath(projectPath)
        fs.writeFileSync(projectInfoFilePath, jsonString, 'utf8')
 
        let projectConfig = projectConfigManager.initialize(projectPath, initialConfig)
       
        Object.assign(projectInfo, projectConfig)
    }
    return projectInfo
}
 
function configureProject(callback){
    let projectInfo = getProjectInfo()
    Eif(projectInfo){
        projectConfigManager.configureProject(projectInfo, (isConfigChanged, projectConfig_0, projectConfig)=>{
            Eif(isConfigChanged){
                projectInfo.LastConfigurationTime = moment().format(awsmobileJSConstant.DateTimeFormatString)
                setProjectInfo(projectInfo)
            }
    
            Eif(callback){
                let projectInfo_1 = getProjectInfo()
                Object.assign(projectInfo, projectConfig_0)
                Object.assign(projectInfo_1, projectConfig)
                callback(projectInfo, projectInfo_1)
            }
        })
    }
}
 
function getProjectInfo(noWarningFlag) { // will do a search up folder tree and find the projectpath first
    let projectInfo
    let projectPath = searchProjectRootPath()
    Eif(projectPath){
        try{
            let projectInfoFilePath = pathManager.getProjectInfoFilePath(projectPath)
            projectInfo = JSON.parse(fs.readFileSync(projectInfoFilePath, 'utf8'))
            projectInfo.ProjectPath = projectPath
 
            let projectConfig = projectConfigManager.getProjectConfig(projectInfo)
            Eif(projectConfig){
                Object.assign(projectInfo, projectConfig)
            }else{
                if(!noWarningFlag){
                    console.log(chalk.red('Your project is not properly configured'))
                    console.log(chalk.gray('    # configure your project'))
                    console.log('    $ awsmobile configure project')
                }
                projectInfo = undefined
            }
        }catch(e){
            projectInfo = undefined
        }
    }
    
    Iif(!projectInfo && !noWarningFlag){
        console.log(chalk.red('you are not working inside a valid awsmobilejs project'))
    }
 
    return projectInfo
}
 
function listProjectConfig(){
    let projectInfo = getProjectInfo()
    if(projectInfo){
        let projectConfig = {}
        projectConfig.SourceDir = projectInfo.SourceDir
        projectConfig.DistributionDir = projectInfo.DistributionDir
        projectConfig.BuildCommand = projectInfo.BuildCommand
        projectConfig.StartCommand = projectInfo.StartCommand
       
        console.log()
        console.log(util.inspect(projectConfig, false, null))
        console.log()
    }
}
 
function setProjectInfo(projectInfo){
    let projectPath = searchProjectRootPath()
    Eif(projectPath){
        let projectInfoFilePath = pathManager.getProjectInfoFilePath(projectPath)
        projectInfo.ProjectPath = projectPath
 
        let projectConfig = projectConfigManager.extractProjectConfig(projectInfo)
        trimProjectInfo(projectInfo)
        let jsonString = JSON.stringify(projectInfo, null, '\t')
        fs.writeFileSync(projectInfoFilePath, jsonString, 'utf8')
        
        Object.assign(projectInfo, projectConfig)
    }else{
        console.log(chalk.red('you are not working inside a valid awsmobilejs project'))
    }
}
 
function trimProjectInfo(projectInfo){
    //remove deprecated properties to avoid confusion
    delete projectInfo.BackendLastUpdateTime
    delete projectInfo.BackendLastUpdateSuccessful
    delete projectInfo.BackendFormat
    //remove project configuration
    delete projectInfo.SourceDir
    delete projectInfo.DistributionDir
    delete projectInfo.BuildCommand
    delete projectInfo.StartCommand
}
 
function updateBackendProjectDetails(projectInfo, backendProjectDetails){
    Eif(backendProjectDetails && backendProjectDetails.projectId && backendProjectDetails.projectId.length > 0){
        _.keys(backendPropertyMappings).forEach(function(propertyName){
            projectInfo[propertyName] = backendProjectDetails[backendPropertyMappings[propertyName]]
        })
    }else{
        _.keys(backendPropertyMappings).forEach(function(propertyName){
            projectInfo[propertyName] = ''
        })
    }
    setProjectInfo(projectInfo)
    return projectInfo
}
 
function onClearBackend(projectInfo){
    _.keys(backendPropertyMappings).forEach(function(propertyName){
        projectInfo[propertyName] = ''
    })
    setProjectInfo(projectInfo)
}
 
function checkBackendUpdateNoConflict(projectInfo, backendDetails){
    let result = false
    if(projectInfo && projectInfo.BackendProjectLastUpdatedTime && backendDetails && backendDetails.lastUpdatedDate){
        let localStamp = moment(projectInfo.BackendProjectLastUpdatedTime)
        let cloudStamp = moment(backendDetails.lastUpdatedDate)
        result = !localStamp.isBefore(cloudStamp)
    }
    return result
}
 
function searchProjectRootPath()
{
    let result
    let currentPath = process.cwd()
 
    do{
        Eif(projectValidator.validate(currentPath)){
            result = currentPath 
            break 
        }else{
            let parentPath = path.dirname(currentPath) 
            if(currentPath == parentPath){
                break
            }else{
                currentPath = parentPath
            }
        }
    }while(true)
 
    return result
}
 
const projectInfoTemplate = {
	"ProjectName": "",
	"ProjectPath": "",
	"InitializationTime": "",
	"LastConfigurationTime": "",
	"LastNPMInstallTime": "",
	"FrontendLastBuildTime": "",
	"LastPublishTime": "",
	"BackendLastSyncTime": "",
	"BackendLastBuildTime": "",
	"BackendLastPushTime": "",
	"BackendLastPushSuccessful": false,
	"BackendProjectID": "",
	"BackendProjectName":  "",
    "BackendProjectConsoleUrl": "",
	"BackendProjectCreationTime": "",
	"BackendProjectLastUpdatedTime": ""
}
 
const backendPropertyMappings = {
    "BackendProjectName":  "name",
    "BackendProjectID": "projectId",
    "BackendProjectCreationTime": "createdDate",
    "BackendProjectLastUpdatedTime": "lastUpdatedDate",
    "BackendProjectConsoleUrl": "consoleUrl"
}
 
module.exports = {
    initialize,
    configureProject,
    getProjectInfo,
    listProjectConfig,
    setProjectInfo,
    updateBackendProjectDetails,
    onClearBackend,
    checkBackendUpdateNoConflict
}