From fe7738759201399ebffdc28d99763d8493b2975c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Mon, 14 Aug 2017 23:30:01 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20Add=20progress=20indicators?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 99 ++++++++++++++++++++++++-------------------- lib/amazon.js | 4 +- package-lock.json | 102 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 + 4 files changed, 161 insertions(+), 46 deletions(-) diff --git a/app.js b/app.js index 36567ac..2d0246d 100755 --- a/app.js +++ b/app.js @@ -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') @@ -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[''], 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[''], 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[''] }) - 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[''] }) + 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 } } diff --git a/lib/amazon.js b/lib/amazon.js index dd1ea53..01f5dc3 100644 --- a/lib/amazon.js +++ b/lib/amazon.js @@ -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 }) { @@ -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 }) { diff --git a/package-lock.json b/package-lock.json index a22f0ba..4f49602 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,11 @@ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" }, + "ansi-styles": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", + "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=" + }, "argparse": { "version": "1.0.9", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.9.tgz", @@ -54,6 +59,31 @@ "isarray": "1.0.0" } }, + "chalk": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", + "requires": { + "ansi-styles": "2.2.1", + "escape-string-regexp": "1.0.5", + "has-ansi": "2.0.0", + "strip-ansi": "3.0.1", + "supports-color": "2.0.0" + } + }, + "cli-cursor": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-2.1.0.tgz", + "integrity": "sha1-s12sN2R5+sw+lHR9QdDQ9SOP/LU=", + "requires": { + "restore-cursor": "2.0.0" + } + }, + "cli-spinners": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-1.0.0.tgz", + "integrity": "sha1-75h+09SDkaw9q5GAtAanQhgNbmo=" + }, "cp-file": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/cp-file/-/cp-file-4.2.0.tgz", @@ -89,6 +119,11 @@ "is-arrayish": "0.2.1" } }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" + }, "esprima": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.0.tgz", @@ -123,6 +158,14 @@ "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=" }, + "has-ansi": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", + "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", + "requires": { + "ansi-regex": "2.1.1" + } + }, "ieee754": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.8.tgz", @@ -194,6 +237,14 @@ "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=" }, + "log-symbols": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-1.0.2.tgz", + "integrity": "sha1-N2/3tY6jCGoPCfrMdGF+ylAeGhg=", + "requires": { + "chalk": "1.1.3" + } + }, "lru-cache": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.1.tgz", @@ -211,6 +262,11 @@ "pify": "2.3.0" } }, + "mimic-fn": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-1.1.0.tgz", + "integrity": "sha1-5md4PZLonb00KBi1IwudYqZyrRg=" + }, "neodoc": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/neodoc/-/neodoc-1.4.0.tgz", @@ -235,6 +291,25 @@ "path-key": "2.0.1" } }, + "onetime": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-2.0.1.tgz", + "integrity": "sha1-BnQoIw/WdEOyeUsiu6UotoZ5YtQ=", + "requires": { + "mimic-fn": "1.1.0" + } + }, + "ora": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/ora/-/ora-1.3.0.tgz", + "integrity": "sha1-gAeN0rkqk0r2ajrXKluRBpTt5Ro=", + "requires": { + "chalk": "1.1.3", + "cli-cursor": "2.1.0", + "cli-spinners": "1.0.0", + "log-symbols": "1.0.2" + } + }, "p-finally": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", @@ -258,6 +333,11 @@ "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=" }, + "pretty-bytes": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-4.0.2.tgz", + "integrity": "sha1-sr+C5zUNZcbDOqlaqlpPYyf2HNk=" + }, "pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", @@ -273,6 +353,15 @@ "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" }, + "restore-cursor": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-2.0.0.tgz", + "integrity": "sha1-n37ih/gv0ybU/RYpI9YhKe7g368=", + "requires": { + "onetime": "2.0.1", + "signal-exit": "3.0.2" + } + }, "rm-file": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/rm-file/-/rm-file-1.0.0.tgz", @@ -319,6 +408,14 @@ "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "requires": { + "ansi-regex": "2.1.1" + } + }, "strip-bom": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", @@ -329,6 +426,11 @@ "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=" }, + "supports-color": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", + "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=" + }, "url": { "version": "0.10.3", "resolved": "https://registry.npmjs.org/url/-/url-0.10.3.tgz", diff --git a/package.json b/package.json index 2a77726..65f4430 100644 --- a/package.json +++ b/package.json @@ -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" }