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

95.12% Statements 39/41
100% Branches 6/6
77.78% Functions 7/9
95% Lines 38/40
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 821x 1x 1x 1x 1x     1x   6x   6x           6x 6x   6x 3x   6x 6x 6x       1x   6x 6x 3x     6x             6x 6x 6x       1x   6x 4x   2x 2x   6x 6x 6x   6x     1x 6x 6x 6x               1x       1x    
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 = {
      'user-files': {
        enabled: false
      }
    };
 
    let backendProjectYamlFilePath = pathManager.getBackendSpecProjectYmlFilePath(_projectInfo.ProjectPath);
    let yamlDefinition = yamlOps.readYamlFileSync(backendProjectYamlFilePath);
    
      if (yamlDefinition['features']['user-files']) {
        currentDefinition['user-files']['enabled'] = true;
      }
      currentDefinition.yamlDefinition = yamlDefinition;
      currentDefinition.projectInfo = _projectInfo;
      resolve(currentDefinition);
    
  });
 
var askEnableUserFiles = (currentDefinition) => {
 
  let defaultMessage = 'This feature is for storing user files in the cloud, would you like to enable it?';
  if (currentDefinition['user-files'].enabled) {
    defaultMessage = 'User files storage is enabled, do you want to keep it enabled?'
  }
  
  var enableQuestion = {
    type: 'confirm',
    name: 'enableUserData',
    message: defaultMessage,
    default: true
  }
 
  return inquirer.prompt(enableQuestion).then(answers => {
    currentDefinition['user-files']['enabled'] = answers.enableUserData;
    return currentDefinition;
  });
}
 
var saveSettings = (currentDefinition) => {
 
  if (currentDefinition['user-files']['enabled']) {
    currentDefinition.yamlDefinition['features']['user-files'] = new yamlSchema.dressObject({attributes: {enabled: true, 'wildcard-cors-policy': true}, 'backend-class': 'UserFiles'});
  } else {
    delete currentDefinition.yamlDefinition['features']['user-files'];
    delete currentDefinition.yamlDefinition['features']['user-profiles'];
  }
  let yaml = currentDefinition.yamlDefinition;
  let backendProjectYamlFilePath = pathManager.getBackendSpecProjectYmlFilePath(_projectInfo.ProjectPath);
  yamlOps.writeYamlFileSync(yaml, backendProjectYamlFilePath);
 
  return Promise.resolve(currentDefinition);
}
 
exports.specify = (projectInfo) => {
  _projectInfo = JSON.parse(JSON.stringify(projectInfo)); // clone projectInfo
  return new Promise(function(resolve,reject) { 
      startQuestionary()
      .then(askEnableUserFiles)
      .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
}