All files / lib/backend-operations/appsync-operations appsync-manager.js

91.3% Statements 126/138
64.1% Branches 25/39
88.46% Functions 23/26
91.97% Lines 126/137

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                          29x 29x 29x 29x 29x   29x   29x 29x 29x 29x     1x 1x 1x     1x 1x 1x         2x 2x 2x   2x 2x           1x 1x 1x       3x 3x 3x       4x 1x   4x 4x       1x 1x 1x   1x 1x 1x       5x 5x 5x 1x   5x       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         2x   2x 2x 2x   2x 2x 2x 10x 10x 2x 8x 2x 2x 2x   6x 6x 6x 6x 6x         2x       1x   1x           1x       1x 1x 3x 3x   1x   1x     29x                             29x                                            
/* 
 * 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 os = require('os')
const path = require('path')
const moment = require('moment')
const lineByLine = require('n-readlines')
 
const _featureName = 'appsync'
 
const resolversHelper = require('./helpers/helper-resolvers.js')
const pathManager = require('../../utils/awsmobilejs-path-manager.js')
const awsmobilejsConstant = require('../../utils/awsmobilejs-constant.js')
const dfOps = require('../../utils/directory-file-ops.js')
 
function enable(projectPath){
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectPath, _featureName)
  Eif(fs.existsSync(featureDirPath)){
    fs.removeSync(featureDirPath)
  }
 
  let templateDirPath = pathManager.getAppSyncTemplateDirPath()
  fs.copySync(templateDirPath, featureDirPath, {filter: (path)=>{return path.indexOf(awsmobilejsConstant.AppSyncConfigurablesDirName)<0 }})
  markAppsyncInfoLocalFlag(projectPath, 'enable')
}
 
 
function markAppsyncInfoLocalFlag(projectPath, freshLocalEnableDisableFlag){
  let appsyncInfo = getAppSyncInfo(projectPath)
  Eif(!appsyncInfo){
    appsyncInfo = {}
  }
  appsyncInfo.freshLocalEnableDisableFlag = freshLocalEnableDisableFlag
  setAppSyncInfo(projectPath, appsyncInfo)
}
 
function disable(projectPath){
  //should not completely wipe out the appsync information, the user can change his mind and do a pull
  //and sync to dev, 
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectPath, _featureName)
  markAppsyncInfoLocalFlag(projectPath, 'disable')
  fs.removeSync(featureDirPath)
}
 
function getAppSyncInfo(projectPath){
  let appsyncInfoFilePath = pathManager.getAppSyncInfoFilePath(projectPath)
  let appsyncInfo = dfOps.readJsonFile(appsyncInfoFilePath)
  return appsyncInfo
}
 
function setAppSyncInfo(projectPath, appsyncInfo){
  if(appsyncInfo && appsyncInfo.apiId && appsyncInfo.region){
    appsyncInfo.AppSyncConsoleUrl = awsmobilejsConstant.AppSyncConsoleUrl.replace('{apiId}', appsyncInfo.apiId).replace('{region}', appsyncInfo.region)
  }
  let appsyncInfoFilePath = pathManager.getAppSyncInfoFilePath(projectPath)
  return dfOps.writeJsonFile(appsyncInfoFilePath, appsyncInfo)
}
 
function clearAppSyncInfo(projectPath){
  let appsyncInfo = {}
  let currentFeatureDirPath = pathManager.getCurrentBackendFeatureDirPath(projectPath, _featureName)
  let appsyncJSFilePath = pathManager.getAppSyncJSFilePath(projectPath)
  
  setAppSyncInfo(projectPath, appsyncInfo)
  fs.removeSync(currentFeatureDirPath)
  fs.removeSync(appsyncJSFilePath)
}
 
function getEnabledFeatures(projectPath){
  let result = []
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectPath, _featureName)
  if(fs.existsSync(featureDirPath)){
    result.push(_featureName)
  }
  return result
}
 
function getMapping(projectPath, name){
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectPath, _featureName)
  let resolverMappingsDirPath = path.join(featureDirPath, awsmobilejsConstant.AppSyncResolverMappingsDirName)
  let filePath = path.join(resolverMappingsDirPath, name)
  Eif(fs.existsSync(filePath)){
    return fs.readFileSync(filePath).toString()
  }else{
    return undefined
  }
}
 
function getCurrentMapping(projectPath, name){
  let featureDirPath = pathManager.getCurrentBackendFeatureDirPath(projectPath, _featureName)
  let resolverMappingsDirPath = path.join(featureDirPath, awsmobilejsConstant.AppSyncResolverMappingsDirName)
  let filePath = path.join(resolverMappingsDirPath, name)
  Eif(fs.existsSync(filePath)){
    return fs.readFileSync(filePath).toString()
  }else{
    return undefined
  }
}
 
function getApiKeys(projectPath){
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncApiKeysFileName)
  return dfOps.readJsonFile(filePath)
}
 
function setApiKeys(projectPath, apiKeys){
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncApiKeysFileName)
  return dfOps.writeJsonFile(filePath, apiKeys)
}
 
function getCurrentApiKeys(projectPath){
  let featureDirPath = pathManager.getCurrentBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncApiKeysFileName)
  return dfOps.readJsonFile(filePath)
}
 
function getDataSources(projectPath){
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncDataSourcesFileName)
  return dfOps.readJsonFile(filePath)
}
 
function getCurrentDataSources(projectPath){
  let featureDirPath = pathManager.getCurrentBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncDataSourcesFileName)
  return dfOps.readJsonFile(filePath)
}
 
function getGraphqlApi(projectPath){
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncGraphqlApiFileName)
  return dfOps.readJsonFile(filePath)
}
 
function getCurrentGraphqlApi(projectPath){
  let featureDirPath = pathManager.getCurrentBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncGraphqlApiFileName)
  return dfOps.readJsonFile(filePath)
}
 
function getResolvers(projectPath){
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncResolversFileName)
  Eif(fs.existsSync(filePath)){
    let resolvers = dfOps.readJsonFile(filePath)
    Iif(resolvers){
      resolvers.forEach(resolver=>{
        resolversHelper.readResolverMappings(featureDirPath, resolver)
      })
    }
    return resolvers
  }else{
    return undefined
  }
}
 
function getCurrentResolvers(projectPath){
  let featureDirPath = pathManager.getCurrentBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncResolversFileName)
  Eif(fs.existsSync(filePath)){
    let resolvers = dfOps.readJsonFile(filePath)
    Iif(resolvers){
      resolvers.forEach(resolver=>{
        resolversHelper.readResolverMappings(featureDirPath, resolver)
      })
    }
    return resolvers
  }else{
    return undefined
  }
}
 
function getSchema(projectPath){
  let result = {}
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncSchemaFileName)
  Iif(fs.existsSync(filePath)){
    result.definition = fs.readFileSync(filePath).toString()
  }
  return result
}
 
function getCurrentSchema(projectPath){
  let result = {}
  let featureDirPath = pathManager.getCurrentBackendFeatureDirPath(projectPath, _featureName)
  let filePath = path.join(featureDirPath, awsmobilejsConstant.AppSyncSchemaFileName)
  Iif(fs.existsSync(filePath)){
    result.definition = fs.readFileSync(filePath).toString()
  }
  return result
}
 
function getAppSyncJS(projectPath){
  let result
  let appsyncJSFilePath = pathManager.getAppSyncJSFilePath(projectPath)
 
  Eif(fs.existsSync(appsyncJSFilePath)){
    let content = fs.readFileSync(appsyncJSFilePath)
    let lines = content.toString().split(os.EOL)
  
    let inObject = false
    let temp = {}
    for(let i = 0; i<lines.length; i++){
      let line = lines[i]
      if(/{$/.test(line)){
        inObject = true
      }else if(/}$/.test(line)){
        Eif(inObject){
          result = temp
          break
        }
      }else Eif(inObject){
        let index = line.indexOf(":")
        let key = line.slice(0, index).trim().replace(/,$/, '').replace(/^"/,'').replace(/"$/,'')
        let value = line.slice(index+1).trim().replace(/,$/, '').replace(/^"/,'').replace(/"$/,'')
        temp[key] = value
      }
    }
  }
 
  return result
}
 
function setAppSyncJS(projectPath, appSyncInfo){
  let appsyncJSFilePath = pathManager.getAppSyncJSFilePath(projectPath)
 
  let appsyncJS ={
    graphqlEndpoint: appSyncInfo.graphqlEndpoint,
    region: appSyncInfo.region,
    authenticationType: appSyncInfo.authenticationType
  }
 
  Iif(appSyncInfo.apiKey){
    appsyncJS.apiKey = appSyncInfo.apiKey
  }
 
  let content = "export default {" + os.EOL
  Object.keys(appsyncJS).forEach(function(key) {
    var val = appsyncJS[key]
    content += '\t"' + key + '": "' + val + '",' + os.EOL
  })
  content += "}"
 
  fs.writeFileSync(appsyncJSFilePath, content.trim())
}
 
const appsyncInfoTemplate = {
  "apiId": "",
  "name": "",
  "graphqlEndpoint": "",
  "region": "us-east-1",
  "authenticationType": "API_KEY",
  "creationTime": "",
  "lastUpdateTime": "",
  "lastSyncTime": "",
  "apiKey": "da2-",
  "lastSyncToDevTime": "",
  "freshLocalEnableFlag": false, 
  "AppSyncConsoleUrl": "",
}
 
module.exports = {
  enable, 
  disable,
  getAppSyncInfo,
  setAppSyncInfo,
  clearAppSyncInfo,
  getEnabledFeatures,
  getMapping,
  getApiKeys,
  setApiKeys,
  getDataSources,
  getGraphqlApi,
  getResolvers,
  getSchema,
  getCurrentMapping,
  getCurrentApiKeys,
  getCurrentDataSources,
  getCurrentGraphqlApi,
  getCurrentResolvers,
  getCurrentSchema,
  getAppSyncJS,
  setAppSyncJS,
}