diff --git a/lib/defaults/actions/ProjectCreate.js b/lib/defaults/actions/ProjectCreate.js index 1b22f26e1..60dc563e3 100644 --- a/lib/defaults/actions/ProjectCreate.js +++ b/lib/defaults/actions/ProjectCreate.js @@ -5,15 +5,15 @@ */ const JawsPlugin = require('../../JawsPlugin'), - JawsError = require('../../jaws-error'), - JawsCLI = require('../../utils/cli'), - Promise = require('bluebird'), - fs = require('fs'), - path = require('path'), - os = require('os'), - AWSUtils = require('../../utils/aws'), - utils = require('../../utils'), - shortid = require('shortid'); + JawsError = require('../../jaws-error'), + JawsCLI = require('../../utils/cli'), + Promise = require('bluebird'), + fs = require('fs'), + path = require('path'), + os = require('os'), + AWSUtils = require('../../utils/aws'), + utils = require('../../utils'), + shortid = require('shortid'); /** * ProjectCreate Class @@ -48,6 +48,20 @@ class ProjectCreate extends JawsPlugin { return function*(config) { + // Validate if non-interactive + if (!_this.Jaws._interactive) { + if (!config || + !config.name || + !config.stage || + !config.region || + !config.domain || + !config.notificationEmail || + !config.awsAdminKeyId || + !config.awsAdminSecretKey) { + throw new JawsError('Missing required properties'); + } + } + // Defaults config = config ? config : {}; _this._name = config.name ? config.name : null; @@ -72,7 +86,7 @@ class ProjectCreate extends JawsPlugin { if (_this.Jaws._interactive) yield _this._prompt(); // Prepare project data - //yield _this.prepareProjectData(); + yield _this._prepareProjectData(); } } @@ -98,9 +112,11 @@ class ProjectCreate extends JawsPlugin { // Prompt: name (project name) _this.Prompter.override.name = _this._name; + // Set default name + _this._name = 'jaws-' + _this._generateShortId(19); _this._prompts.properties.name = { description: nameDescription.yellow, - default: 'jaws-' + _this._generateShortId(19), + default: _this._name, message: 'Name must be only letters, numbers or dashes', conform: function(name) { let re = /^[a-zA-Z0-9-_]+$/; @@ -254,6 +270,57 @@ class ProjectCreate extends JawsPlugin { }); } + /** + * Prepare Project Data + */ + _prepareProjectData() { + + let _this = this; + + // Validate: Ensure stage isn't "local" + if (_this._stage.toLowerCase() == 'local') { + throw new JawsError('Stage ' + _this._stage + ' is reserved'); + } + + // Validate: AWS only allows Alphanumeric and - in name + let nameOk = /^([a-zA-Z0-9-]+)$/.exec(_this._name); + if (!nameOk) { + throw new JawsError('Project names can only be alphanumeric and -'); + } + + // Append unique id if name is in use + if (utils.dirExistsSync(path.join(process.cwd(), _this._name))) { + _this._name = _this._name + '-' + generateShortId(19); + } + + // Append unique id if domain is default + if (_this._domain === 'myapp.com') { + _this._domain = 'myapp-' + generateShortId(8) + '.com'; + } + + // Set JAWS Bucket + _this._jawsBucket = utils.generateJawsBucketName(_this._stage, _this._region, _this._domain); + + // Validate: If no profile, ensure access keys, create profile + if (!_this._profile) { + + if (!_this._awsAdminKeyId) { + throw new JawsError( + 'An AWS Access Key ID is required', + JawsError.errorCodes.MISSING_AWS_CREDS); + } + + if (!_this._awsAdminSecretKey) { + throw new JawsError( + 'An AWS Secret Key is required', + JawsError.errorCodes.MISSING_AWS_CREDS); + } + + // Set profile + AWSUtils.profilesSet('default', _this._region, _this._awsAdminKeyId, _this._awsAdminSecretKey); + _this._profile = 'default'; + } + } }