UNPKG

4.34 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const archiver = require("archiver");
5const fs = require("fs-extra");
6const path = require("path");
7const base_command_1 = require("../../base-command");
8const decorators_1 = require("../../utils/decorators");
9const prepare_config_1 = require("../../utils/prepare-config");
10const CONFIG_FILE = 'bearer.config.json';
11const HANDLER_NAME = 'index';
12const HANDLER_NAME_WITH_EXT = [HANDLER_NAME, 'js'].join('.');
13class PackFunctions extends base_command_1.default {
14 async run() {
15 // we are assuming prepare and build have been run previously
16 const { args } = this.parse(PackFunctions);
17 const { ARCHIVE_PATH } = args;
18 const target = path.resolve(ARCHIVE_PATH);
19 this.debug(`Zipping to: ${target}`);
20 const output = fs.createWriteStream(target);
21 const archive = archiver('zip', {
22 zlib: { level: 9 }
23 });
24 const archiveEntries = [];
25 archive.pipe(output);
26 const archived = new Promise((resolve, reject) => {
27 output.on('close', () => {
28 resolve(archiveEntries);
29 });
30 archive.on('error', (err) => {
31 reject(err);
32 });
33 archive.on('entry', (entry) => {
34 if (!entry.stats || !entry.stats.isDirectory()) {
35 archiveEntries.push(entry);
36 }
37 });
38 archive.on('warning', (err) => {
39 this.debug(err);
40 });
41 });
42 try {
43 // add functions
44 archive.directory(this.locator.buildFunctionsDir, false);
45 // add CONFIG
46 const functions = await this.retrieveFunctions();
47 const { config, handlers } = buildLambdaDefinitions(functions);
48 const finalConfig = await this.retrieveAuthConfig(config);
49 archive.append(JSON.stringify(finalConfig, null, 2), { name: CONFIG_FILE });
50 // add handlers
51 this.debug(`Generated config: ${JSON.stringify(config, null, 2)}`);
52 archive.append(handlers, { name: HANDLER_NAME_WITH_EXT });
53 // ZIP
54 archive.finalize();
55 const entries = await archived;
56 this.debug(`Zip content: ${entries.length} files\n * ${entries.map(e => e.name).join('\n * ')}`);
57 this.success('Successfully generated lambda package');
58 // log files added to zip
59 }
60 catch (e) {
61 this.error(e);
62 }
63 }
64 async retrieveAuthConfig(config) {
65 // generate config
66 const content = await fs.readFile(this.locator.authConfigPath, { encoding: 'utf8' });
67 return Object.assign({}, config, { auth: JSON.parse(content) });
68 }
69 // TODO: rewrite this using TS AST
70 async retrieveFunctions() {
71 try {
72 const config = await prepare_config_1.default(this.locator.authConfigPath, this.bearerConfig.bearerUid, this.locator.srcFunctionsDir);
73 return config.functions;
74 }
75 catch (e) {
76 throw e;
77 }
78 }
79}
80PackFunctions.description = 'Pack integration functions';
81PackFunctions.hidden = true;
82PackFunctions.flags = Object.assign({}, base_command_1.default.flags);
83PackFunctions.args = [{ name: 'ARCHIVE_PATH', required: true }];
84tslib_1.__decorate([
85 decorators_1.RequireIntegrationFolder()
86], PackFunctions.prototype, "run", null);
87exports.default = PackFunctions;
88function buildLambdaDefinitions(functions) {
89 return {
90 config: {
91 functions: functions.reduce((acc, functionName) => {
92 acc.push({ [functionName]: [HANDLER_NAME, functionName].join('.') });
93 return acc;
94 }, [])
95 },
96 handlers: buildLambdaIndex(functions)
97 };
98}
99exports.buildLambdaDefinitions = buildLambdaDefinitions;
100function buildLambdaIndex(functions) {
101 const handlers = functions
102 .map((func, index) => {
103 const funcConstName = `func${index}`;
104 return `const ${funcConstName} = require("./dist/${func}").default;
105module.exports['${func}'] = ${funcConstName}.init();
106`;
107 })
108 .join('\n');
109 return `const captureRequest = require('@bearer/x-ray').default
110captureRequest()
111${handlers}
112 `;
113}