All files / lib/feature-operations/scripts hosting-ops.js

92.5% Statements 37/40
100% Branches 6/6
70% Functions 7/10
92.31% Lines 36/39
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    1x 1x 1x 1x 1x     1x   6x   6x           6x 6x   6x 3x   6x 6x         1x   6x 6x 3x     6x             6x 6x 6x       1x   6x 2x   4x                       6x 6x 6x   6x                   1x 6x 6x 6x               1x       1x    
"use strict";
 
var inquirer = require('inquirer');
var fs = require('fs');
var yamlSchema = require('../../aws-operations/mobile-yaml-schema');
var yamlOps = require('../../aws-operations/mobile-yaml-ops');
const pathManager = require('../../utils/awsmobilejs-path-manager.js')
 
// save project info
var _projectInfo = {};
 
var startQuestionary = () => new Promise(
  function(resolve, reject) {
    var currentDefinition = {
      'content-delivery': {
        enabled: false
      }
    };
 
    let backendProjectYamlFilePath = pathManager.getBackendSpecProjectYmlFilePath(_projectInfo.ProjectPath);
    let yamlDefinition = yamlOps.readYamlFileSync(backendProjectYamlFilePath);
 
    if (yamlDefinition['features']['content-delivery']) {
      currentDefinition['content-delivery']['enabled'] = true;
    }
    currentDefinition.yamlDefinition = yamlDefinition;
    resolve(currentDefinition);
      
  }
);
 
var askEnableHosting = (currentDefinition) => {
 
  let defaultMessage = 'Currently Hosting is disabled, do you want to host your web app including a global CDN?';
  if (currentDefinition['content-delivery'].enabled) {
    defaultMessage = 'Currently Hosting is enabled, do you want to keep it enabled?'
  }
 
  var enableQuestion = {
    type: 'confirm',
    name: 'enable',
    message: defaultMessage,
    default: true
  }
 
  return inquirer.prompt(enableQuestion).then(answers => {
    currentDefinition['content-delivery']['enabled'] = answers.enable;
    return currentDefinition;
  });
}
 
var saveSettings = (currentDefinition) => {
 
  if (!currentDefinition['content-delivery']['enabled']) {
    delete currentDefinition.yamlDefinition['features']['content-delivery'];
  } else {
    currentDefinition.yamlDefinition['features']['content-delivery'] =
      new yamlSchema.dressObject({
        attributes: {
          enabled: true,
          visibility: 'public-global'
        },
        'backend-class': 'ContentDelivery',
        components: {
          release: new yamlSchema.dressObject({'backend-class': 'Bucket' })
        }
      });
  }
  let yaml = currentDefinition.yamlDefinition;
  let backendProjectYamlFilePath = pathManager.getBackendSpecProjectYmlFilePath(_projectInfo.ProjectPath);
  yamlOps.writeYamlFileSync(yaml, backendProjectYamlFilePath);
 
  return Promise.resolve(currentDefinition);
}
 
function main() {
  startQuestionary()
  .then(askEnableHosting)
  .then(saveSettings)
  .catch(console.log.bind(null, 'ERROR'));
}
 
exports.specify = function(projectInfo) {
  _projectInfo = JSON.parse(JSON.stringify(projectInfo)); // clone projectInfo
  return new Promise(function(resolve,reject) {
    startQuestionary()
    .then(askEnableHosting)
    .then(saveSettings)
    .then(resolve)
    .catch(reject);
  });
}
 
exports.onFeatureTurnOn = function(projectInfo, cloudProjectSpec){
  _projectInfo = JSON.parse(JSON.stringify(projectInfo)); // clone projectInfo
}
 
exports.onFeatureTurnOff = function(projectInfo, cloudProjectSpec){
  _projectInfo = JSON.parse(JSON.stringify(projectInfo)); // clone projectInfo
}