diff --git a/bin/jaws b/bin/jaws index 27a00459a..18de14f96 100755 --- a/bin/jaws +++ b/bin/jaws @@ -8,7 +8,7 @@ var JawsError = require('../lib/jaws-error'), utils = require('../lib/utils'), Promise = require('bluebird'), minimist = require('minimist'), - handleExit = utils.handleExit; + execute = utils.execute; var JAWS = new Jaws(); @@ -34,7 +34,7 @@ program // New Project var CmdNewProject = require('../lib/commands/new_project'); - handleExit(CmdNewProject.run( + execute(CmdNewProject.run( args.name, args.stage ? args.stage.toLowerCase() : null, args.s3Bucket, @@ -48,7 +48,7 @@ program // New Region/Stage var CmdNewStageRegion = require('../lib/commands/new_stage_region'); - handleExit(CmdNewStageRegion.run( + execute(CmdNewStageRegion.run( JAWS, type, args.stage, @@ -56,44 +56,44 @@ program args.noCf )); + } else if (type === 'action') { + + var action = {}; + + if (args.b || args.both) action.type = 'both'; + else if (args.l || args.lambda) action.type = 'lambda'; + else if (args.e || args.endpoint) action.type = 'endpoint'; + else action.type = 'both'; + + if (args._.length !== 3) { + throw new JawsError('Please specify the resource and action in this command, like: ' + + '"jaws new users create".'); + } + + action.resource = args._[2]; + action.action = args._[3]; + action.runtime = (args.r || args.runtime); + + // New Action + var CmdNewAction = require('../lib/commands/new_action'); + execute(CmdNewAction.run(JAWS, action)); + } else { + + // Unknown Type console.error('Unsupported type ' + type + '. Must be project|region|stage'); process.exit(1); + } }); -program - .command('generate') - .description('Create boilerplate structure for a new lambda or api gateway (or both)') - .option('-l, --lambda', 'will create the files needed for a lambda') - .option('-r, --lambda-runtime', 'only nodejs supported') - .option('-a, --endpoint', 'will create the files needed for an api gateway configuration') - .option('-b, --both', 'shorthand for -l -a') - .option('-f, --function-name', 'lambda functionName. Will ensure this is unique across your project.') - .option('-m, --resource-name', 'parent directory the functionName dir will be created in') - - .action(function(options) { - if (options.lambda || options.both) { - options.lambda = true; - } - - if (options.endpoint || options.both) { - options.endpoint = true; - } - - var theCmd = require('../lib/commands/generate'); - handleExit(theCmd.run( - JAWS, options.lambda, options.endpoint, options.functionName, options.resourceName, options.lambdaRuntime - )); - }); - program .command('install ') .description('Installs a jaws-module from the specified url into the apprpriate area of your JAWS project.') .option('-s, --save', 'Save jaws-module\'s CloudFormation and Swagger Template to your project\'s') .action(function(url, options) { var theCmd = require('../lib/commands/install'); - handleExit(theCmd.install(JAWS, url, options.save)); + execute(theCmd.install(JAWS, url, options.save)); }); program @@ -116,16 +116,16 @@ program var theCmd = require('../lib/commands/tag'); if (options.listAll) { - handleExit(theCmd.listAll(JAWS, type).then(function(relPaths) { + execute(theCmd.listAll(JAWS, type).then(function(relPaths) { console.log(relPaths); })); } else if (options.tagAll || options.untagAll) { var untag = (options.untagAll) ? true : false; - handleExit(theCmd.tagAll(JAWS, type, untag)); + execute(theCmd.tagAll(JAWS, type, untag)); } else if (options.multi) { - handleExit(theCmd.tagMulti(JAWS, type, options.untag)); + execute(theCmd.tagMulti(JAWS, type, options.untag)); } else { //If not tagging all, you have to be tagging whats in your CWD (null 1st param) - handleExit(theCmd.tag(type, null, options.untag)); + execute(theCmd.tag(type, null, options.untag)); } }); @@ -142,13 +142,13 @@ program case 'endpoint': var allTagged = (options.tags) ? true : false; var theCmd = require('../lib/commands/deploy_endpoint'); - handleExit(theCmd.run(JAWS, stage, region, allTagged)); + execute(theCmd.run(JAWS, stage, region, allTagged)); break; case 'lambda': var allTagged = (options.tags) ? true : false, allAtOnce = (options.allAtOnce) ? true : false; var theCmd = require('../lib/commands/deploy_lambda'); - handleExit(theCmd.run(JAWS, stage, region, allTagged, allAtOnce)); + execute(theCmd.run(JAWS, stage, region, allTagged, allAtOnce)); break; default: console.error('Unsupported type ' + type + '. Must be endpoint|lambda|resources'); @@ -167,7 +167,7 @@ program cmd = cmd.toLowerCase(); switch (cmd) { case 'list': - handleExit(theCmd.listEnv(JAWS, stage)); + execute(theCmd.listEnv(JAWS, stage)); break; case 'get': if (!key) { @@ -175,7 +175,7 @@ program process.exit(1); } - handleExit(theCmd.getEnvKey(JAWS, stage, key)); + execute(theCmd.getEnvKey(JAWS, stage, key)); break; case 'set': if (!key || typeof val == 'undefined') { @@ -183,7 +183,7 @@ program process.exit(1); } - handleExit(theCmd.setEnvKey(JAWS, stage, key, val)); + execute(theCmd.setEnvKey(JAWS, stage, key, val)); break; case 'unset': if (!key) { @@ -196,7 +196,7 @@ program process.exit(1); } - handleExit(theCmd.setEnvKey(JAWS, stage, key)); + execute(theCmd.setEnvKey(JAWS, stage, key)); break; default: console.error('Unsupported cmd "' + cmd + '". Must be list|get|set|unset'); @@ -210,7 +210,7 @@ program .description('Check deployment status and deploy resources for a stage and region') .action(function() { var theCmd = require('../lib/commands/dash'); - handleExit(theCmd.run(JAWS)); + execute(theCmd.run(JAWS)); }); if (process.argv.length == 2) { diff --git a/lib/commands/deploy_lambda.js b/lib/commands/deploy_lambda.js index 773052035..51e20941d 100644 --- a/lib/commands/deploy_lambda.js +++ b/lib/commands/deploy_lambda.js @@ -218,7 +218,7 @@ Deployer.prototype.deploy = Promise.method(function() { Deployer.prototype._createOrUpdateLambda = Promise.method(function(packagedLambda) { var _this = this; - var lambdaJawsJson = require(path.join(process.cwd(), packagedLambda.jawsFilePath)); + var lambdaJawsJson = require(packagedLambda.jawsFilePath); var iamRole = utils.getProjRegionConfig( _this._JAWS._meta.projectJson.project.stages[_this._stage], _this._region).iamRoleArnLambda; @@ -307,7 +307,7 @@ function Packager(JAWS, stage, region, lambdaPath) { this._stage = stage; this._region = region; this._srcDir = os.tmpdir(); - this._lambdaJson = require(path.join(process.cwd(), this._lambdaPath)); + this._lambdaJson = require(this._lambdaPath); } /** diff --git a/lib/commands/env.js b/lib/commands/env.js index 9716b9f19..8725bf61a 100644 --- a/lib/commands/env.js +++ b/lib/commands/env.js @@ -1,8 +1,7 @@ 'use strict'; /** - * JAWS Command: tag - * - Tags a lambda function with "deploy:true" + * JAWS Command: env */ var JawsError = require('../jaws-error'), diff --git a/lib/commands/generate.js b/lib/commands/generate.js deleted file mode 100644 index 25bc88fa3..000000000 --- a/lib/commands/generate.js +++ /dev/null @@ -1,244 +0,0 @@ -'use strict'; - -/** - * JAWS Command: generate - * - * Can not safely be run with concurrency because of global _this for simplicity - */ - -// Defaults -var JawsError = require('../jaws-error'), - JawsCLI = require('../utils/jaws-cli') - Promise = require('bluebird'), - fs = require('fs'), - path = require('path'), - utils = require('../utils'), - inquirer = require('bluebird-inquirer'), - wrench = require('wrench'); - -Promise.promisifyAll(fs); - -/** - * - * @param JAWS - * @param lambdaRuntime - * @param resourceName - * @param functionName - * @param isLambda - * @param isApi - * @param handlerPath - * @constructor - */ -function CMD(JAWS, lambdaRuntime, resourceName, functionName, isLambda, isApi) { - this.JAWS = JAWS; - this.lambdaRuntime = lambdaRuntime || 'nodejs'; - this.resourceName = resourceName || ''; - this.functionName = functionName || ''; - this.backDir = path.join(JAWS._meta.projectRootPath, 'back'); - this.lambdaJawsJsonPath = ''; - this.isLambda = (typeof isLambda === 'undefined') ? undefined : isLambda; - this.isApi = (typeof isApi === 'undefined') ? undefined : isApi; -} - -CMD.prototype.constructor = CMD; - -/** - * Get Answers from CLI - * - * @returns {Promise} - * @private - */ -CMD.prototype._getAnswers = Promise.method(function() { - var _this = this, - prompts = []; - - if (typeof _this.isLambda === 'undefined') { - prompts.push({ - type: 'confirm', - name: 'isLambda', - message: 'Create a new lambda function:', - default: true, - }); - } - - if (typeof _this.isApi === 'undefined') { - prompts.push({ - type: 'confirm', - name: 'isApi', - message: 'Create a new api gateway endpoint:', - default: true, - }); - } - if (!_this.resourceName) { - prompts.push({ - type: 'input', - name: 'resourceName', - message: 'Name the resource this is for (e.g., users, images, data):', - default: 'users', - }); - } - - if (!_this.functionName) { - prompts.push({ - type: 'input', - name: 'functionName', - message: 'Name the action for this resource (create, update, delete):', - default: 'create', - }); - } - - if (prompts.length > 0) { - return inquirer.prompt(prompts) - .then(function(answers) { - utils.logIfVerbose('Answers:'); - utils.logIfVerbose(answers); - Object.keys(answers).forEach(function(key) { - _this[key] = answers[key]; - }); - return true; - }); - } else { - return true; - } -}); - -/** - * Prepare skeleton data - * - * @param answers - * @returns {Promise} - * @private - */ -CMD.prototype._prepareData = Promise.method(function() { - var _this = this; - - _this.resourceName = _this.resourceName.toLowerCase().trim() - .replace(/\s/g, '-') - .replace(/[^a-zA-Z-\d:]/g, '') - .substring(0, 19); - - _this.functionName = _this.functionName.toLowerCase().trim() - .replace(/\s/g, '-') - .replace(/[^a-zA-Z-\d:]/g, '') - .substring(0, 19); - - // AWS only allows Alphanumeric and - in name - if (!/^([a-zA-Z0-9-]+)$/.exec(_this.functionName) || !/^([a-zA-Z0-9-]+)$/.exec(_this.resourceName)) { - Promise.reject(new JawsError( - 'AWS only allows names to contain alphanumeric and -', - JawsError.errorCodes.INVALID_RESOURCE_NAME)); - } - - return utils.getAllLambdaNames(_this.JAWS._meta.projectRootPath) - .then(function(lambdaNames) { - if (_this.isLambda) { - if (lambdaNames.indexOf(_this.functionName) !== -1) { - throw new JawsError( - 'You already have a lambda named ' + _this.functionName, - JawsError.errorCodes.INVALID_RESOURCE_NAME); - } - } - - _this.backDir = path.join(_this.JAWS._meta.projectRootPath, 'back'); - _this.lambdaJawsJsonPath = path.join( - _this.backDir, - 'lambdas', - _this.resourceName, - _this.functionName, - 'jaws.json' - ); - }); -}); - -/** - * Create skeleton - * - * @private - */ -CMD.prototype._createSkeleton = Promise.method(function() { - var _this = this, - templatesDir = path.join(__dirname, '..', 'templates'), - jawsJson = JSON.parse(fs.readFileSync(path.join(templatesDir, 'jaws.json'))), - writeFilesDeferred = []; - - if (!_this.isApi) { - delete jawsJson.endpoint; - } - - if (!_this.isLambda) { - delete jawsJson.lambda; - } else { - var handlerPathBaseDir = path.join('lambdas', _this.resourceName, _this.functionName); - - jawsJson.lambda.functionName = _this.functionName; - jawsJson.lambda.runtime = _this.lambdaRuntime; - - switch (_this.lambdaRuntime) { - case 'nodejs': - var nodeJsTemplateDir = path.join(templatesDir, 'nodejs'), - handlerJs = fs.readFileSync(path.join(nodeJsTemplateDir, 'handler.js')), - packageJson = JSON.parse(fs.readFileSync(path.join(nodeJsTemplateDir, 'package.json'))), - targetPackageJsonPath = path.join(_this.backDir, 'package.json'), - fullLambdaDir = path.join(_this.backDir, handlerPathBaseDir); - - jawsJson.lambda.handler = path.join(handlerPathBaseDir, 'index.handler'); - jawsJson.lambda.runtimeVer = '0.10.36'; - - utils.logIfVerbose('creating ' + path.join(fullLambdaDir, 'index.js')); - utils.logIfVerbose('creating ' + path.join(fullLambdaDir, 'event.json')); - - writeFilesDeferred.push( - utils.writeFile(path.join(fullLambdaDir, 'index.js'), handlerJs), - utils.writeFile(path.join(fullLambdaDir, 'event.json'), '{}') - ); - - // Make package.json, if it doesn't exist already - if (!fs.existsSync(targetPackageJsonPath)) { - utils.logIfVerbose('creating package.json since it does not exist'); - writeFilesDeferred.push( - utils.writeFile(targetPackageJsonPath, JSON.stringify(packageJson, null, 2)) - ); - } - - //Copy over dotenv if it doesn't already exist - var backNodeModulesDir = path.join(_this.backDir, 'node_modules'), - targetDotenv = path.join(backNodeModulesDir, 'dotenv'); - if (!fs.existsSync(targetDotenv)) { - if (!fs.existsSync(backNodeModulesDir)) { - fs.mkdirSync(backNodeModulesDir); - } - wrench.copyDirSyncRecursive(path.resolve(__dirname, '..', '..', 'node_modules', 'dotenv'), path.join(backNodeModulesDir, 'dotenv')); - } - break; - default: - throw new JawsError( - 'Unsupported runtime ' + _this.lambdaRuntime, - JawsError.errorCodes.UNKNOWN); - break; - } - } - - writeFilesDeferred.push(utils.writeFile(_this.lambdaJawsJsonPath, JSON.stringify(jawsJson, null, 2))); - - return Promise.all(writeFilesDeferred); -}); - -/** - * Run - */ -CMD.prototype.run = Promise.method(function() { - var _this = this; - - return _this._getAnswers() - .bind(_this) - .then(_this._prepareData) - .then(_this._createSkeleton) - .then(function() { - console.log('Successfully created new folders and files in your "back/lambdas" folder.'); - }); -}); - -module.exports.run = function(JAWS, isLambda, isApi, functionName, resourceName, lambdaRuntime) { - var command = new CMD(JAWS, lambdaRuntime, resourceName, functionName, isLambda, isApi); - return command.run(); -}; diff --git a/lib/commands/new_action.js b/lib/commands/new_action.js new file mode 100644 index 000000000..74b3220fd --- /dev/null +++ b/lib/commands/new_action.js @@ -0,0 +1,162 @@ +'use strict'; + +/** + * JAWS Command: New Action (Lambda/Endpoint) + */ + +// Defaults +var JawsError = require('../jaws-error'), + JawsCLI = require('../utils/cli'), + Promise = require('bluebird'), + fs = require('fs'), + path = require('path'), + prompt = require('prompt'), + utils = require('../utils'), + wrench = require('wrench'); + +Promise.promisifyAll(fs); + +/** + * Run + */ + +module.exports.run = function(JAWS, action) { + var command = new CMD(JAWS, action); + return command.run(); +}; + +/** + * CMD Class + */ + +function CMD(JAWS, action) { + this._JAWS = JAWS; + this._action = action; + this._prompts = { + properties: {}, + }; + this.Prompter = JawsCLI.prompt(); + this.Prompter.override = {}; + + // Defaults + action.runtime = action.runtime || 'nodejs'; +} + +CMD.prototype.constructor = CMD; + +/** + * CMD: Run + */ + +CMD.prototype.run = Promise.method(function() { + + var _this = this; + + return Promise.try(function() {}) + .bind(_this) + .then(_this._sanitizeData) + .then(_this._createSkeleton); +}); + +/** + * CMD: Sanitize Data + */ + +CMD.prototype._sanitizeData = Promise.method(function() { + + var _this = this; + + _this._action.resource = _this._action.resource.toLowerCase().trim() + .replace(/\s/g, '-') + .replace(/[^a-zA-Z-\d:]/g, '') + .substring(0, 19); + + _this._action.action = _this._action.action.toLowerCase().trim() + .replace(/\s/g, '-') + .replace(/[^a-zA-Z-\d:]/g, '') + .substring(0, 19); + +}); + +/** + * CMD: Create skeleton + */ + +CMD.prototype._createSkeleton = Promise.method(function() { + + var _this = this; + var writeFilesDeferred = []; + + // Fetch skeleton resources + var templatesPath = path.join(__dirname, '..', 'templates'); + var actionJson = JSON.parse(fs.readFileSync(path.join(templatesPath, 'jaws.json'))); + var actionPath = path.join( + _this._JAWS._meta.projectRootPath, + 'back', + 'lambdas', + _this._action.resource, + _this._action.action); + + // Make resource/action folders, if don't exist + if (!fs.existsSync(actionPath)) { + writeFilesDeferred.push(actionPath); + } + + // Edit jaws.json + actionJson.name = _this._action.resource + '-' + _this._action.action; + actionJson.lambda.functionName = actionJson.name; + + // Create files for lambda actions + switch (_this._action.runtime) { + case 'nodejs': + + // Edit jaws.json + actionJson.lambda.runtimeVer = '0.10.36'; + actionJson.lambda.handler = path.join(actionPath, 'index.handler'); + + // Create index.js, event.json + var handlerJs = fs.readFileSync(path.join(templatesPath, 'nodejs', 'handler.js')); + writeFilesDeferred.push( + utils.writeFile(path.join(actionPath, 'index.js'), handlerJs), + utils.writeFile(path.join(actionPath, 'event.json'), '{}') + ); + + // Make package.json, if it doesn't exist already + var packageJsonPath = path.join(_this._JAWS._meta.projectRootPath, 'back', 'package.json'); + var packageJson = JSON.parse(fs.readFileSync(path.join(templatesPath, 'nodejs', 'package.json'))); + if (!fs.existsSync(packageJsonPath)) { + utils.logIfVerbose('creating package.json since it does not exist'); + writeFilesDeferred.push( + utils.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)) + ); + } + + // Copy over dotenv if it doesn't already exist + var nodeModulesPath = path.join(_this._JAWS._meta.projectRootPath, 'back', 'node_modules'); + if (!fs.existsSync(path.join(nodeModulesPath, 'dotenv'))) { + if (!fs.existsSync(nodeModulesPath)) { + fs.mkdirSync(nodeModulesPath); + } + wrench.copyDirSyncRecursive(path.resolve(__dirname, '..', '..', 'node_modules', 'dotenv'), + path.join(nodeModulesPath, 'dotenv')); + } + + break; + default: + throw new JawsError('This runtime is not supported "' + _this._action.runtime + '"'); + break; + } + + // Trim unnecessary JSON + if (_this._action.type === 'lambda') { + delete actionJson.endpoint; + } + + if (_this._action.type === 'endpoint') { + delete actionJson.lambda; + } + + // Write Files + writeFilesDeferred.push(utils.writeFile(path.join(actionPath, 'jaws.json'), JSON.stringify(actionJson, null, 2))); + return Promise.all(writeFilesDeferred); +}); \ No newline at end of file diff --git a/lib/commands/new_stage_region.js b/lib/commands/new_stage_region.js index 93459a194..ac83a326d 100644 --- a/lib/commands/new_stage_region.js +++ b/lib/commands/new_stage_region.js @@ -175,11 +175,12 @@ CMD.prototype._createCfStack = Promise.method(function() { var _this = this; // Start loading icon - var spinner = JawsCli.spinner('Creating CloudFormation Stack for stage "' + var spinner = JawsCli.spinner( + 'Creating CloudFormation stack "' + _this._stage - + '" and region "' + + '" - "' + _this._region - + '". This doesn\'t cost anything, but it takes a few minutes...'); + + '"'); spinner.start(); return AWSUtils.cfCreateStack( diff --git a/lib/commands/tag.js b/lib/commands/tag.js index 166d80a96..87b235e36 100644 --- a/lib/commands/tag.js +++ b/lib/commands/tag.js @@ -22,29 +22,24 @@ Promise.promisifyAll(fs); * @param {boolean} untag. default false * @returns {Promise} full path to jaws.json that was updated */ + module.exports.tag = function(type, fullPathToJawsJson, untag) { untag = !!(untag); var jawsJsonPath = fullPathToJawsJson ? fullPathToJawsJson : path.join(process.cwd(), 'jaws.json'); + var jawsJson = require(jawsJsonPath); return new Promise(function(resolve, reject) { if (!fs.existsSync(jawsJsonPath)) { - reject(new JawsError( - 'Could\'nt find a valid jaws.json. Sure you are in the correct directory?', - JawsError.errorCodes.UNKNOWN - )); + reject(new JawsError('Could\'nt find a valid jaws.json. Sure you are in the correct directory?')); } - var jawsJson = require(jawsJsonPath); if (type === 'lambda' && typeof jawsJson.lambda !== 'undefined') { jawsJson.lambda.deploy = !untag; } else if (type === 'endpoint' && typeof jawsJson.endpoint !== 'undefined') { jawsJson.endpoint.deploy = !untag; } else { - reject(new JawsError( - 'This jaws-module is not a lambda function or endpoint resource', - JawsError.errorCodes.UNKNOWN - )); + reject(new JawsError('This jaws-module is not a lambda function or endpoint resource')); } fs.writeFileSync(jawsJsonPath, JSON.stringify(jawsJson, null, 2)); @@ -68,6 +63,7 @@ module.exports.tagAll = function(JAWS, type, untag) { return utils[findAllFunc](JAWS._meta.projectRootPath) .then(function(jawsJsonPaths) { var tagQueue = []; + if (!jawsJsonPaths) { throw new JawsError('Could not find any lambdas', JawsError.errorCodes.UNKNOWN); } @@ -81,71 +77,34 @@ module.exports.tagAll = function(JAWS, type, untag) { }; /** - * List all lambda|api that are currently tagged + * List all lambda|endpoints that are currently tagged * * @param {Jaws} JAWS * @param type * @returns {Promise} */ module.exports.listAll = function(JAWS, type) { - var cwd = process.cwd(), - findAllFunc = (type == 'lambda') ? 'findAllLambdas' : 'findAllEndpoints'; + + var cwd = process.cwd(); + var findAllFunc = (type == 'lambda') ? 'findAllLambdas' : 'findAllEndpoints'; return utils[findAllFunc](JAWS._meta.projectRootPath) .then(function(lJawsJsonPaths) { + if (!lJawsJsonPaths) { - throw new JawsError('Could not find any ' + type + 's', JawsError.errorCodes.UNKNOWN); + throw new JawsError('Could not find any ' + type + 's'); } - var relPaths = [], - attr = (type == 'lambda') ? 'lambda' : 'endpoint'; + var fullPaths = []; + var attr = (type == 'lambda') ? 'lambda' : 'endpoint'; lJawsJsonPaths.forEach(function(ljp) { var jawsJson = require(ljp); if (jawsJson[attr] && jawsJson[attr].deploy == true) { - relPaths.push(path.relative(cwd, ljp)); + fullPaths.push(ljp); } }); - return Promise.all(relPaths); + return Promise.all(fullPaths); }); -}; - -/** - * Interactive select of items to tag - * - * @param {Jaws} JAWS - * @param type - * @param untag - * @returns {*} - */ -module.exports.tagMulti = function(JAWS, type, untag) { - var _this = this, - findAllFunc = (type == 'lambda') ? 'findAllLambdas' : 'findAllEndpoints'; - - return utils[findAllFunc](JAWS._meta.projectRootPath) - .then(function(lJawsJsonPaths) { - if (!lJawsJsonPaths) { - throw new JawsError('Could not find any ' + type + 's', JawsError.errorCodes.UNKNOWN); - } - - return inquirer.prompt([{ - type: 'checkbox', - message: 'Select actions to tag', - name: 'jawsPaths', - choices: lJawsJsonPaths, - },]); - }) - .then(function(answers) { - if (!answers || answers.jawsPaths.length == 0) { - throw new JawsError('Must select at least one', JawsError.errorCodes.UNKNOWN); - } - - var tagQueue = []; - answers.jawsPaths.forEach(function(jp) { - tagQueue.push(_this.tag(type, jp, untag)); - }); - - return Promise.all(tagQueue); - }); -}; +}; \ No newline at end of file diff --git a/lib/utils/index.js b/lib/utils/index.js index 24bf7f51c..d21b233b2 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -93,12 +93,12 @@ module.exports.findProjectRootPath = function(startDir) { }; /** - * Handle exit + * Execute (Command) * * @param promise */ -module.exports.handleExit = function(promise) { +module.exports.execute = function(promise) { promise .catch(JawsError, function(e) { console.error(e); @@ -258,6 +258,7 @@ module.exports.getAllLambdaNames = function(projectRootPath) { * @returns {*} region object for stage */ module.exports.getProjRegionConfig = function(projectStageObj, region) { + var region = projectStageObj.filter(function(regionObj) { return regionObj.region == region; }); diff --git a/tests/all.js b/tests/all.js index 5e7cbd7ce..57dd388d9 100644 --- a/tests/all.js +++ b/tests/all.js @@ -14,18 +14,18 @@ describe('AllTests', function() { after(function() {}); //require tests vs inline so we can run sequentially - //require('./cli/tag'); - //require('./cli/install'); - //require('./cli/env'); - //require('./cli/generate'); + require('./cli/tag'); + require('./cli/install'); + require('./cli/env'); + require('./cli/new_action'); /** * Tests below create AWS Resources */ - require('./cli/new_stage_region'); //require('./cli/dash'); //require('./cli/deploy_lambda'); //require('./cli/deploy_endpoint'); + //require('./cli/new_stage_region'); //require('./cli/new_project'); }); \ No newline at end of file diff --git a/tests/cli/deploy_endpoint.js b/tests/cli/deploy_endpoint.js index c7557f9ad..d7a1e3a51 100644 --- a/tests/cli/deploy_endpoint.js +++ b/tests/cli/deploy_endpoint.js @@ -1,12 +1,11 @@ 'use strict'; /** - * JAWS Test: Deploy API Command - * - Copies the test-prj template to your system's temp directory - * - Deploys an API based on the endpoints in the project + * JAWS Test: Deploy Endpoint */ + var Jaws = require('../../lib/index.js'), - theCmd = require('../../lib/commands/deploy_api'), + theCmd = require('../../lib/commands/deploy_endpoint'), JawsError = require('../../lib/jaws-error'), testUtils = require('../test_utils'), path = require('path'), @@ -16,32 +15,29 @@ var Jaws = require('../../lib/index.js'), projPath, JAWS; -describe('Test deploy api command', function() { +describe('Test deploy endpoint command', function() { before(function(done) { - projPath = testUtils.createTestProject( - config.name, - config.region, - config.stage, - config.iamRoleArnLambda, - config.iamRoleArnApiGateway, - config.envBucket); - process.chdir(path.join(projPath, 'back/lambdas/users/show')); - JAWS = new Jaws(); + return Promise.try(function() { + projPath = testUtils.createTestProject( + config.name, + config.region, + config.stage, + config.iamRoleArnLambda, + config.iamRoleArnApiGateway, + config.envBucket); + process.chdir(path.join(projPath, 'back/lambdas/sessions/show')); + JAWS = new Jaws(); - // Get Lambda Paths - lambdaPaths.lambda1 = path.join(projPath, 'back', 'lambdas', 'users', 'show', 'jaws.json'); - lambdaPaths.lambda2 = path.join(projPath, 'back', 'lambdas', 'users', 'signin', 'jaws.json'); - lambdaPaths.lambda3 = path.join(projPath, 'back', 'lambdas', 'users', 'signup', 'jaws.json'); - - done(); - }); - - after(function(done) { - done(); + // Get Lambda Paths + lambdaPaths.lambda1 = path.join(projPath, 'back', 'lambdas', 'users', 'show', 'jaws.json'); + lambdaPaths.lambda2 = path.join(projPath, 'back', 'lambdas', 'users', 'signin', 'jaws.json'); + lambdaPaths.lambda3 = path.join(projPath, 'back', 'lambdas', 'users', 'signup', 'jaws.json'); + }).then(done); }); describe('Positive tests', function() { + it('Deploy REST API', function(done) { this.timeout(0); diff --git a/tests/cli/deploy_lambda.js b/tests/cli/deploy_lambda.js index 546133c4a..e08f07e6b 100644 --- a/tests/cli/deploy_lambda.js +++ b/tests/cli/deploy_lambda.js @@ -4,7 +4,7 @@ * JAWS Test: Deploy Lambda Command */ var Jaws = require('../../lib/index.js'), - theCmd = require('../../lib/commands/deploy_lambda'), + CmdDeployLambda = require('../../lib/commands/deploy_lambda'), testUtils = require('../test_utils'), path = require('path'), assert = require('chai').assert, @@ -33,10 +33,6 @@ describe('Test "deploy lambda" command', function() { done(); }); - after(function(done) { - done(); - }); - describe('Positive tests', function() { it('Multi level module deploy', function(done) { @@ -44,7 +40,7 @@ describe('Test "deploy lambda" command', function() { process.chdir(path.join(projPath, 'back/lambdas/sessions/show')); - theCmd.run(JAWS, config.stage, false, false) + CmdDeployLambda.run(JAWS, config.stage, [config.region], false) .then(function(d) { done(); }) @@ -57,7 +53,7 @@ describe('Test "deploy lambda" command', function() { this.timeout(0); process.chdir(path.join(projPath, 'back/lambdas/bundle/browserify')); - theCmd.run(JAWS, config.stage, false, false) + CmdDeployLambda.run(JAWS, config.stage, [config.region], false) .then(function(d) { done(); }) @@ -70,7 +66,7 @@ describe('Test "deploy lambda" command', function() { this.timeout(0); process.chdir(path.join(projPath, 'back/lambdas/bundle/nonoptimized')); - theCmd.run(JAWS, config.stage, false, false) + CmdDeployLambda.run(JAWS, config.stage, [config.region], false) .then(function(d) { done(); }) diff --git a/tests/cli/env.js b/tests/cli/env.js index a91246be5..fb27cff5c 100644 --- a/tests/cli/env.js +++ b/tests/cli/env.js @@ -5,7 +5,7 @@ */ var Jaws = require('../../lib/index.js'), - theCmd = require('../../lib/commands/env'), + CmdEnv = require('../../lib/commands/env'), testUtils = require('../test_utils'), path = require('path'), assert = require('chai').assert; @@ -24,7 +24,7 @@ describe('Test "env" command', function() { config.iamRoleArnLambda, config.iamRoleArnApiGateway, config.envBucket); - process.chdir(path.join(projPath, 'back', 'lambdas', 'users', 'show')); + process.chdir(path.join(projPath, 'back', 'lambdas', 'sessions', 'show')); JAWS = new Jaws(); }); @@ -36,7 +36,7 @@ describe('Test "env" command', function() { it('Test env command', function(done) { this.timeout(0); - theCmd.listEnv(JAWS, config.stage) + CmdEnv.listEnv(JAWS, config.stage) .then(function(d) { done(); }) diff --git a/tests/cli/generate.js b/tests/cli/generate.js deleted file mode 100644 index 9eb693493..000000000 --- a/tests/cli/generate.js +++ /dev/null @@ -1,87 +0,0 @@ -'use strict'; - -/** - * JAWS Test: Deploy API Command - * - Copies the test-prj template to your system's temp directory - * - Deploys an API based on the endpoints in the project - */ -var Jaws = require('../../lib/index.js'), - theCmd = require('../../lib/commands/generate'), - testUtils = require('../test_utils'), - path = require('path'), - fs = require('fs'), - assert = require('chai').assert; - -var config = require('../config'); - -var resourceDir, - projPath, - JAWS; - -describe('Test generate command', function() { - - before(function(done) { - projPath = testUtils.createTestProject( - config.name, - config.region, - config.stage, - config.iamRoleArnLambda, - config.iamRoleArnApiGateway, - config.envBucket); - - process.chdir(projPath); - JAWS = new Jaws(); - - resourceDir = path.join(projPath, 'back', 'lambdas', 'unittests'); - - done(); - }); - - after(function(done) { - done(); - }); - - describe('Positive tests', function() { - it('Just lambda', function() { - - this.timeout(0); - - var testAction = 'lambdaOnly'; - - return theCmd.run(JAWS, true, false, testAction, 'unittests') - .then(function() { - var jawsJson = require(resourceDir + '/' + testAction + '/jaws.json'); - - assert.equal(true, !!jawsJson); - assert.equal(true, !!jawsJson.lambda); - assert.equal(true, !jawsJson.endpoint); - assert.equal(true, fs.existsSync(path.join(resourceDir, testAction, 'index.js'))); - assert.equal(true, fs.existsSync(path.join(resourceDir, testAction, 'event.json'))); - - testAction = 'apiOnly'; - return theCmd.run(JAWS, false, true, testAction, 'unittests'); - }) - .then(function() { - var jawsJson = require(resourceDir + '/' + testAction + '/jaws.json'); - - assert.equal(true, !!jawsJson); - assert.equal(true, !jawsJson.lambda); - assert.equal(true, !!jawsJson.endpoint); - assert.equal(true, !fs.existsSync(path.join(resourceDir, testAction, 'index.js'))); - assert.equal(true, !fs.existsSync(path.join(resourceDir, testAction, 'event.json'))); - - testAction = 'both'; - return theCmd.run(JAWS, true, true, testAction, 'unittests') - }) - .then(function() { - var jawsJson = require(resourceDir + '/' + testAction + '/jaws.json'); - - assert.equal(true, !!jawsJson); - assert.equal(true, !!jawsJson.lambda); - assert.equal(true, !!jawsJson.endpoint); - assert.equal(true, fs.existsSync(path.join(resourceDir, testAction, 'index.js'))); - assert.equal(true, fs.existsSync(path.join(resourceDir, testAction, 'event.json'))); - }); - }); - }); -}); \ No newline at end of file diff --git a/tests/cli/install.js b/tests/cli/install.js index 8ff45dff3..958b66576 100644 --- a/tests/cli/install.js +++ b/tests/cli/install.js @@ -5,7 +5,7 @@ */ var Jaws = require('../../lib/index.js'), - theCmd = require('../../lib/commands/install'), + CmdInstall = require('../../lib/commands/install'), JawsError = require('../../lib/jaws-error'), testUtils = require('../test_utils'), path = require('path'), @@ -25,7 +25,7 @@ describe('Test "install" command', function() { config.iamRoleArnLambda, config.iamRoleArnApiGateway, config.envBucket); - process.chdir(path.join(projPath, 'back', 'lambdas', 'users', 'show')); + process.chdir(path.join(projPath, 'back', 'lambdas', 'sessions', 'show')); JAWS = new Jaws(); }); @@ -37,7 +37,7 @@ describe('Test "install" command', function() { it('Install module', function(done) { this.timeout(0); - theCmd.install(JAWS, 'https://github.com/jaws-stack/jaws-users-crud-ddb-jwt-js') + CmdInstall.install(JAWS, 'https://github.com/jaws-stack/jaws-users-crud-ddb-jwt-js') .then(function() { done(); }) diff --git a/tests/cli/new_action.js b/tests/cli/new_action.js new file mode 100644 index 000000000..50c8c8dda --- /dev/null +++ b/tests/cli/new_action.js @@ -0,0 +1,70 @@ +'use strict'; + +/** + * JAWS Test: New Action Command + */ + +var Jaws = require('../../lib/index.js'), + CmdNewAction = require('../../lib/commands/new_action'), + JawsError = require('../../lib/jaws-error'), + testUtils = require('../test_utils'), + Promise = require('bluebird'), + path = require('path'), + shortid = require('shortid'), + assert = require('chai').assert; + +var config = require('../config'), + projPath, + JAWS; + +describe('Test "new action" command', function() { + + before(function(done) { + this.timeout(0); + + // Tag All Lambdas & Endpoints + return Promise.try(function() { + + // Create Test Project + projPath = testUtils.createTestProject( + config.name, + config.region, + config.stage, + config.iamRoleArnLambda, + config.iamRoleArnApiGateway, + config.envBucket); + process.chdir(path.join(projPath, 'back')); + + // Instantiate JAWS + JAWS = new Jaws(); + }).then(done); + }); + + describe('Positive tests', function() { + + it('Test "new action" command', function(done) { + this.timeout(0); + + var action = { + type: 'both', + resource: 'users', + action: 'create', + }; + + CmdNewAction.run(JAWS, action) + .then(function() { + var jawsJson = require(path.join(process.cwd(), 'lambdas/users/create/jaws.json')); + assert.isTrue(typeof jawsJson.lambda !== 'undefined'); + assert.isTrue(typeof jawsJson.endpoint !== 'undefined'); + assert.isTrue(jawsJson.lambda.functionName === 'users-create'); + done(); + }) + .catch(JawsError, function(e) { + done(e); + }) + .error(function(e) { + done(e); + }); + }); + }); +}); \ No newline at end of file diff --git a/tests/cli/new_project.js b/tests/cli/new_project.js index 590aff05a..77f338b63 100644 --- a/tests/cli/new_project.js +++ b/tests/cli/new_project.js @@ -5,6 +5,7 @@ * - Creates a new project in your system's temp directory * - Deletes the CF stack created by the project */ + var Jaws = require('../../lib/index.js'), JawsError = require('../../lib/jaws-error'), theCmd = require('../../lib/commands/new_project'), diff --git a/tests/cli/new_stage_region.js b/tests/cli/new_stage_region.js index 37935265e..59f29d5c3 100644 --- a/tests/cli/new_stage_region.js +++ b/tests/cli/new_stage_region.js @@ -41,14 +41,7 @@ describe('Test "new stage/region" command', function() { // Instantiate JAWS JAWS = new Jaws(); - }) - .then(function() { - return done(); - }); - }); - - after(function(done) { - done(); + }).then(done); }); describe('Positive tests', function() { diff --git a/tests/cli/tag.js b/tests/cli/tag.js index 109b0fd2c..d174c634c 100644 --- a/tests/cli/tag.js +++ b/tests/cli/tag.js @@ -6,7 +6,7 @@ * - Deploys an API based on the endpoints in the project */ var Jaws = require('../../lib/index.js'), - theCmd = require('../../lib/commands/tag'), + CmdTag = require('../../lib/commands/tag'), testUtils = require('../test_utils'), path = require('path'), assert = require('chai').assert, @@ -32,9 +32,9 @@ describe('Test "tag" command', function() { JAWS = new Jaws(); // Get Lambda Paths - lambdaPaths.lambda1 = path.join(projPath, 'back', 'lambdas', 'users', 'show', 'jaws.json'); - lambdaPaths.lambda2 = path.join(projPath, 'back', 'lambdas', 'users', 'signin', 'jaws.json'); - lambdaPaths.lambda3 = path.join(projPath, 'back', 'lambdas', 'users', 'signup', 'jaws.json'); + lambdaPaths.lambda1 = path.join(projPath, 'back', 'lambdas', 'sessions', 'show', 'jaws.json'); + lambdaPaths.lambda2 = path.join(projPath, 'back', 'lambdas', 'sessions', 'create', 'jaws.json'); + lambdaPaths.lambda3 = path.join(projPath, 'back', 'lambdas', 'users', 'create', 'jaws.json'); done(); }); @@ -47,18 +47,18 @@ describe('Test "tag" command', function() { this.timeout(0); - theCmd.tag('lambda', lambdaPaths.lambda1, false) + CmdTag.tag('lambda', lambdaPaths.lambda1, false) .then(function() { assert.equal(true, require(lambdaPaths.lambda1).lambda.deploy); assert.equal(false, require(lambdaPaths.lambda2).lambda.deploy); assert.equal(false, require(lambdaPaths.lambda3).lambda.deploy); - return theCmd.tagAll(JAWS, 'lambda', false); + return CmdTag.tagAll(JAWS, 'lambda', false); }) .then(function() { assert.equal(true, require(lambdaPaths.lambda1).lambda.deploy); assert.equal(true, require(lambdaPaths.lambda2).lambda.deploy); assert.equal(true, require(lambdaPaths.lambda3).lambda.deploy); - return theCmd.tagAll(JAWS, 'lambda', true); + return CmdTag.tagAll(JAWS, 'lambda', true); }) .then(function() { assert.equal(false, require(lambdaPaths.lambda1).lambda.deploy); @@ -74,18 +74,18 @@ describe('Test "tag" command', function() { it('tag endpoints', function(done) { this.timeout(0); - theCmd.tag('api', lambdaPaths.lambda1, true) + CmdTag.tag('endpoint', lambdaPaths.lambda1, true) .then(function() { assert.equal(false, require(lambdaPaths.lambda1).endpoint.deploy); assert.equal(true, require(lambdaPaths.lambda2).endpoint.deploy); assert.equal(true, require(lambdaPaths.lambda3).endpoint.deploy); - return theCmd.tagAll(JAWS, 'api', false); + return CmdTag.tagAll(JAWS, 'endpoint', false); }) .then(function() { assert.equal(true, require(lambdaPaths.lambda1).endpoint.deploy); assert.equal(true, require(lambdaPaths.lambda2).endpoint.deploy); assert.equal(true, require(lambdaPaths.lambda3).endpoint.deploy); - return theCmd.tagAll(JAWS, 'api', true); + return CmdTag.tagAll(JAWS, 'endpoint', true); }) .then(function() { assert.equal(false, require(lambdaPaths.lambda1).endpoint.deploy);