2015-09-14 11:42:32 -05:00

257 lines
7.3 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'),
minimist = require('minimist'),
execute = utils.execute;
Promise.onPossiblyUnhandledRejection(function(error) {
throw error;
});
var JAWS = new Jaws();
program
.option('-v, --verbose')
.version(JAWS._meta.version);
/**
* New
* - Create a new project|stage|region|action
*/
program
.command('new <type>')
.allowUnknownOption()
.description('Make a new "project", "stage", "region", or "action <resource> <action>"')
.action(function() {
// Parse Args
var args = minimist(process.argv.slice(3));
var type = args._[0] ? args._[0].toLowerCase() : null;
if (type == 'project') {
// New Project
var CmdNewProject = require('../lib/commands/new_project');
execute(CmdNewProject.run(
args.name,
args.stage ? args.stage.toLowerCase() : null,
args.s3Bucket,
args.region,
args.email,
args.profile,
args.noCf
));
} else if (type == 'region' || type == 'stage') {
// New Region/Stage
var CmdNewStageRegion = require('../lib/commands/new_stage_region');
execute(CmdNewStageRegion.run(
JAWS,
type,
args.stage,
args.region,
args.noCf
));
} else if (type === 'action') {
var action = {};
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 action users create".');
}
action.resource = args._[1];
action.action = args._[2];
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|action');
process.exit(1);
}
});
/**
* Install
* - Install a jaws-module from a Github url
*/
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 to your project')
.action(function(url, options) {
var theCmd = require('../lib/commands/install');
execute(theCmd.install(JAWS, url, options.save));
});
/**
* Tag
* - Tag a lambda or endpoint for deployment
*/
program
.command('tag [type]')
.description('Tag lambda function or api gateway resource (endpoint) for deployment ' +
'the next time deploy command is run. Type "lambda" is the default.')
.option('-u, --untag', 'un-tag lambda|endpoint')
.option('-a, --tag-all', 'tag all lambda|endpoint functions in project')
.option('-l, --list-all', 'list all tagged lambda|endpoint functions in project')
.option('-n, --untag-all', 'un-tag all lambda|endpoint functions in project')
.action(function(type, options) {
type = type || 'lambda';
type = type.toLowerCase();
if (-1 == ['endpoint', 'lambda'].indexOf(type)) {
console.error('Unsupported type ' + type + '. Must be endpoint|lambda');
process.exit(1);
}
var CmdTag = require('../lib/commands/tag');
if (options.listAll) {
execute(CmdTag.listAll(JAWS, type).then(function(relPaths) {
console.log(relPaths);
}));
} else if (options.tagAll || options.untagAll) {
var untag = (options.untagAll) ? true : false;
execute(CmdTag.tagAll(JAWS, type, untag));
} else {
// If not tagging all, you have to be tagging whats in your CWD (null 1st param)
execute(CmdTag.tag(type, null, options.untag));
}
});
/**
* Deploy
* - Deploy Lambda or Endpoint
*/
program
.command('deploy <type> [stage] [region]')
.description('Deploy a lambda function (type lambda), a REST API (endpoint), 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 'endpoint':
var allTagged = (options.tags) ? true : false;
var theCmd = require('../lib/commands/deploy_endpoint');
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');
execute(theCmd.run(JAWS, stage, region, allTagged, allAtOnce));
break;
default:
console.error('Unsupported type ' + type + '. Must be endpoint|lambda|resources');
process.exit(1);
break;
}
});
/**
* Env
* - Manage ENV variables for a stage
*/
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 CmdEnv = require('../lib/commands/env');
cmd = cmd.toLowerCase();
switch (cmd) {
case 'list':
execute(CmdEnv.listEnv(JAWS, stage));
break;
case 'get':
if (!key) {
console.error('Must specify key to set');
process.exit(1);
}
execute(CmdEnv.getEnvKey(JAWS, stage, key));
break;
case 'set':
if (!key || typeof val == 'undefined') {
console.error('Must specify key and value');
process.exit(1);
}
execute(CmdEnv.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);
}
execute(CmdEnv.setEnvKey(JAWS, stage, key));
break;
default:
console.error('Unsupported cmd "' + cmd + '". Must be list|get|set|unset');
process.exit(1);
break;
}
});
/**
* Dash
*/
program
.command('dash')
.description('View a project summary and select resources to deploy.')
.action(function() {
var CmdDash = require('../lib/commands/dash');
execute(CmdDash.run(JAWS));
});
if (process.argv.length == 2) {
program.outputHelp();
} else {
program.parse(process.argv);
if (program.verbose) {
process.env.JAWS_VERBOSE = true;
}
}