Skip to content

Commit

Permalink
🎉 Add progress indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Aug 14, 2017
1 parent 863d8e2 commit fe77387
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 46 deletions.
99 changes: 55 additions & 44 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env node

const ora = require('ora')
const neodoc = require('neodoc')
const parseArn = require('aws-arn-parser')
const prettyBytes = require('pretty-bytes')

const amazon = require('./lib/amazon')
const builder = require('./lib/builder')
Expand All @@ -24,59 +26,68 @@ Options:

async function main () {
const args = neodoc.run(usage)

if (args.create) {
const zipFile = await builder.createZipFile(process.cwd())
const result = await amazon.createFunction({ zipFile, functionName: args['<name>'], role: args['--role'] })

const lambdaArn = result.FunctionArn

console.log(`Created new Lambda function with ARN: ${lambdaArn}`)

const definition = args['--swagger']
? await swagger.loadSwaggerFile(args['--swagger'], lambdaArn)
: await swagger.forwardAllDefinition(lambdaArn)

const { id } = await amazon.createApiGateway({ definition, lambdaArn })

console.log(`Created new API Gateway with id: ${id}`)

if (args['--deploy']) {
const stage = 'prod'
const { region } = parseArn(lambdaArn)

await amazon.deployApiGateway({ id, stage })

console.log(`Now serving live requests at: https://${id}.execute-api.${region}.amazonaws.com/${stage}`)
const spinner = ora()

try {
if (args.create) {
spinner.start('Packaging app for Lambda')
const zipFile = await builder.createZipFile(process.cwd())
spinner.succeed(`App packaged successfully! Final size: ${prettyBytes(zipFile.byteLength)}`)

spinner.start('Creating Lambda function')
const lambdaArn = await amazon.createFunction({ zipFile, functionName: args['<name>'], role: args['--role'] })
spinner.succeed(`Created new Lambda function with ARN: ${lambdaArn}`)

const definition = args['--swagger']
? await swagger.loadSwaggerFile(args['--swagger'], lambdaArn)
: await swagger.forwardAllDefinition(lambdaArn)

spinner.start('Creating new API Gateway')
const { id } = await amazon.createApiGateway({ definition, lambdaArn })
spinner.succeed(`Created new API Gateway with id: ${id}`)

if (args['--deploy']) {
const stage = 'prod'
const { region } = parseArn(lambdaArn)

spinner.start('Deploying the API to a live address')
await amazon.deployApiGateway({ id, stage })
spinner.succeed(`Now serving live requests at: https://${id}.execute-api.${region}.amazonaws.com/${stage}`)
}
}
}

if (args.update) {
const zipFile = await builder.createZipFile(process.cwd())
const result = await amazon.updateFunction({ zipFile, functionName: args['<name>'] })

const lambdaArn = result.FunctionArn
if (args.update) {
spinner.start('Packaging app for Lambda')
const zipFile = await builder.createZipFile(process.cwd())
spinner.succeed(`App packaged successfully! Final size: ${prettyBytes(zipFile.byteLength)}`)

console.log(`Updated existing Lambda function with ARN: ${lambdaArn}`)
spinner.start('Creating Lambda function')
const lambdaArn = await amazon.updateFunction({ zipFile, functionName: args['<name>'] })
spinner.succeed(`Updated existing Lambda function with ARN: ${lambdaArn}`)

const definition = args['--swagger']
? await swagger.loadSwaggerFile(args['--swagger'], lambdaArn)
: await swagger.forwardAllDefinition(lambdaArn)
const definition = args['--swagger']
? await swagger.loadSwaggerFile(args['--swagger'], lambdaArn)
: await swagger.forwardAllDefinition(lambdaArn)

const id = args['--rest-api-id']
const id = args['--rest-api-id']

await amazon.updateApiGateway({ id, definition, lambdaArn })
spinner.start('Updating existing API Gateway')
await amazon.updateApiGateway({ id, definition, lambdaArn })
spinner.succeed(`Updated existing API Gateway with id: ${id}`)

console.log(`Updated existing API Gateway with id: ${id}`)
if (args['--deploy']) {
const stage = 'prod'
const { region } = parseArn(lambdaArn)

if (args['--deploy']) {
const stage = 'prod'
const { region } = parseArn(lambdaArn)

await amazon.deployApiGateway({ id, stage })

console.log(`Now serving live requests at: https://${id}.execute-api.${region}.amazonaws.com/${stage}`)
spinner.start('Deploying the API to a live address')
await amazon.deployApiGateway({ id, stage })
spinner.succeed(`Now serving live requests at: https://${id}.execute-api.${region}.amazonaws.com/${stage}`)
}
}
} catch (err) {
spinner.fail(err.toString())

throw err
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/amazon.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ exports.createFunction = function ({ zipFile, functionName, role }) {
Timeout: defaultParams.Timeout
}

return lambda.createFunction(params).promise()
return lambda.createFunction(params).promise().then(res => res.FunctionArn)
}

exports.updateFunction = function ({ zipFile, functionName }) {
Expand All @@ -48,7 +48,7 @@ exports.updateFunction = function ({ zipFile, functionName }) {
ZipFile: zipFile
}

return lambda.updateFunctionCode(params).promise()
return lambda.updateFunctionCode(params).promise().then(res => res.FunctionArn)
}

exports.createApiGateway = async function ({ definition, lambdaArn }) {
Expand Down
102 changes: 102 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"load-json-file": "^2.0.0",
"load-yaml-file": "^0.1.0",
"neodoc": "^1.4.0",
"ora": "^1.3.0",
"pretty-bytes": "^4.0.2",
"rm-file": "^1.0.0",
"shortid": "^2.2.8"
}
Expand Down

0 comments on commit fe77387

Please sign in to comment.