2015-09-06 22:05:41 -05:00

206 lines
7.5 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')
.description('Creates a new JAWS project in the current working directory and creates an AWS CloudFormation ' +
'stack which provisions essential AWS resources for your JAWS project.')
.option('-n, --proj-name <name>', 'Project name')
.option('-s, --stage <stage>', 'Stage to create. all lowercase')
.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')
.option('-r, --lambda-region <region>', 'Region lambda(s) will be created in (Can add more later)')
.option('-e, --notification-email <email>', 'Email to be notified with stack alerts')
.option('-p, --profile <profile name>', 'AWS profile to use (as defined in ~/.aws/credentials)')
.option('-d, --dont-exe-cf', 'Dont execute CloudFormation file')
.action(function(options) {
var theCMd = require('../lib/commands/new');
handleExit(theCMd.new(
options.projName, // name is reserved in commander...
options.stage ? options.stage.toLowerCase() : null,
options.s3Bucket,
options.lambdaRegion,
options.notificationEmail,
options.profile,
options.dontExeCf
));
});
//TODO: generate stage
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.generate(
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(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) {
var theCmd = require('../lib/commands/deploy');
type = type.toLowerCase();
switch (type) {
case 'api':
var allTagged = (options.tags) ? true : false;
handleExit(theCmd.deployApi(JAWS, stage, region, allTagged));
break;
case 'lambda':
var allTagged = (options.tags) ? true : false,
allAtOnce = (options.allAtOnce) ? true : false;
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.' +
'\nEx: 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('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;
}
}