1 | var txain = require('txain')
|
2 | var utils = require('./utils')
|
3 | var AWS = require('aws-sdk')
|
4 | var _ = require('underscore')
|
5 | var inquirer = require('inquirer')
|
6 |
|
7 | module.exports = function(yargs) {
|
8 | yargs.command('create', 'Creates a new AWS stage', function (yargs) {
|
9 | module.exports.create(utils.end)
|
10 | })
|
11 | .command('select', 'Selects an existing AWS stage to be used for deploying', function (yargs) {
|
12 | module.exports.select(utils.end)
|
13 | })
|
14 | .command('show', 'Shows the information of an existing AWS stage', function(yargs) {
|
15 | module.exports.show(utils.end)
|
16 | })
|
17 | .wrap(yargs.terminalWidth())
|
18 | .help('help')
|
19 | .argv
|
20 | }
|
21 |
|
22 | module.exports.show = function(callback) {
|
23 | var config = utils.readConfig()
|
24 | var stage = config.stage
|
25 | if (!stage) {
|
26 | console.log('No stage selected yet. Please use `backbeam stage select` to select an existing stage')
|
27 | process.exit(1)
|
28 | }
|
29 | console.log(stage)
|
30 | callback()
|
31 | }
|
32 |
|
33 | module.exports.create = function(callback) {
|
34 | var region = utils.requiresRegion()
|
35 | var api = utils.requiresAPI()
|
36 | console.log('Creating API for region', region)
|
37 | var apigateway = new AWS.APIGateway()
|
38 |
|
39 | var question = {
|
40 | name: 'name',
|
41 | message: 'Stage name',
|
42 | }
|
43 | inquirer.prompt([question], function(answers) {
|
44 | txain(function(callback) {
|
45 | var params = {
|
46 | restApiId: api.id,
|
47 | stageName: answers.name,
|
48 | }
|
49 | apigateway.createDeployment(params, callback)
|
50 | })
|
51 | .then(function(api, callback) {
|
52 | var config = utils.readConfig()
|
53 | config.stage = answers.name
|
54 | utils.writeConfig(config)
|
55 | console.log('Updated stage configuration successfully')
|
56 | })
|
57 | .end(callback)
|
58 | })
|
59 | }
|
60 |
|
61 | module.exports.select = function(callback) {
|
62 | var region = utils.requiresRegion()
|
63 | var api = utils.requiresAPI()
|
64 | console.log('Querying stages for region %s and API %s', region, api.name)
|
65 |
|
66 | var apigateway = new AWS.APIGateway()
|
67 |
|
68 | txain(function(callback) {
|
69 | apigateway.getStages({ restApiId: api.id }, callback)
|
70 | })
|
71 | .then(function(data, callback) {
|
72 | if (data.item.length === 0) {
|
73 | var question = {
|
74 | name: 'create',
|
75 | type: 'confirm',
|
76 | message: 'No existing stages found. Do you want to create a new one?',
|
77 | }
|
78 | inquirer.prompt([question], function(answers) {
|
79 | if (answers.create) {
|
80 | module.exports.create(callback)
|
81 | }
|
82 | })
|
83 | return
|
84 | }
|
85 |
|
86 | var choices = data.item.map(function(item) {
|
87 | return item.stageName
|
88 | })
|
89 | choices.push(new inquirer.Separator())
|
90 | choices.push({ name: 'Or create a new stage', value: '' })
|
91 |
|
92 | var question = {
|
93 | name: 'stage',
|
94 | type: 'list',
|
95 | choices: choices,
|
96 | message: 'Choose a stage',
|
97 | }
|
98 | inquirer.prompt([question], function(answers) {
|
99 | var stage = answers.stage
|
100 | if (!stage) {
|
101 | return module.exports.create(callback)
|
102 | }
|
103 | var config = utils.readConfig()
|
104 | config.stage = stage
|
105 | utils.writeConfig(config)
|
106 | console.log('Updated stage configuration successfully')
|
107 | callback()
|
108 | })
|
109 | })
|
110 | .end(callback)
|
111 | }
|