2015-09-09 16:01:24 -07:00

231 lines
8.8 KiB
JavaScript
Executable File

#!/usr/bin/env node
'use strict';
var JawsError = require('../lib/jaws-error'),
Jaws = require('../lib/index.js'),
program = require('commander'),
utils = require('../lib/utils'),
Promise = require('bluebird'),
handleExit = utils.handleExit;
var JAWS = new Jaws();
Promise.onPossiblyUnhandledRejection(function(error) {
throw error;
});
program
.option('-v, --verbose')
.version(JAWS._meta.version);
program
.command('new <type> [regionName] [stageName]')
.description('Creates JAWS "project" or "region"/"stage" in existing project. Does so in cwd. ' +
'If type is region|stage, regionName and stageName are required. If region, the stageName is the stage that will' +
'be primed in the new region.')
.option('-d, --dont-exe-cf', 'Dont execute CloudFormation file')
//Things only valid for new project below:
.option('-p, --profile <profile name>', 'AWS profile to use (as defined in ~/.aws/credentials). Only valid for new project.')
.option('-n, --proj-name <name>', 'Project name. Only valid for new project.')
.option('-s, --stage <stage>', 'Stage to create. all lowercase Only valid for new project.')
.option('-r, --region <region>', 'Region lambda(s)&API(s) will be created in (Can add more later). Only valid for new project.')
.option('-3, --s3-bucket <bucketName>', 'S3 bucket to store stage env files. Key is JAWS/envVars/<projName>/<stage>. Bucket will be created if it DNE. Only valid for new project.')
.option('-e, --notification-email <email>', 'Email to be notified with stack alerts. Only valid for new project.')
.action(function(type, regionName, stageName, options) {
type = type.toLowerCase();
if (type == 'project') {
var theCmd = require('../lib/commands/new_project');
handleExit(theCmd.create(
options.projName, // name is reserved in commander...
options.stage ? options.stage.toLowerCase() : null,
options.s3Bucket,
options.region,
options.notificationEmail,
options.profile,
options.dontExeCf
));
} else if (type == 'region' || type == 'stage') {
var theCmd = require('../lib/commands/new_region_stage'),
isCreateRegion = (type == 'region');
if (!regionName || !stageName) {
console.error("Region and stage name required. Stage name will be the 1st stage primed in new region");
process.exit(1);
}
handleExit(theCmd.create(isCreateRegion, JAWS, regionName, stageName, options.dontExeCf));
} else {
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, --api', '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.api || options.both) {
options.api = true;
}
var theCmd = require('../lib/commands/generate');
handleExit(theCmd.run(
JAWS, options.lambda, options.api, options.functionName, options.resourceName, options.lambdaRuntime
));
});
program
.command('install <url>')
.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));
});
program
.command('tag [type]')
.description('Tag lambda function or api gateway resource (api) for deployment ' +
'the next time deploy command is run. Type "lambda" is the default.')
.option('-u, --untag', 'un-tag lambda|api')
.option('-m, --multi', 'interactively select multiple')
.option('-a, --tag-all', 'tag all lambda|api functions in project')
.option('-l, --list-all', 'list all tagged lambda|api functions in project')
.option('-n, --untag-all', 'un-tag all lambda|api functions in project')
.action(function(type, options) {
type = type || 'lambda';
type = type.toLowerCase();
if (-1 == ['api', 'lambda'].indexOf(type)) {
console.error('Unsupported type ' + type + '. Must be api|lambda');
process.exit(1);
}
var theCmd = require('../lib/commands/tag');
if (options.listAll) {
handleExit(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));
} else if (options.multi) {
handleExit(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));
}
});
program
.command('deploy <type> [stage] [region]')
.description('Deploy a lambda function (type lambda), a REST API (api), or provision AWS resources (resources) for the specified stage.' +
' By default will tag and deploy type at cwd')
.option('-t, --tags', 'Deploy all lambdas tagged as deployable in their jaws.json. Default is to just deploy cwd')
.option('-e, --all-at-once', 'By default, lambdas are deployed once at a time. This deploys all concurrently')
.action(function(type, stage, region, options) {
type = type.toLowerCase();
switch (type) {
case 'api':
var allTagged = (options.tags) ? true : false;
var theCmd = require('../lib/commands/deploy_api');
handleExit(theCmd.deployApi(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.deployLambdas(JAWS, stage, allTagged, allAtOnce, region));
break;
default:
console.error('Unsupported type ' + type + '. Must be api|lambda|resources');
process.exit(1);
break;
}
});
program
.command('env <cmd> <stage> [key] [val]')
.description('Manage env vars for stage. Valid cmds: list,get,set,unset.')
.usage('get test USERNAME or set test USERNAME blah.')
.action(function(cmd, stage, key, val) {
var theCmd = require('../lib/commands/env');
cmd = cmd.toLowerCase();
switch (cmd) {
case 'list':
handleExit(theCmd.listEnv(JAWS, stage));
break;
case 'get':
if (!key) {
console.error('Must specify key to set');
process.exit(1);
}
handleExit(theCmd.getEnvKey(JAWS, stage, key));
break;
case 'set':
if (!key || typeof val == 'undefined') {
console.error('Must specify key and value');
process.exit(1);
}
handleExit(theCmd.setEnvKey(JAWS, stage, key, val));
break;
case 'unset':
if (!key) {
console.error('Must specify key to unset');
process.exit(1);
}
if (val) {
console.error('Do not specify val for unset');
process.exit(1);
}
handleExit(theCmd.setEnvKey(JAWS, stage, key));
break;
default:
console.error('Unsupported cmd "' + cmd + '". Must be list|get|set|unset');
process.exit(1);
break;
}
});
program
.command('dash [stage] [region]')
.description('Check deployment status and deploy resources for a stage and region')
.action(function(stage, region) {
var theCmd = require('../lib/commands/dash');
handleExit(theCmd.run(JAWS, stage, region));
});
program
.command('logs <stage>')
.description('Get logs for the lambda function in the specified stage in your current working directory.')
.action(function(stage) {
var theCmd = require('../lib/commands/logs');
handleExit(theCmd.logs(JAWS, stage));
});
if (process.argv.length == 2) {
program.outputHelp();
} else {
program.parse(process.argv);
if (program.verbose) {
process.env.JAWS_VERBOSE = true;
}
}