UNPKG

express-lambda

Version:

Make AWS lambda behave like an express app

78 lines (66 loc) 3.45 kB
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var shell = require("shelljs"); var fs = require("fs"); var path = require("path"); var crypto = require("crypto"); var tmp = require("tmp"); var log = require("./log"); var Lambda = function () { function Lambda(config) { _classCallCheck(this, Lambda); var id = crypto.randomBytes(4).toString("hex").slice(0, 6); this.config = config; this.built = false; this._folder = tmp.dirSync().name + "/" + path.parse(this.config.src).name + "-" + id; this.name = path.parse(this.config.src).name + "-" + id + "-" + process.env.AWS_ENVIRONMENT; } _createClass(Lambda, [{ key: "build", value: function build() { if (this.built) { log.info("Skipping building lambda " + this.name + "...already built."); return this; } log.info("Building lambda " + this.name); shell.exec("mkdir " + this._folder); this._writeWrapper(); shell.exec("cp " + this.config.src + "* " + this._folder); shell.exec("cp .env " + this._folder); // create package.json and copy dependencies over shell.exec("cd " + this._folder + "; npm init -y", { silent: true }); var depsMain = JSON.parse(fs.readFileSync(process.cwd() + "/" + "package.json")); var depsModule = JSON.parse(fs.readFileSync(this._folder + "/package.json")); depsModule.dependencies = depsMain.dependencies; fs.writeFileSync(this._folder + "/package.json", JSON.stringify(depsModule, null, 2), "utf8"); // copy node modules shell.exec("cp -r " + process.cwd() + "/node_modules " + this._folder); this.built = true; log.info("Built lambda " + this.name); return this; } }, { key: "deploy", value: function deploy() { if (this.deployed) { log.info("Skipping deploying lambda " + this.name + "...already deployed."); return this; } log.info("Deploying lambda " + this.name); var nodeLambdaPath = require.resolve("node-lambda/bin/node-lambda"); shell.exec("cd " + this._folder + "; " + nodeLambdaPath + " deploy", { silent: true }); this.deployed = true; log.info("Deployed lambda " + this.name); return this; } }, { key: "_writeWrapper", value: function _writeWrapper() { var wrapper = "\n var utils = require(\"express-lambda/dist/utils\");\n\n exports.handler = function(event, context) {\n var req = utils.makeRequest(event, context);\n var res = utils.makeResponse(event, context, req);\n require(\"./" + path.basename(this.config.src) + "\")(req, res);\n };"; fs.writeFileSync(this._folder + "/index.js", wrapper); } }]); return Lambda; }(); module.exports = Lambda;